Create a Multilevel Bill of Materials in Excel Power Query
Question details
Create a refreshable multilevel bill of materials from single-level parent-child data using Power Query.

- Product
- Microsoft Excel Power Query
- Device & OS
- not provided
- Scenario
- An ERP source provides parent, component, and quantity relationships that need to be expanded into component levels for analysis.
- Observed behavior
- The desired output lists each assembly's components at every level, with paths and quantities suitable for pivot tables and cost calculations.
Prepare a single-level Excel table named BOM with columns named Parent, Component, and Quantity. Use consistent text identifiers for parts and numeric quantities per parent; check for blank values and circular relationships before expanding the data.
Expand the BOM with a recursive Power Query function
Use this approach to generate component paths and extended quantities for every parent in the source table.
The function follows each component to its children and multiplies quantities along the path. Each source parent is treated as a possible top-level assembly, so filter TopLevelParent if you only need particular assemblies.
The query stops following a path when a component would repeat. This prevents infinite recursion but also omits the cycle-closing relationship; correct circular references in the source data.
Select the parent-child data in Excel, choose Insert > Table, and name the table BOM on the Table Design tab. Make sure the column headers are Parent, Component, and Quantity.
Choose Data > Get Data > From Other Sources > Blank Query, then open Home > Advanced Editor.
Replace the Advanced Editor contents with the following M code and select Done: let Raw = Excel.CurrentWorkbook(){[Name="BOM"]}[Content], Typed = Table.TransformColumnTypes(Raw, {{"Parent", type text}, {"Component", type text}, {"Quantity", type number}}), Source = Table.Buffer(Table.SelectRows(Typed, each [Parent] <> null and [Parent] <> "" and [Component] <> null and [Component] <> "" and [Quantity] <> null)), Walk = (top as text, current as text, component as text, level as number, qty as number, extended as number, visited as list) as list => let Row = [TopLevelParent = top, CurrentParent = current, Component = component, Level = level, Quantity = qty, ExtendedQuantity = extended, Path = Text.Combine(visited, " > ")], Children = Table.SelectRows(Source, each [Parent] = component and not List.Contains(visited, [Component])), Descendants = List.Combine(List.Transform(Table.ToRecords(Children), (child as record) => @Walk(top, component, child[Component], level + 1, child[Quantity], extended * child[Quantity], visited & {child[Component]}))) in {Row} & Descendants, Roots = Table.SelectRows(Source, each [Parent] <> [Component]), Expanded = List.Combine(List.Transform(Table.ToRecords(Roots), (r as record) => Walk(r[Parent], r[Parent], r[Component], 1, r[Quantity], r[Quantity], {r[Parent], r[Component]}))), Result = Table.FromRecords(Expanded, {"TopLevelParent", "CurrentParent", "Component", "Level", "Quantity", "ExtendedQuantity", "Path"}, MissingField.UseNull) in Result
Check the preview, set appropriate column types, and choose Home > Close & Load To to load the result to a worksheet or, where available, the Data Model. Use Insert > PivotTable to summarize ExtendedQuantity by assembly and component.
Update the BOM table from the ERP source, then choose Data > Refresh All. If another query populates the BOM table, verify that its refresh completes before refreshing the expansion.

Review BOM Workbooks with WPS Spreadsheets
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface for opening and reviewing BOM workbooks. Its Excel format compatibility helps you move existing files into WPS, although it does not replace Excel's Power Query recursion.

Frequently Asked Questions
How do I show only one finished assembly?
In the Power Query preview, open the TopLevelParent filter and select the assembly you need. The query initially treats every source parent as a possible top-level assembly.
What is the difference between Quantity and ExtendedQuantity?
Quantity is the component quantity for its immediate parent. ExtendedQuantity multiplies quantities along the path, giving the amount needed per one unit of the top-level parent.
How can I refresh the BOM when ERP data changes?
Update or refresh the Excel BOM source table first, then select Data > Refresh All. If a separate query populates that table, check that it finishes before the recursive query refreshes.
Can I use the expanded BOM for a cost rollup?
Yes. Match components to unit costs and multiply each relevant ExtendedQuantity by its unit cost. Decide whether to cost purchased components, subassemblies, or both to avoid counting the same underlying costs twice.
What happens if the BOM contains a circular reference?
The sample query stops a path before a part repeats, preventing infinite recursion. It does not report the cycle, so inspect and correct circular references in the source table.




