Forum Discussion

Data4Beer's avatar
Data4Beer
Frequent Visitor
8 years ago
Solved

Help me manipulate this data

Hi everyone!   I am struggling to solve this problem. I have bill of materials which contains anInventoryID's and the materials.   In this example, there are 4 Work in Progress phases (inventory ...
  • MarcelBeug's avatar
    MarcelBeug
    8 years ago

    In the query below, recursive function ExplodeBOM is used as part of a solution that is independent from the sort order of the original table.

     

    As a prerequisite, all materials must have the same case for their codes, e.g. w1br001 is not the same as W1BR001.

     

    This also allows for subassemblies to appear in multiple finished goods.

     

    In each iteration, a new BOM level is added to the resulting table.

     

    let
        Source = Table1,
        SelectedFinishedGoods = Table.NestedJoin(Source,{"InventoryID"},Table1,{"Material Added"},"Table1",JoinKind.LeftAnti),
        RemovedJoinColumn = Table.RemoveColumns(SelectedFinishedGoods,{"Table1"}),
        AddedFinishedInventoryID = Table.Buffer(Table.DuplicateColumn(RemovedJoinColumn, "InventoryID", "Finished InventoryID")),
    
        ExplodeBOM = (TableSoFar as table, PreviousTable as table) as table =>
        let
            SelectedRemainingRecords = Table.NestedJoin(PreviousTable,{"InventoryID"},TableSoFar,{"InventoryID"},"JoinColumn",JoinKind.LeftAnti),
            RemainingRecords = Table.RemoveColumns(SelectedRemainingRecords,{"JoinColumn"}),
            SelectedNewRecords = Table.NestedJoin(RemainingRecords,{"InventoryID"},TableSoFar,{"Material Added"},"RemainingRecords",JoinKind.Inner),
            NewRecords = Table.ExpandTableColumn(SelectedNewRecords, "RemainingRecords", {"Finished InventoryID"}),
            NewTable = Table.Buffer(TableSoFar & NewRecords),
            Result = if Table.IsEmpty(SelectedRemainingRecords) then TableSoFar else @ExplodeBOM(NewTable, RemainingRecords)
        in
            Result,
    
        ExplodedBOM = ExplodeBOM(AddedFinishedInventoryID,Source),
        Sorted = Table.Sort(ExplodedBOM,{{"Finished InventoryID", Order.Ascending}, {"Material Added", Order.Ascending}}),
        Reordered = Table.ReorderColumns(Sorted,{"Finished InventoryID", "InventoryID", "Material Added"}) 
    
    in
        Reordered

     

  • MarcelBeug's avatar
    MarcelBeug
    8 years ago

    Troubleshooting your issues takes me a multitude of time that was required to come up with a solution in the first place.

     

    Again, your ExplodedBOM step is wrong.

     

    It should be:

     

        ExplodedBOM = ExplodeBOM(AddedFinishedInventoryID,#"Removed Duplicates"),

     

    My suggestion would be not to use any solution you don't understand.