Forum Discussion

H_insight's avatar
H_insight
Icon for Helper V rankHelper V
3 years ago
Solved

Can I achieve the below hierarchy?

Hi All,   I am trying to achieve the below hierarchy in PQ, but not getting much success. Any chance of help?   Expected outcome: Sample excel file Link.   Many thanks
  • ImkeF's avatar
    3 years ago

    Hi H_insight ,
    this can be done with a bit of Power Query Magic:


    // Result
    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Client Key", type text}, {"Client", type text}, {"Type", type text}, {"Score", Int64.Type}, {"Status", type text}, {"ref Key", type text}}),
        #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "Type", "Type - Copy"),
        #"Added Index" = Table.AddIndexColumn(#"Duplicated Column", "Index", 0, 1, Int64.Type),
        #"Pivoted Column" = Table.Pivot(#"Added Index", List.Distinct(#"Added Index"[Type]), "Type", "Type - Copy"),
        #"Reordered Columns" = Table.ReorderColumns(#"Pivoted Column",{"Client Key", "Client"} & SortOrder & {"Score", "Status", "ref Key"}),
        #"Sorted Rows" = Table.Sort(#"Reordered Columns",{{"Client Key", Order.Ascending}} & List.Transform(SortOrder, each {_, Order.Descending})),
        #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Index"})
    in
        #"Removed Columns"
    
    // SortOrder
    let
        Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
        Custom1 = #"Changed Type"[Column1]
    in
        Custom1

    Please check the file enclosed.
    Please not that I've used a "helper table" for the sort order that you have had in your file already.
    If there will be more actions or projects in your data, you can adjust that table and the result query will consider these values accordingly.