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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
moumitadeb
Helper I
Helper I

Need to build a Hierarchy

Hi Team,


I found a great method in this forum to build out hierarchy using PATH . However i am facing challenges with this methodology and I couldn't find a solution anywhere so I figured I'd start here.  I am trying to build a hierarchy using my data. 

 

Below the sample table data:

moumitadeb_0-1659380210563.png

Below the hierarchy tree i need to build:

moumitadeb_1-1659380258194.png

Below the Hierarchy i am getting in PowerBI Desktop:

moumitadeb_2-1659380294384.png

Any help would be highly appreciated! Thanks so much!

 

 

 

1 ACCEPTED SOLUTION
v-yangliu-msft
Community Support
Community Support

Hi  @moumitadeb ,

Here are the steps you can follow:

1. Create calculated table.

Table 3 =
DISTINCT('Table'[Child])

vyangliumsft_0-1659607158240.png

2. In Power query. Add Column Index Column From 1.

vyangliumsft_1-1659607158241.png

3. Create calculated column.

IF =
IF(
 'Table'[Parent] in SELECTCOLUMNS('Table 3',"1",[Child]),0,1
)
Group =
SUMX(FILTER(ALL('Table'),'Table'[Index]<=EARLIER('Table'[Index])),[IF])
True1 =
var _1=CALCULATE(
    MAX('Table'[Parent]),
    FILTER(ALL('Table'), 'Table'[Group]=EARLIER('Table'[Group])&&'Table'[Index]=MINX(FILTER(ALL('Table'),'Table'[Group]=EARLIER('Table'[Group])),[Index])))
    return
IF(
[Parent]=BLANK(),BLANK(),_1)
True2 =
IF(
    'Table'[Parent]='Table'[True1],
    'Table'[Child],'Table'[Parent])
True3 =
IF(
    [True2]=[Parent],[Child])
Flag =
IF(
    'Table'[True1]=BLANK()&&'Table'[True3]=BLANK(),0,
    IF(
    'Table'[True1]='Table'[Parent]&&'Table'[True2]='Table'[Child]&&'Table'[True2]<>BLANK(),1,0))

vyangliumsft_2-1659607158245.png

4. Create calculated table

Table 4 =
var _tabel1=
SUMMARIZE('Table',[True1],[True2],[True3],[Flag])
var _tabel2=
FILTER(_tabel1,[Flag]=0)
return
SUMMARIZE(_tabel2,[True1],[True2],[True3])

5. Result:

vyangliumsft_3-1659607158246.png

If you need pbix, please click here.

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

View solution in original post

9 REPLIES 9
v-yangliu-msft
Community Support
Community Support

Hi  @moumitadeb ,

Here are the steps you can follow:

1. Create calculated table.

Table 3 =
DISTINCT('Table'[Child])

vyangliumsft_0-1659607158240.png

2. In Power query. Add Column Index Column From 1.

vyangliumsft_1-1659607158241.png

3. Create calculated column.

IF =
IF(
 'Table'[Parent] in SELECTCOLUMNS('Table 3',"1",[Child]),0,1
)
Group =
SUMX(FILTER(ALL('Table'),'Table'[Index]<=EARLIER('Table'[Index])),[IF])
True1 =
var _1=CALCULATE(
    MAX('Table'[Parent]),
    FILTER(ALL('Table'), 'Table'[Group]=EARLIER('Table'[Group])&&'Table'[Index]=MINX(FILTER(ALL('Table'),'Table'[Group]=EARLIER('Table'[Group])),[Index])))
    return
IF(
[Parent]=BLANK(),BLANK(),_1)
True2 =
IF(
    'Table'[Parent]='Table'[True1],
    'Table'[Child],'Table'[Parent])
True3 =
IF(
    [True2]=[Parent],[Child])
Flag =
IF(
    'Table'[True1]=BLANK()&&'Table'[True3]=BLANK(),0,
    IF(
    'Table'[True1]='Table'[Parent]&&'Table'[True2]='Table'[Child]&&'Table'[True2]<>BLANK(),1,0))

vyangliumsft_2-1659607158245.png

4. Create calculated table

Table 4 =
var _tabel1=
SUMMARIZE('Table',[True1],[True2],[True3],[Flag])
var _tabel2=
FILTER(_tabel1,[Flag]=0)
return
SUMMARIZE(_tabel2,[True1],[True2],[True3])

5. Result:

vyangliumsft_3-1659607158246.png

If you need pbix, please click here.

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Hi,

Thanks for the  solution. I tried the same but its not giving the expected results. 

Wherein my case though i have only 2 columns (parent, child) but the hierarchy can be True1 to True-nth.

Any help is appreciated.

Sorry for the late response. 

Thanks for the  solution. I tried the same but its not giving the expected results. 

Wherein my case though i have only 2 columns (parent, child) but the hierarchy can be True1 to True-nth.

Please advise.

 

 

parry2k
Super User
Super User

@moumitadeb check this post, it will help you what you are looking for Parent-child hierarchies – DAX Patterns

 

 

Follow us on LinkedIn and YouTube.gif to our YouTube channel

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make effort to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

I have alread tried the above link..Getting the below error message:

 

MdxScript(Model)(82,27)Calculation error in measure EntityPath:The value 'Test' in Parent must also exist in Child. Please add the missing data and try again.

 

any idea how this can be resolved?

moumitadeb
Helper I
Helper I

Hi Otravers,

Thanks for your response. However i can't restructure the source data as its coming from tool directly to PowerBI.

Is there any other way to do this (if we are not using PATH function). Recommend your help.

 

 

You can restructure the data once in Power BI with Power Query.

------------------------------------------------
1. How to get your question answered quickly - good questions get good answers!
2. Learning how to fish > being spoon-fed without active thinking.
3. Please accept as a solution posts that resolve your questions.
------------------------------------------------
BI Blog: Datamarts | RLS/OLS | Dev Tools | Languages | Aggregations | XMLA/APIs | Field Parameters | Custom Visuals

Thanks for your response.

I am new to PowerBI , so require your help to restructure the data in Power Query.

Can you share some examples.

otravers
Community Champion
Community Champion

Can you restructure your source data so that columns reflect the 4 levels of depth in your chart? Grand Grand Parent > Grand Parent > Parent > Child. In the DAX PATH doc you'll see that "The path is not constrained to a single level of parent-child relationships; it can return related rows that are several levels up from the specified starting row."

------------------------------------------------
1. How to get your question answered quickly - good questions get good answers!
2. Learning how to fish > being spoon-fed without active thinking.
3. Please accept as a solution posts that resolve your questions.
------------------------------------------------
BI Blog: Datamarts | RLS/OLS | Dev Tools | Languages | Aggregations | XMLA/APIs | Field Parameters | Custom Visuals

Helpful resources

Announcements
July 2024 Power BI Update

Power BI Monthly Update - July 2024

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

July Newsletter

Fabric Community Update - July 2024

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