Forum Discussion

nirvana_moksh's avatar
nirvana_moksh
Icon for Impactful Individual rankImpactful Individual
7 years ago
Solved

Transpose of table

Hello All,   So I have tried pivot and having an index column in the main table and few other approaches but I cannot achieve the following. My main table is like below:   ID Name Name 2 Da...
  • Ashish_Mathur's avatar
    7 years ago

    Hi,

    This M code works

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Name", type text}, {"Name 2", type text}, {"Date", type text}, {"Detail", type text}, {"Rank 1 ", Int64.Type}, {"Rank 2", Int64.Type}, {"Rank 3", Int64.Type}, {"Category", type text}, {"Movement", type text}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Index"}, "Attribute", "Value"),
        Partition = Table.Group(#"Unpivoted Other Columns", {"Index"}, {{"Partition", each Table.AddIndexColumn(_, "Index1",1,1), type table}}),
        #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Attribute", "Value", "Index1"}, {"Attribute", "Value", "Index1"}),
        #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Expanded Partition", {{"Index", type text}}, "en-IN"), List.Distinct(Table.TransformColumnTypes(#"Expanded Partition", {{"Index", type text}}, "en-IN")[Index]), "Index", "Value"),
        #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Index1"})
    in
        #"Removed Columns"

    The only thing i cannot get to work here is the order of the items in the attribute column to match your order.

    Hope this helps.