Forum Discussion

bartvana's avatar
bartvana
Iron Contributor
Sep 13, 2021

Parent-child flattening (multiple levels)

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...
  • SergeiBaklan's avatar
    SergeiBaklan
    Sep 19, 2021

    bartvana 

    I didn't dive a deep into calculations, here is only suggestion for iterations.

    We keep as it is now tBOM and tCalc1.

    Levels are iterated by fntCalcQty:

    ( tCalc as table, tBasis as table, tAccumulate as table ) =>
    
    let
        nextLevel = fnCalc( tCalc, tBasis ),
        addTable = Table.Combine( {tAccumulate, nextLevel} )
    
    in
        if  Table.RowCount( nextLevel ) = 0
        then tAccumulate
        else fntCalcQty (nextLevel, tBasis, addTable)

    It iterates till first empty table appears.

    Final result is calculated as

    let
        Source = fntCalcQty( tCalc1, tBOM, tCalc1),
        addSales = Table.Combine( {Source, tSalesRenamed} ), 
        #"Grouped Rows" = Table.Group(
            addSales, {"Subpart"},
            {{"TotalQty", each List.Sum([TotalQty]), type nullable number}}
        ),
        #"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"Subpart", Order.Ascending}})
    in
        #"Sorted Rows"

    Above is in Variant 03 group in attached.