Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Unpivot or unstack columns dynamically

I get data from outkook emails which are stacked in power query where each day's emails have tables with different headers. Currently data is like below where first 4 rows represent table 1 from day...
  • ronrsnfld's avatar
    3 years ago

    Try this:

    It works on your sample

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY0rDoAwEESv0qxGMOUvoT0DpqmowGER3J6diq1BvGYy3cxLSWJ5HaSTs9zP5XZLh6UguUvCH955ZagNL0ZlUubaBE2Lsipbbbjuf9aDpdjWez50wJsANIAKNAcoAS1QTf4A", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{
            {"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}}),
    
    //Add column to group by dates
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Day", each if Text.StartsWith([Column1],"Day") then [Column1] else null, type nullable text),
        #"Filled Down" = Table.FillDown(#"Added Custom",{"Day"}),
    
    //Unpivot each subgroup
        #"Grouped Rows" = Table.Group(#"Filled Down", {"Day"}, {
            {"UNPivot", (t)=>
                let 
                    #"Promote Headers" = Table.PromoteHeaders(t),
                    #"Removed Columns" = Table.RemoveColumns(#"Promote Headers", List.Last(Table.ColumnNames(#"Promote Headers"))),
                    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{List.First(Table.ColumnNames(#"Removed Columns")),"Category"}}),
                    #"Unpivot" = Table.UnpivotOtherColumns(#"Renamed Columns",{"Category"},"Type","Value"),
                    #"Value to Number" = Table.TransformColumns(#"Unpivot",{"Value", each Number.From(_)})
                in 
                    #"Value to Number",
                type table[Category=text, Type=text, Value=number]}
            }),
        
        #"Expanded UNPivot" = Table.ExpandTableColumn(#"Grouped Rows", "UNPivot", {"Category", "Type", "Value"})
     
    in
        #"Expanded UNPivot"