Forum Discussion

RL_171's avatar
RL_171
Frequent Visitor
3 years ago
Solved

Pivot sales table

I have a data table like this: Product Date (month 1) Month 1 Rev. Date (month 2) Month 2 Rev. Date (month 3) Month 3 Rev. Apple 1/1/2022 4658 2/1/2022 599 3/1/2022 6546 Orange ...
  • Dinesh_Suranga's avatar
    3 years ago

    RL_171 

    Hi,

    Try following M code.

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WciwoyElV0lEy1DfUNzIwMgIyTcxMLYCUEULE1NISSBojBMxMTcyUYnWilfyLEvPSIfqNYJKGZkBJkH64iClExBghYmRqAtbvnVmeCdZtDFdrYmkC1m2MYhlIN5IaU6XYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, #"Date (month 1)" = _t, #"Month 1 Rev." = _t, #"Date (month 2)" = _t, #"Month 2 Rev." = _t, #"Date (month 3)" = _t, #"Month 3 Rev." = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}, {"Date (month 1)", type date}, {"Month 1 Rev.", Int64.Type}, {"Date (month 2)", type date}, {"Month 2 Rev.", Int64.Type}, {"Date (month 3)", type date}, {"Month 3 Rev.", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Product"}, "Attribute", "Value"),
    #"Added Index" = Table.AddIndexColumn(#"Unpivoted Columns", "Index", 0, 1, Int64.Type),
    #"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 1, 1, Int64.Type),
    #"Merged Queries" = Table.NestedJoin(#"Added Index1", {"Index.1"}, #"Added Index1", {"Index"}, "Added Index1", JoinKind.LeftOuter),
    #"Expanded Added Index1" = Table.ExpandTableColumn(#"Merged Queries", "Added Index1", {"Value"}, {"Added Index1.Value"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Added Index1", "Custom", each Number.IsOdd([Index.1])),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = true)),
    #"Renamed Columns" = Table.RenameColumns(#"Filtered Rows",{{"Value", "Date"}, {"Added Index1.Value", "Product#(tab)Rev"}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Renamed Columns",{"Product", "Date", "Product#(tab)Rev"})
    in
    #"Removed Other Columns"

     Thank you.