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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
aarongh
New Member

Flatten a parent-child hierarchy of unknown depth, with a separate sort/weight field

Hi there,

 

I need to take a parent-child hierarchy of unknown/variable depth, and convert it into a structure that gives the depth level and the full path from the child to the parent.

 

I've found some very helpful articles on the topic, especially this one: https://pivotalbi.com/dynamically-flatten-a-parent-child-hierarchy-using-power-query-m/

 

However, I have an addition requirement, in that the structure should be in a specifc order. For example:

 

ParentChildWeight
 CompanyName1
CompanyNameDepartment A3
Department AUnit X9
Department AUnit Y7
Department AUnit Z3
Company NameDepartment B1
Department BUnit P2
Department BUnit S5

  

So, the final structure needs to be built dynamically, but sortable using the weights above, to give something like this:

  

ParentSortDepthLevel 1Level 2IsLeaf
CompanyName11  

False

CompanyName1.22Department B False
CompanyName1.2.23Department BUnit PTrue
CompanyName1.2.53Department BUnit STrue
CompanyName1.32Department A False
CompanyName1.3.33Department AUnit ZTrue
CompanyName1.3.73Department AUnit YTrue
CompanyName1.3.93Department AUnit XTrue

 

Unfortunately, the PowerQuery M code in the link above is way above my head, and I can't figure out how to modify it to use the Weight as a sorting mechanism.

 

Any help or pointers would be really appreciated!  

2 REPLIES 2
smpa01
Super User
Super User

@aarongh  you data probably corresponds to the json below. Where do you expect the `unknown depth` in this dataset to occur and how does the resulting dataset needs to look like with that change in depth occuring?

 

const data = [{
    "levelOneAttr": {
        "companyName": 1,
        "weight": 1,
        "levelTwoAttr": [{
                "deptName": "A",
                "weight": 3,
                "children": [
                    { "deptName": "X", "weight": 9 },
                    { "deptName": "Y", "weight": 7 },
                    { "deptName": "Z", "weight": 3 }
                ]
            },
            {
                "deptName": "B",
                "weight": 1,
                "children": [
                    { "deptName": "P", "weight": 2 },
                    { "deptName": "S", "weight": 5 }

                ]
            }

        ]
    }
}];

 

 

 

 

.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
aarongh
New Member

Sorry, I just saw a mistake in the results table!

 

The 'Sort' string is derived from the 'Weight' field. As the 'Weight' value for Department B is '1', then then 'Sort' values for Department B should start with 1.1, not 1.2.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors