Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Pivoting Data

Hi all,   I have a problem where I am trying to pivot the data in a table to get a particular output but I am struggling to find a solution.    For example, I have a table of data similar to belo...
  • Fowmy's avatar
    4 years ago

    Anonymous 

    Hope this is what you are after.


    Create a blank Query, go to the Advanced Editor, clear the existing code, and paste the codes give below and follow the steps.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTXdSwo0jUyUtJRCsnPrswH0sYGZsZGYNrI0NBUKVYHVZlPfl5Kfh5Y3sTcFEwbG2Aq80stV4jML8oGK7AwhppnDlVogc1aA0NDIG1kYWFqgqkMbq2RpZGJMUSdIYYqFFuNTExMwQotjUD2xgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Location = _t, #"1.2 High" = _t, #"1.2 Low" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"1.2 High", Int64.Type}, {"1.2 Low", Int64.Type}, {"Date", type date}}),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Date", "Location"}, "Attribute", "Value"),
        #"Added Custom Column" = Table.AddColumn(#"Unpivoted Other Columns", "Locations", each Text.Combine({Text.Start([Attribute], 4), [Location], Text.Middle([Attribute], 3)}), type text),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom Column",{"Location", "Attribute"}),
        #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Locations]), "Locations", "Value", List.Sum)
    in
        #"Pivoted Column"