Forum Discussion

pistachio's avatar
pistachio
Helper I
6 years ago
Solved

Unpivot (Append?) Repeating column formats

I have a bulky (55mb) excel sheet of repeating attribute readings. There are 800k rows & about 25 columns with the repeating format of ID / Datetime / Value. For example:

 

| ID   | Datetime  |   Value | (blank) | ID_1 | Datetime_1 | Value_1 | (blank) ....
| 47   |   17/3    |   17.45 | (blank) |  455 |  18/4      |   2     | (blank) ....
| 47   |   21/3    |   12    | (blank) |  455 |  12/4      |   21    | (blank) ....

 

I would like to combine these into a single power query table of 3 columns and possibly a couple million rows

| ID   | Datetime  |   Value |

 

  • ImkeF's avatar
    ImkeF
    6 years ago

    Hi pistachio  

    sorry, just read your second comment previously. you can try the following technique, less "intelligent" action required:

     

     

    let
        Source = Web.Page(Web.Contents("https://community.powerbi.com/t5/Desktop/Unpivot-Append-Repeating-column-formats/m-p/964070/highlight/false#M462025")),
        Data0 = Source{0}[Data],
        #"Promoted Headers" = Table.PromoteHeaders(Data0, [PromoteAllScalars=true]),
        #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"ID", Int64.Type}, {"Datetime", type datetime}, {"Value", Int64.Type}, {"", type text}, {"ID_1", Int64.Type}, {"Datetime_2", type datetime}, {"Value_3", type number}, {"_4", type text}, {"ID_5", Int64.Type}, {"Datetime_6", type datetime}, {"Value_7", type number}, {"_8", type text}, {"ID_9", Int64.Type}, {"Datetime_10", type datetime}, {"Value_11", type number}}),
        Custom1 = Table.ToColumns(#"Changed Type"),
        #"Converted to Table" = Table.FromList(Custom1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Added Index" = Table.AddIndexColumn(#"Converted to Table", "Index", 0, 1),
        #"Integer-Divided Column" = Table.TransformColumns(#"Added Index", {{"Index", each Number.IntegerDivide(_, 4), Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Integer-Divided Column", {"Index"}, {{"Partition", each Table.FromColumns(_[Column1]), type table [Column1=list, Index=number]}}, GroupKind.Local),
        Custom2 = Table.Combine(#"Grouped Rows"[Partition])
    in
        Custom2

     

    For performance it is crucial to use the "GroupKind.Local" in step "Grouped Rows"

    Please let me know about the performance difference to the first Pivot-solution, thanks.

     

    Please not that for this solution it is crucial that you always have the same number of columns per repetition!!

     

8 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    So, there are probably easier ways to do this and ImkeF probably has the solution. But, worst case, create a query from the Excel file and remove all but the first 3 columns. Create a second query, choose the next 3 columns (ID_1, Datetime_1, Value_1) and remove all the other columns. Rename the columns to ID, Datetime, Value. Rinse and repeat the same steps for the second query for a third, fourth, fifth query, etc. Then, use an Append query to append them all together.

    • pistachio's avatar
      pistachio
      Helper I

      Yep cheers I've done this but each query seems like it has to reload the 55mb workbook so it takes a long time to get all together. Wondering if there was a better way

      • ImkeF's avatar
        ImkeF
        Community Champion

        Hi pistachio  

        sorry, just read your second comment previously. you can try the following technique, less "intelligent" action required:

         

         

        let
            Source = Web.Page(Web.Contents("https://community.powerbi.com/t5/Desktop/Unpivot-Append-Repeating-column-formats/m-p/964070/highlight/false#M462025")),
            Data0 = Source{0}[Data],
            #"Promoted Headers" = Table.PromoteHeaders(Data0, [PromoteAllScalars=true]),
            #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"ID", Int64.Type}, {"Datetime", type datetime}, {"Value", Int64.Type}, {"", type text}, {"ID_1", Int64.Type}, {"Datetime_2", type datetime}, {"Value_3", type number}, {"_4", type text}, {"ID_5", Int64.Type}, {"Datetime_6", type datetime}, {"Value_7", type number}, {"_8", type text}, {"ID_9", Int64.Type}, {"Datetime_10", type datetime}, {"Value_11", type number}}),
            Custom1 = Table.ToColumns(#"Changed Type"),
            #"Converted to Table" = Table.FromList(Custom1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
            #"Added Index" = Table.AddIndexColumn(#"Converted to Table", "Index", 0, 1),
            #"Integer-Divided Column" = Table.TransformColumns(#"Added Index", {{"Index", each Number.IntegerDivide(_, 4), Int64.Type}}),
            #"Grouped Rows" = Table.Group(#"Integer-Divided Column", {"Index"}, {{"Partition", each Table.FromColumns(_[Column1]), type table [Column1=list, Index=number]}}, GroupKind.Local),
            Custom2 = Table.Combine(#"Grouped Rows"[Partition])
        in
            Custom2

         

        For performance it is crucial to use the "GroupKind.Local" in step "Grouped Rows"

        Please let me know about the performance difference to the first Pivot-solution, thanks.

         

        Please not that for this solution it is crucial that you always have the same number of columns per repetition!!

         

    • ImkeF's avatar
      ImkeF
      Community Champion

      Hi pistachio  

      the following steps should do the work:

      1) Add an Index column to the table

      2) Check ID-column & new Index-column -> Transform (or righ-mouse-click) -> Unpivot other columns

      3) Split "Attribute"-column by underscore "_"

      4) Pivot "back" on the first splitted column ("Attribute.1") with "Values.1" as the "Value"-field and select "Don't aggregate" in the advanced options

       

      Paste the following code into the advanced editor and you can follow the steps:

       

      let
          Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjFX0lEyNNc3MAbTOiamQBrEtNA3AVJGSrE6UEVGhvpgNUZQBUb6RmBRpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, Datetime = _t, Value = _t, Column1 = _t, Datetime_1 = _t, Value_1 = _t]),
          #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Datetime", type date}, {"Value", Int64.Type}, {"Column1", type text}, {"Datetime_1", type date}, {"Value_1", Int64.Type}}),
          #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
          #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"ID", "Index"}, "Attribute", "Value.1"),
          #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"Attribute.1", "Attribute.2"}),
          #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Attribute.1", type text}, {"Attribute.2", Int64.Type}}),
          #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Changed Type1", {{"Attribute.1", type text}}, "en-GB"), List.Distinct(Table.TransformColumnTypes(#"Changed Type1", {{"Attribute.1", type text}}, "en-GB")[Attribute.1]), "Attribute.1", "Value.1"),
          #"Removed Other Columns" = Table.SelectColumns(#"Pivoted Column",{"ID", "Value", "Datetime"})
      in
          #"Removed Other Columns"

      Next time if you paste sample data, please use HTML-table like described here: https://community.powerbi.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216

       

      • pistachio's avatar
        pistachio
        Helper I

        Hi Imke, 

         

        Thanks very much for your detailed response. I get confused at the unpivoting stage but I think this is the right track. When I pasted your code steps into power query they show the base data as only having one ID column. I've taken your advice and pasted more example data as HTML.

         

        This is straight from the Excel, I assume Power Query puts the _2, _3 suffixes etc when I use first row as headers. In reality there are many more column groups

         

        IDDatetimeValue IDDatetimeValue IDDatetimeValue IDDatetimeValue
        40728/01/2020 14:36:270 728/01/2020 14:36:402.83 6828/01/2020 14:36:460.26 5728/01/2020 14:36:380.72
        40728/01/2020 14:37:270 728/01/2020 14:37:402.82 6828/01/2020 14:37:460.25 5728/01/2020 14:37:380.72
        40728/01/2020 14:38:270 728/01/2020 14:38:402.83 6828/01/2020 14:38:460.26 5728/01/2020 14:37:560.66
        40728/01/2020 14:39:270 728/01/2020 14:39:402.77 6828/01/2020 14:39:460.25 5728/01/2020 14:38:580.68
        40728/01/2020 14:40:270 728/01/2020 14:40:182.81 6828/01/2020 14:40:460.26 5728/01/2020 14:39:580.7

         

        Can I convert this to a single 3 column table?