Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Hari01
Frequent Visitor

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   
1g525>1m>g
4d345>2>3>4m>n>f>d
2n525>2m>n
3f235>2>3m>n>f
5m 15m

 

I'm preparing data for Table visiual to show manager hierarchy till top level of an(filtered single selection) employee. 

created custom column for split `top level path id` columns into rows

 

 

 

hierarchy id = Text.Split([#" ""top level path id"""] , ">")
hierarchy names = Text.Split([#" ""top level name"""] , ">")

Table.FromColumns({[hierarchy id],[hierarchy names]})

 

 

 

 Now I want level for each `hierarchy id`, so i was merging queries (like self join) like query.`hierarchy id`=query.`emp id`

and expected below result, when expanded columns after merging

 

idname    manager id   level    top level path     top level name    hierarchy id     hierarchy names    manager level    
1g525>1m>g5m1
1g525>1m>g1g2
4d745>2>7>4m>n>f>d5m1
4d745>2>7>4m>n>f>d2n2
4d745>2>7>4m>n>f>d7f3
4d745>2>7>4m>n>f>d4d4
2n525>2m>n5m1
2n525>2m>n2n2
7f275>2>7m>n>f5m1
7f275>2>7m>n>f2n2
7f275>2>7m>n>f7f3
5m 15m5m1

 

Suprisengly i got `manager level` (level in orginal dataset) column values as nulls.

 

Can we do merge on custom columns ?

 

Many thanks to anyone who can help me! 

1 ACCEPTED SOLUTION
liuqi_pbi
Resolver III
Resolver III

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"

 

liuqi_pbi_0-1660116631558.png

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!

View solution in original post

2 REPLIES 2
liuqi_pbi
Resolver III
Resolver III

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"

 

liuqi_pbi_0-1660116631558.png

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!

lbendlin
Super User
Super User

You may want to read about (and use) the PATH* functions instead.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors