Forum Discussion
Generate parent id from BOM level information
I am trying to figure out a DAX formula (or Power Query) to determine each rows parent if it exists.
I have data which contains the index, level and component information. Parent column is the one I would like to generate. Below is an example of what is wanted to generate a parent-child relationship (like here https://www.daxpatterns.com/parent-child-hierarchies/)
Index Level Component Parent 1 1 A - 2 2 B 1 3 3 C 2 4 3 D 2 5 2 E 1 6 3 F 5 7 1 G - 8 2 H 7
I have accomplished this recursively with VBA to group my rows in Excel according to the BOM grouping of sub-components of each assembly. I cannot figure out how to do a similar thing with DAX since it seems recursive formulas are not possible.
There was also some example in this forum about finding earlier matching values but it does not work out of the box with my case. The below code only finds the first matching value the filter of Level - 1. It does not work with the data above for row with index 8 for example. It generates a parent id of 1 for row 8 when it should be 7.
Parent =
CALCULATE(
FIRSTNONBLANK(VALUES(BOM[Index]);0);
FILTER(
ALL(BOM);
BOM[Level] = EARLIER(BOM[Level]) - 1 &&
BOM[Index] < EARLIER(BOM[Index])
)
)
Anonymous
Your DAX formula also seems correct.
JUST REPLACE FIRSTNONBLANK with LASTNONBLANK
9 Replies
- Zubair_Muhammad
Community Champion
Anonymous
Your DAX formula also seems correct.
JUST REPLACE FIRSTNONBLANK with LASTNONBLANK
- AnonymousNot applicable
Zubair_Muhammad Thank you, that DAX suggestion works, but I could not get the Power Query one to work. It only generates null values.
Now I have two working solutions to this:
Parent = MAXX( TOPN( 1; FILTER( 'BOM'; 'BOM'[Level] = EARLIER('BOM'[Level]) - 1 && 'BOM'[Index] < EARLIER('BOM'[Index]) ); 'BOM'[Index]; DESC ); 'BOM'[Index] )and
Parent = CALCULATE( LASTNONBLANK(VALUES(BOM[Index]);0); FILTER( ALL(BOM); BOM[Level] = EARLIER(BOM[Level]) - 1 && BOM[Index] < EARLIER(BOM[Index]) ) )- Zubair_Muhammad
Community Champion
hi Anonymous
I am attaching the pbix file with Power Query Custom formulas
It works with me
- Zubair_Muhammad
Community Champion
Anonymous
As a Power Query Custom Column, try this
=try let myindex=[Index], mylevel=[Level] in Table.Max(Table.SelectRows(#"Changed Type",each [Index]< myindex and [Level]=mylevel-1),"Index")[Index] otherwise null
- richardnlove918Frequent Visitor
Great input, thank you! How can I use this approach on a dataset which only has parent and child keys but no level column?