SOLVED

Parent-child flattening (multiple levels)

%3CLINGO-SUB%20id%3D%22lingo-sub-2744756%22%20slang%3D%22en-US%22%3EParent-child%20flattening%20(multiple%20levels)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2744756%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3EI%20have%20an%20interesting%20problem%20to%20work%20out%20and%20I'm%20sure%20there's%20a%20very%20efficient%20and%20elegant%20solution%20using%20Power%20Query.%20Before%20I%20start%20tinkering%20myself%20(in%20probably%20not%20the%20most%20efficient%20way)%3A%20how%20would%20you%20approach%20this%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20dataset%20with%20parents%2C%20children%20and%20quantities%20of%20children%20in%20the%20parent.%26nbsp%3B%3C%2FP%3E%3CP%3EI%20also%20have%20sales%20figures%20(quantities)%20of%20(some%20of%20the)%20parents%20and%20children.%20Some%20components%20are%20sold%20both%20as-is%2C%20and%20as%20part%20of%20a%20parent.%3C%2FP%3E%3CP%3EWhat%20I%20need%20is%20the%20quantity%20of%20all%20components%2C%20flattened.%20See%20below%20(and%20attached%20workbook)%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22bartvana_3-1631510986959.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F309981i2A8187441D62E90E%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22bartvana_3-1631510986959.png%22%20alt%3D%22bartvana_3-1631510986959.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EAny%20help%20to%20put%20me%20on%20the%20right%20track%20would%20be%20greatly%20appreciated!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENote%20that%3A%3C%2FP%3E%3CUL%3E%3CLI%3ESome%20of%20the%20children%20contain%20grandchildren%2C%20which%20in%20turn%20contain%20grand-grandchildren%2C%20and%20so%20on%2C%20and%20the%20maximum%20depth%20is%20unknown%20beforehand.%3C%2FLI%3E%3C%2FUL%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2744756%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBI%20%26amp%3B%20Data%20Analysis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPower%20BI%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2752523%22%20slang%3D%22en-US%22%3ERe%3A%20Parent-child%20flattening%20(multiple%20levels)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2752523%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F863782%22%20target%3D%22_blank%22%3E%40bartvana%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBased%20on%20current%20dataset%2C%20the%20number%20of%20Children%20per%20parent%20does%20not%20look%20correct%3F%3C%2FP%3E%3CP%3EComp3%20has%20100%20children%20and%26nbsp%3B%3C%2FP%3E%3CP%3EComp3.1%20has%2020%20children%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Yea_So_0-1631708948217.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F310570iE5149C1427B873DA%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Yea_So_0-1631708948217.png%22%20alt%3D%22Yea_So_0-1631708948217.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2753404%22%20slang%3D%22en-US%22%3ERe%3A%20Parent-child%20flattening%20(multiple%20levels)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2753404%22%20slang%3D%22en-US%22%3EThe%2020%20children%20of%20COMP3.1%20are%20only%20related%20to%20COMP3.1%2C%20not%20upstream.%3CBR%20%2F%3ESo%20Comp3%20has%20100%20x%2020%20x%20Comp3.1.1.%3CBR%20%2F%3EThe%20quantities%20only%20refer%20to%201%20level%20lower.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2753441%22%20slang%3D%22en-US%22%3ERe%3A%20Parent-child%20flattening%20(multiple%20levels)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2753441%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F863782%22%20target%3D%22_blank%22%3E%40bartvana%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20you're%20saying%20Comp3%20has%20100%20children%2C%20its%20just%20not%20showing%20in%20the%20current%20sample%20dataset.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2753536%22%20slang%3D%22en-US%22%3ERe%3A%20Parent-child%20flattening%20(multiple%20levels)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2753536%22%20slang%3D%22en-US%22%3E%3CP%3ENo%2C%20not%20really.%20Comp3%20has%20100%20children%20(named%20Comp3.1)%2C%20and%20each%20of%20these%20children%20has%2020%20children%20(Comp3.1.1)%2C%20which%20are%20grandchildren%20if%20Comp3.1.%20So%20Comp3%20has%20100%20children%20Comp3.1%2C%20and%202000%20grandchildren%20Comp3.1.1.%3CBR%20%2F%3E(Apart%20from%20that%20branch%2C%20there%20are%20also%20other%20children%20and%20grandchildren%2C%20like%20Comp3.2%20and%20Comp3.2.1.)%3CBR%20%2F%3E%3CSTRIKE%3EI%20did%20make%20an%20error%2C%20I%20see%20now%2C%20with%20Comp3.2%20and%20Comp3.1.2%2C%20I'll%20fix%20that%20when%20I%20get%20home.%20But%20maybe%20you%20can%20see%20the%20general%20idea%3F%26nbsp%3B%3C%2FSTRIKE%3EUpdate%3A%20the%20data%20as%20such%20was%20correct%2C%20but%20it's%20more%20logical%20to%20name%203.1.2%203.2.1%2C%20so%20I%20attached%20a%20new%20workbook.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2760907%22%20slang%3D%22en-US%22%3ERe%3A%20Parent-child%20flattening%20(multiple%20levels)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2760907%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F863782%22%20target%3D%22_blank%22%3E%40bartvana%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYour%20narrative%20is%20ambiguous%2C%20I%20see%20in%20the%20screenshot%20that%20KOF%20has%20two%20children%2C%20and%20yet%20in%20the%20%23children%20in%20Parent%20for%20PIRO%20says%20three.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESomehow%20either%20you%20are%20skipping%20details%20in%20your%20narrative%20maybe%20KOF%20has%20a%20secret%20or%20your%20narrative%20is%20not%20explained%20very%20well%20for%20any%20reader%20to%20comprehend%20the%20problem.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2761088%22%20slang%3D%22en-US%22%3ERe%3A%20Parent-child%20flattening%20(multiple%20levels)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2761088%22%20slang%3D%22en-US%22%3E%22KOF%20%3B%20PIRO%20%3B%203%22%20means%3A%20in%20every%20KOF%20there%20are%203%20PIRO's.%20(Apart%20from%20all%20the%20rest%20that%20KOF%20may%20contain).%3CBR%20%2F%3ESo%20if%20you%20see%20it%20like%20that%2C%20I%20think%20the%20narrative%20is%20coherent%3F%3CBR%20%2F%3EThanks%20for%20looking%20into%20my%20problem!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2761167%22%20slang%3D%22en-US%22%3ERe%3A%20Parent-child%20flattening%20(multiple%20levels)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2761167%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F863782%22%20target%3D%22_blank%22%3E%40bartvana%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20your%20sample%20COMP3%20has%20two%20different%20parents%20-%20PIRO%20and%20PRIM.%20Is%20that%20misprint%20%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2761207%22%20slang%3D%22en-US%22%3ERe%3A%20Parent-child%20flattening%20(multiple%20levels)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2761207%22%20slang%3D%22en-US%22%3Eso%20then%20PRIM%20should%20be%204%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2761459%22%20slang%3D%22en-US%22%3ERe%3A%20Parent-child%20flattening%20(multiple%20levels)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2761459%22%20slang%3D%22en-US%22%3ENo%2C%20it's%20not%20a%20misprint.%20Maybe%20the%20term%20Parent%2FChild%20is%20misleading%20here.%20You%20can%20see%20it%20as%20follows%3A%3CBR%20%2F%3EPIRO%20is%20an%20electronic%20device%20that%20contains%2C%20among%20other%20parts%2C%20COMP3%20(let's%20say%20COMP3%20is%20a%20meter%20of%20wire%2C%20so%200.5%20qty%20of%20the%20item%20COMP3%20is%20half%20a%20meter%20of%20wire).%3CBR%20%2F%3EPIRM%20is%20another%20electronic%20device%20that%20also%20contains%2C%20among%20other%20parts%2C%20COMP3%20(in%20the%20case%20of%20PIRM%2010%20meters%20of%20wire).%3CBR%20%2F%3EOne%20COMP3%2C%20in%20turn%2C%20contains%20100%20COMP3.1%2C%20et%20cetera.%3CBR%20%2F%3E(The%20data%20is%20fictitious%20so%20don't%20go%20too%20far%20in%20the%20details%20of%20this%20example%2C%20but%20that's%20how%20the%20numbers%20should%20be%20understood).%3CBR%20%2F%3EDoes%20that%20make%20sense%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2761489%22%20slang%3D%22en-US%22%3ERe%3A%20Parent-child%20flattening%20(multiple%20levels)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2761489%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F863782%22%20target%3D%22_blank%22%3E%40bartvana%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20have%20to%20normalize%20your%20components%20first%20and%20establish%20a%20lowest%20unit%20of%20measure%20for%20each%20component%2C%20then%20you%20can%20group%20like%20components%20that%20go%20to%20like%20devices%20etc%20etc.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2761583%22%20slang%3D%22en-US%22%3ERe%3A%20Parent-child%20flattening%20(multiple%20levels)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2761583%22%20slang%3D%22en-US%22%3EI'm%20sorry%2C%20I%20don't%20quite%20follow%2C%20can%20you%20give%20an%20example%20please%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2761622%22%20slang%3D%22en-US%22%3ERe%3A%20Parent-child%20flattening%20(multiple%20levels)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2761622%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F863782%22%20target%3D%22_blank%22%3E%40bartvana%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELet's%20make%20coffee%20an%20example.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ea%20cup%20of%20coffee%20has%20several%20components%3A%3C%2FP%3E%3CP%3Ecoffee%3C%2FP%3E%3CP%3Ecreamer%3C%2FP%3E%3CP%3Esweetener%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethe%20lowest%20unit%20of%20measure%20when%20you're%20making%20coffee%20is%20how%20many%20scoops%20to%20make%201%20cup%20of%20coffee%20do%20you%20need%20to%20put%20in%20the%20coffee%20maker%20maybe%201%20or%202%20scoops%20of%20grounds%20to%20make%204%20cups%3C%2FP%3E%3CP%3Eusually%20creamer%20is%20two%20teaspoons%20variance%20of%20%2B%2F-%201%3C%2FP%3E%3CP%3Eand%20usually%202%20packets%20of%20sweetener%20variance%20of%20%2B%2F-%201%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eso%20for%20each%20device%20what%20is%20the%20usual%20components%20and%20their%20usual%20lowest%20unit%20of%20measure%3C%2FP%3E%3CP%3Eso%20if%20you%20are%20making%2010%20devices%20you%20would%20be%20using%20x%20number%20of%20like%20components%20example%20screws%20how%20many%20screws%20do%20you%20need%20to%20assemble%20a%20pc%20etc..%3C%2FP%3E%3CP%3Ethe%20reason%20for%20lowest%20unit%20of%20measure%20is%20for%20easy%20costing%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ewhen%20you%20buy%20corned%20beef%20for%20example%20each%20can%20is%20a%20fixed%20ounces%20and%20each%20case%20is%20say%2024%20cans%20etc.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eso%20if%20you%20think%20about%20it%20all%20products%20in%20the%20market%20are%20packaged%20in%20like%20unit%20of%20measure.%3C%2FP%3E%3CP%3Ecan%20you%20imagine%20selling%20a%20case%20of%20soft%20drink%203%20two%20litter%20bottles%204%208%20ounces%2C%2010%2012%20ounces%20etc%3C%2FP%3E%3CP%3Ehow%20do%20you%20cost%20that%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2761703%22%20slang%3D%22en-US%22%3ERe%3A%20Parent-child%20flattening%20(multiple%20levels)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2761703%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F863782%22%20target%3D%22_blank%22%3E%40bartvana%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20see%2C%20thank%20you.%20I%20thought%20about%20building%20hierarchy%20to%20summaries%20by%20its%20branches%2C%20but%20here%20that%20won't%20work.%20Is%20maximum%20number%20of%20levels%20known%20or%20it%20differ%20from%20case%20to%20case%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2762564%22%20slang%3D%22en-US%22%3ERe%3A%20Parent-child%20flattening%20(multiple%20levels)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2762564%22%20slang%3D%22en-US%22%3ENo%2C%20maximum%20number%20of%20levels%20is%20not%20known%20in%20advance.%3C%2FLINGO-BODY%3E
Frequent Contributor

Hello,

I have an interesting problem to work out and I'm sure there's a very efficient and elegant solution using Power Query. Before I start tinkering myself (in probably not the most efficient way): how would you approach this?

 

I have a dataset with parents, children and quantities of children in the parent. 

I also have sales figures (quantities) of (some of the) parents and children. Some components are sold both as-is, and as part of a parent.

What I need is the quantity of all components, flattened. See below (and attached workbook):

bartvana_3-1631510986959.png

Any help to put me on the right track would be greatly appreciated!

 

Note that:

  • Some of the children contain grandchildren, which in turn contain grand-grandchildren, and so on, and the maximum depth is unknown beforehand.
30 Replies

@bartvana 

 

Based on current dataset, the number of Children per parent does not look correct?

Comp3 has 100 children and 

Comp3.1 has 20 children?

 

Yea_So_0-1631709643153.png

 

 

The 20 children of COMP3.1 are only related to COMP3.1, not upstream.
So Comp3 has 100 x 20 x Comp3.1.1.
The quantities only refer to 1 level lower.

@bartvana 

 

So you're saying Comp3 has 100 children, its just not showing in the current sample dataset.

No, not really. Comp3 has 100 children (named Comp3.1), and each of these children has 20 children (Comp3.1.1), which are grandchildren if Comp3.1. So Comp3 has 100 children Comp3.1, and 2000 grandchildren Comp3.1.1.
(Apart from that branch, there are also other children and grandchildren, like Comp3.2 and Comp3.2.1.)
I did make an error, I see now, with Comp3.2 and Comp3.1.2, I'll fix that when I get home. But maybe you can see the general idea? Update: the data as such was correct, but it's more logical to name 3.1.2 3.2.1, so I attached a new workbook.

@bartvana 

 

Your narrative is ambiguous, I see in the screenshot that KOF has two children, and yet in the #children in Parent for PIRO says three.

 

Somehow either you are skipping details in your narrative maybe KOF has a secret or your narrative is not explained very well for any reader to comprehend the problem.

"KOF ; PIRO ; 3" means: in every KOF there are 3 PIRO's. (Apart from all the rest that KOF may contain).
So if you see it like that, I think the narrative is coherent?
Thanks for looking into my problem!

@bartvana 

In your sample COMP3 has two different parents - PIRO and PRIM. Is that misprint ?

so then PRIM should be 4?
No, it's not a misprint. Maybe the term Parent/Child is misleading here. You can see it as follows:
PIRO is an electronic device that contains, among other parts, COMP3 (let's say COMP3 is a meter of wire, so 0.5 qty of the item COMP3 is half a meter of wire).
PIRM is another electronic device that also contains, among other parts, COMP3 (in the case of PIRM 10 meters of wire).
One COMP3, in turn, contains 100 COMP3.1, et cetera.
(The data is fictitious so don't go too far in the details of this example, but that's how the numbers should be understood).
Does that make sense?

@bartvana 

 

You have to normalize your components first and establish a lowest unit of measure for each component, then you can group like components that go to like devices etc etc.

I'm sorry, I don't quite follow, can you give an example please?

@bartvana 

 

Let's make coffee an example.

 

a cup of coffee has several components:

coffee

creamer

sweetener

 

the lowest unit of measure when you're making coffee is how many scoops to make 1 cup of coffee do you need to put in the coffee maker maybe 1 or 2 scoops of grounds to make 4 cups

usually creamer is two teaspoons variance of +/- 1

and usually 2 packets of sweetener variance of +/- 1

 

so for each device what is the usual components and their usual lowest unit of measure

so if you are making 10 devices you would be using x number of like components example screws how many screws do you need to assemble a pc etc..

the reason for lowest unit of measure is for easy costing

 

when you buy corned beef for example each can is a fixed ounces and each case is say 24 cans etc.

 

so if you think about it all products in the market are packaged in like unit of measure.

can you imagine selling a case of soft drink 3 two litter bottles 4 8 ounces, 10 12 ounces etc

how do you cost that?

@bartvana 

I see, thank you. I thought about building hierarchy to summaries by its branches, but here that won't work. Is maximum number of levels known or it differ from case to case?

No, maximum number of levels is not known in advance.

@Yea_So @Sergei Baklan Maybe the following example is easier to understand (see also attached workbook):

bartvana_0-1631987435144.png

So I have as sellable products a ToyCarSet1, a ToyCarSet2, the ToyCars individually, and Wheels which I sell as spare parts (see Table "Sales" bottom left). And I have the sales qties for these.

Above this, I have the Bills of Materials (in multiple levels). So a ToyCarSet1 contains 10 ToyCars, a ToyCarSet2 contains 2 Toy Cars and 1 spare Tyre (which is a subpart of a Wheel). In turn, a ToyCar contains 1 Top, 1 Bottom, 2 Bumpers, 4 Wheels etc. In turn, a Wheel contains 1 CenterPlate and 1 Tyre, which in turn contains 1 OuterTyre and 1 InnerTyre.

 

So for the end result, eg. for the OuterTyre, I need to multiply the number of OuterTyres in a Tyre with the number of Tyres in a Wheel, with the total number of wheels used (namely 23 sold separately as spare parts, plus 4 times 1000 as part of individually sold Toy Cars, plus 4 times 2 times 200 as parts of Toy Cars sold in ToyCarSet2, etc.).

 

(Edited to correct a calculation mistake, screenshot and workbook modified)

@bartvana 

Thank you. The idea is clear. It's not clear how to solve, at least for me.

@Sergei Baklan @Yea_So I got somewhere using a chain of identical queries, one referring to the other.

Basically I merge the query with itself, looking up the subparts of the subparts. I then multiply the quantities of the lowest level with the in-between-level, remove the in-between-level, rename, and end up with a new table which has the same format as the original one, but with the data one level flattened.

 

This is the first loop:

let
    Source = Excel.CurrentWorkbook(){[Name="tBOM"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Part", type text}, {"Subpart", type text}, {"# Subparts in Part", Int64.Type}}),
    // Merge with self and expand grandchildren level
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Subpart"}, #"Changed Type", {"Part"}, "Changed Type", JoinKind.LeftOuter),
    #"Expanded Changed Type" = Table.ExpandTableColumn(#"Merged Queries", "Changed Type", {"Subpart", "# Subparts in Part"}, {"Subpart.1", "# Subparts in Part.1"}),
    //Multiply qties of children with qties of grandchildren to obtain total qties of grandchildren in original parent
    #"Added Custom" = Table.AddColumn(#"Expanded Changed Type", "Total # Subparts", each [#"# Subparts in Part"] * [#"# Subparts in Part.1"]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Total # Subparts", Int64.Type}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"# Subparts in Part.1"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Total # Subparts", "# Subparts in Part.1"}}),
    //If grandchild is null, then take child and child qty instead
    #"Added Custom1" = Table.AddColumn(#"Renamed Columns", "SubPart.1 Merged", each if [Subpart.1] <> null then [Subpart.1] else [Subpart]),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "# Subparts in Part.1 Merged", each if [#"# Subparts in Part.1"] <> null then [#"# Subparts in Part.1"] else [#"# Subparts in Part"]),
    #"Changed Type2" = Table.TransformColumnTypes(#"Added Custom2",{{"# Subparts in Part.1 Merged", Int64.Type}}),
    //Make the table exactly as before this loop (but with one level flattened of course)
    #"Removed Columns1" = Table.RemoveColumns(#"Changed Type2",{"Subpart", "# Subparts in Part", "Subpart.1", "# Subparts in Part.1"}),
    #"Renamed Columns1" = Table.RenameColumns(#"Removed Columns1",{{"SubPart.1 Merged", "Subpart"}, {"# Subparts in Part.1 Merged", "# Subparts in Part"}})
in
    #"Renamed Columns1"

The second loop then refers to this query as source, and does exactly the same, etc.

 

There are still some issues though.

One is that I don't know when to stop the chain, or how to make it loop until a certain condition is reached.

The other is that the data I end up with is correct, but incomplete.

My result:

bartvana_0-1631987904943.png

The desired result:

bartvana_1-1631987933142.png

So I end up with all the products that have no children, in the correct quantities, but all the upstream products are missing.

 

Any help, even just high-level in terms of a strategy that would work or a point that I'm missing, would be greatly appreciated!

@bartvana 

 

Like I mentioned before, you have to normalize the lowest unit of measure for each part in order to accurately cost them for example:

If you ordered tires for the toy car, when you order toy car tires, how much is in a box of tires, and how much does a box cost you.  So if the box of toy car tires costs 100 per box contains 100 tires per box you divide it cost / qty = 1.00 for each tire x 4 for each toy car that's 4.00 per toy car x number of toy car you're going to manufacture = 400.00 per 100 toy cars

 

so you're data model lacks that table the costing table for each part of one product.

It might be me but I still don't follow, I'm not trying to calculate the cost, I'm trying to calculate quantities only. So in this case the lowest unit of measure would be, for each product, 1. (1 unit).