Forum Discussion

Hari01's avatar
Hari01
Frequent Visitor
4 years ago
Solved

Self Merge Queries on custom columns

Hi Everyone,   I have employee and manager id data. Below is sample data   emp id    name    manager id    level    top level path id    top level name    1 g 5 2 5>1 m>g 4 d ...
  • liuqi_pbi's avatar
    4 years ago

    Hi Hari01 

     

    Here is my solution with Power Query. You can create a blank query, open its Advanced editor and paste below code to replace everything there. Click ok to save the query. 

     

    let
      Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VYwxDsAgDAO/UmVmAdI1H0HZEEzh/yNOCkOHEyi2rzXKlGiCFxR/xS8mkzQ1Yvw7qIAjLVKFo7FkSI+W79bPUb5GpL4dJ6nXcQ3R8J2BB+TjMVLd", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"emp id" = _t, name = _t, #"manager id" = _t, level = _t, #"top level path id" = _t, #"top level name" = _t]),
      #"Changed column type" = Table.TransformColumnTypes(Source, {{"emp id", Int64.Type}, {"name", type text}, {"manager id", Int64.Type}, {"level", Int64.Type}, {"top level path id", type text}, {"top level name", type text}}),
      #"Duplicated column" = Table.DuplicateColumn(#"Changed column type", "top level path id", "hierarchy id"),
      #"Split column by delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Duplicated column", {{"hierarchy id", Splitter.SplitTextByDelimiter(">"), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "hierarchy id"),
      #"Changed column type 1" = Table.TransformColumnTypes(#"Split column by delimiter", {{"hierarchy id", Int64.Type}}),
      #"Merged queries" = Table.NestedJoin(#"Changed column type 1", {"hierarchy id"}, #"Changed column type 1", {"emp id"}, "Changed column type 1", JoinKind.LeftOuter),
      #"Added custom" = Table.AddColumn(#"Merged queries", "manager level", each Table.RowCount([Changed column type 1])),
      #"Expanded Changed column type 1" = Table.AggregateTableColumn(#"Added custom", "Changed column type 1", {{"name", List.Max, "hierarchy names"}})
    in
      #"Expanded Changed column type 1"

     

    Hope this helps. Let me know if you have any questions. 

     

    ----------------------------------------------------------------------

    If this reply helps solve the problem, please mark it as Solution! Kudos are appreciated too!