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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
joshua1990
Post Prodigy
Post Prodigy

Determine level within multi level parent child hierarchy

Hi all!

I have a table that shows me a multi level parent child hierarchy:

Parent Child Quantity
1 A1 1
1 XC2 2
A1 B2 5
B2 XC7 8

 

As of now, I don't know how many levels are distributed in this table.

Furthermore, I don't know the level for each parent-child combination.

 

How can I calculate both?

 

In the end, I would like to have this:

Parent Child Quantity Level
1 A1 1 1
1 XC2 2 2
A1 B2 5 3
B2 XC7 8 4

 

How would you do this in Power Query?

6 REPLIES 6
wdx223_Daniel
Super User
Super User

wdx223_Daniel_0-1674789926309.png

why the level in your table is 1,2,3,4?

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEEYZKsToQboSzEZA0AvPBUk4gvimYD2ZGOJsDSQul2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Parent = _t, Child = _t, Quantity = _t]),
    fx=(t)=>
        let
           a=Source{[Child=t]}?[Parent]?
        in if a=null then 1 else @Fx(a)+1,
    Custom1 = Table.AddColumn(Source,"Level",each fx([Parent]))
in
    Custom1
nitishsh91
Solution Supplier
Solution Supplier

Hi @joshua1990 

 

In the query, add a custom column that calculates the level for each parent-child combination by calling a recursive function. The recursive function will take the current parent-child combination as input and check if the parent is also present in the child column. If so, it will call itself with the new parent-child combination, and increment the level by . Otherwise, it will return the current level.

You can define the function like this:

let
hierarchy = [Parent = "1", Child = "A1", Quantity = 1],
level = 1,
findLevel = (hierarchy, level) =>
let
parent = hierarchy[Parent],
child = hierarchy[Child],
newHierarchy = Table.SelectRows(hierarchy, each [Parent] = child),
newLevel = level + 1
in
if List.Count(newHierarchy) = 0 then
level
else
findLevel(newHierarchy{0}, newLevel)
in
findLevel(hierarchy, level)

 

Then you can use this function in a custom column, you can call this function by passing the current row to it, and use the result in a custom column. 

= Table.AddColumn(hierarchy, "Level", each findLevel([Parent = [Parent], Child = [Child]], 1))

 

Finally, you can expand the new column to have it in your table.

Please note, this example is based on the assumption that you have only one Parent-child combination in the table. If there are multiple parent-child combinations, you need to iterate over the table rows, and call the function for each row.

Please let me know if this helps or if you have any other questions.

@nitishsh91 : Thanks a lot! Since it is multi level, how would you iterate this per row? I don't get it.

@nitishsh91 : ANy idea?

The function will take parent n child as input n it will have to be added as a new column to give desired result, I believe it should work though still need to try it myself as I have visualised it conceptually the way an algorithm works.

I will try this in detail sometime tomorrow n let you know as I couldn't find time to work out this solution n had marked it for further analysis.

@nitishsh91 : Thanks a lot for your support. Any updates?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors