Forum Discussion

Justas4478's avatar
Justas4478
Post Prodigy
2 years ago
Solved

Turning rows in to columns

Hello, I am trying to turn som rows in to columns. In a sense opposite of unpivot. This is table that I have. And this is my expected end result: Its really simple but I cant seam to m...
  • Gabry's avatar
    Gabry
    2 years ago

    this is the M code to do the transformation

    let
    Source = Excel.Workbook(File.Contents("C:\Users\----\Downloads\Sample data.xlsx"), null, true),
    Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"Agency", type text}, {"Dept/Shift", type text}, {"RATE", type text}, {"PAYRATE", type number}, {"CHGRATE", type number}}),
    #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Changed Type", {"PAYRATE", "CHGRATE"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Only Selected Columns", "Custom", each [RATE]& " " &
    [Attribute]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"RATE", "Attribute"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Custom]), "Custom", "Value", List.Sum)
    in
    #"Pivoted Column"