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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
HenrB
Regular Visitor

Parent missing

How to calculate parent from this SQL table? I need to create Parent-Child hierarchy somehow. 

 

data.PNG

2 ACCEPTED SOLUTIONS
Oxenskiold
Advocate I
Advocate I

Hi @MattAllington I just happend to see your name when I was browsing the list of questions to find the one that one of our customers has posted and got curious. @HenrB I don't know if this is what you need but this calculated column should find the parent code of a given line in the table you use as an example.

 

=
CALCULATE (
    LASTNONBLANK ( VALUES ( 'table'[code] ), 1 ),
    FILTER (
        ALL ( 'table'[code], 'table'[indentation] ),
        'table'[code] < VALUE ( EARLIER ( 'table'[code] ) )
            && 'table'[indentation]
                = VALUE ( EARLIER ( 'table'[indentation] ) ) - 1
    ),
    ALL ( 'table' )
)

I take it that the 'code' field is a string field hence the 'VALUE()' function. Is it a NAV table I think I can recognize the structure?

 

Merry Christmas to both of you @MattAllington@HenrB

 

View solution in original post

ImkeF
Community Champion
Community Champion

Hi @Oxenskiold,

that's a very nice code!

 

Just in case there is the need to do this in the query editor, the corresponding M-code would look like this:

 

Table.AddColumn(PreviousStep, "Parent", 
	(row) => List.Max(
		Table.SelectRows(PreviousStep, 
		each  [Code]< row[Code])[Code] 
		and [Indentation]< row[Indentation] 
   			)
      	    )

 

 

This is a Custom Column with some hand-edited code (in bold) for the nested "row-context".

  

It would enable you to break down your PC-hierarchy in an environment where you don't have to define the number of levels in advance and you could also use this simplified method for working with hierarchical schemas in PowerBI:

http://www.thebiccountant.com/2016/07/21/easy-profit-loss-account-scheme-reports-power-bi-power-pivo...

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

7 REPLIES 7
Oxenskiold
Advocate I
Advocate I

Hi @MattAllington I just happend to see your name when I was browsing the list of questions to find the one that one of our customers has posted and got curious. @HenrB I don't know if this is what you need but this calculated column should find the parent code of a given line in the table you use as an example.

 

=
CALCULATE (
    LASTNONBLANK ( VALUES ( 'table'[code] ), 1 ),
    FILTER (
        ALL ( 'table'[code], 'table'[indentation] ),
        'table'[code] < VALUE ( EARLIER ( 'table'[code] ) )
            && 'table'[indentation]
                = VALUE ( EARLIER ( 'table'[indentation] ) ) - 1
    ),
    ALL ( 'table' )
)

I take it that the 'code' field is a string field hence the 'VALUE()' function. Is it a NAV table I think I can recognize the structure?

 

Merry Christmas to both of you @MattAllington@HenrB

 

ImkeF
Community Champion
Community Champion

Hi @Oxenskiold,

that's a very nice code!

 

Just in case there is the need to do this in the query editor, the corresponding M-code would look like this:

 

Table.AddColumn(PreviousStep, "Parent", 
	(row) => List.Max(
		Table.SelectRows(PreviousStep, 
		each  [Code]< row[Code])[Code] 
		and [Indentation]< row[Indentation] 
   			)
      	    )

 

 

This is a Custom Column with some hand-edited code (in bold) for the nested "row-context".

  

It would enable you to break down your PC-hierarchy in an environment where you don't have to define the number of levels in advance and you could also use this simplified method for working with hierarchical schemas in PowerBI:

http://www.thebiccountant.com/2016/07/21/easy-profit-loss-account-scheme-reports-power-bi-power-pivo...

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

MattAllington
Community Champion
Community Champion

Read this. http://www.daxpatterns.com/parent-child-hierarchies/



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
I will not give you bad advice, even if you unknowingly ask for it.

Thanks Matt, but I have only Code and Indentation, but not Parent. What is best way to calculate Parent. After that can use for example PATH function.

 

data2.PNG

 

 

 

 

 

Create a new lookup table that does have what you want and the join it to the table you have, or replace the table you have



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
I will not give you bad advice, even if you unknowingly ask for it.

I know that I have to make some lookups or something, but how?

Well it depends.  You could create a table that just has the parent row in "Enter Data" in Power BI, then append it to the table you have.



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
I will not give you bad advice, even if you unknowingly ask for it.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

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