TOP

Reorganization of tables (Unpivot) in LibreOffice Calc

Description

Sometimes there is a need to convert a cross (pivot) table into a flat one. This procedure is called " data straightening " (unpivot data) . As a result of such " straightening " we get a table where all similar data are in one column.

The program LibreOffice Calc does not have a standard functionality for converting pivot tables to " flat ", but you can write the corresponding procedure yourself. Let's see how to do it.



Let's look at the picture below to understand the essence of the problem more clearly. On the left we have a table in the form of a matrix (pivot table), and on the right - a regular table with columns (flat table):

When you expand a table, you unpack the attribute-value pair that is the intersection of the columns in the matrix table and reorient them into the flattened columns in the flat table:

  • Value (Values) (blue on the left) expand into a new column (blue on the right);
  • Attributes (Attributes) (green on the left) are also expanded into a new column (green on the right) and duplicated according to the new column of values.
  • StarBASIC code for the UnPivotTable procedure

    Open the menu Tools - Macros - Edit Macros..., select Module1 and copy the following text into the module:

    1. Sub UnPivotTable  
    2.   ' moonexcel.com.ua   
    3.   Dim oBook        As Object  
    4.   Dim oActiveSheet As Object  
    5.   Dim oSelRange    As Object  
    6.   Dim oNewSheet    As Object  
    7.     
    8.   Dim i                  As Long  
    9.   Dim iTopLabelRowCount  As Integer  
    10.   Dim iSideLabelColCount As Integer         
    11.     
    12.   oBook        = ThisComponent  
    13.   oActiveSheet = oBook.CurrentController.ActiveSheet  
    14.   oSelRange    = oBook.CurrentSelection  
    15.     
    16.   ' Add a new sheet   
    17.   oBook.Sheets.insertNewByName("UnPivoted Table",0)  
    18.   oNewSheet = oBook.Sheets(0)  
    19.     
    20.   ' Determine the size of the headers   
    21.   iTopLabelRowCount  = InputBox(" How many lines in the top header? ")  
    22.   iSideLabelColCount = InputBox(" How many columns in the side header? ")  
    23.               
    24.   i = 0  
    25.        
    26.   For r = (iTopLabelRowCount + 1) To oSelRange.Rows.Count  
    27.     For c = (iSideLabelColCount + 1) To oSelRange.Columns.Count              
    28.                          
    29.       For j = 1 To iSideLabelColCount                
    30.         oNewSheet.getCellByPosition(j-1, i).Formula = oSelRange.getCellByPosition(j-1, r-1).Formula                 
    31.       Next j  
    32.                
    33.       For k = 1 To iTopLabelRowCount  
    34.         oNewSheet.getCellByPosition(j + k - 2, i).Formula = oSelRange.getCellByPosition(c-1, k-1).Formula  
    35.       Next k             
    36.                            
    37.       oNewSheet.getCellByPosition(j + k - 2, i).Formula = oSelRange.getCellByPosition(c-1, r-1).Formula  
    38.       i = i + 1                     
    39.               
    40.     Next c  
    41.   Next r  
    42. End Sub  

    Then, close Macro Editor and return to your worksheet in LibreOffice Calc. Select the entire table with the top and left headers and run our new macro via the menu Tools - Macros - Run Macro...

    The macro will insert a new named sheet into your workbook "UnPivoted Table" , and then add a table in the new format. With such a table you can do any analysis in LibreOffice Calc.

    Using the extension

    You can also use the utility "Pivot table (Unpivot)" by installing the extension YLC_Utilities.oxt .

    After that, this utility will be available in all files that will be opened in LibreOffice Calc.

    Related Articles:

  • Reorganization of tables (Unpivot) in Excel