Flatten a Multi-Level Bill of Materials in Excel Power Query
Question details
Flatten a multi-level bill of materials so each parent is linked to its children and deeper descendants.

- Product
- Microsoft Excel Power Query
- Device & OS
- not provided
- Scenario
- A bill of materials lists parent-child relationships, and some child parts are also parents of other parts.
- Observed behavior
- The desired result is a flat list of descendants with their relationship depth and path.
Arrange the source data in two columns named Parent and Child, with one relationship per row. Make sure part identifiers are consistent and check for blank entries or circular relationships.
Use a recursive Power Query to expand all BOM levels
Use this method to list each parent’s descendants, including their levels and paths.
The query treats every part that appears in the Parent column as a starting parent. It follows each child until no further relationship is found and skips a link if it would repeat a part already in the current path.
The result preserves separate paths to the same descendant but does not calculate quantities or identify skipped circular links.
Select the Parent and Child data in Excel, choose Insert > Table, and confirm that the table has headers. On the Table Design tab, set the table name to BOM.
Go to Data > Get Data > From Other Sources > Blank Query. In the Power Query Editor, choose Home > Advanced Editor.
Replace the existing code in Advanced Editor with the following, then select Done: let Source = Excel.CurrentWorkbook(){[Name="BOM"]}[Content], Columns = Table.SelectColumns(Source, {"Parent", "Child"}), TextParts = Table.TransformColumnTypes(Columns, {{"Parent", type text}, {"Child", type text}}), Clean = Table.Buffer(Table.Distinct(Table.SelectRows(TextParts, each [Parent] <> null and [Parent] <> "" and [Child] <> null and [Child] <> ""))), Roots = List.Distinct(Clean[Parent]), Walk = (root as text, node as text, path as list, level as number) as list => let children = Table.SelectRows(Clean, each [Parent] = node)[Child], rows = List.Combine(List.Transform(children, (child) => if List.Contains(path, child) then {} else let nextPath = path & {child}, row = [Parent = root, Descendant = child, Level = level, Path = Text.Combine(nextPath, " > ")] in {row} & @Walk(root, child, nextPath, level + 1) )) in rows, Records = List.Combine(List.Transform(Roots, (root) => Walk(root, root, {root}, 1))), Result = Table.FromRecords(Records, type table [Parent = text, Descendant = text, Level = number, Path = text]) in Result
Review the Parent, Descendant, Level, and Path columns, then choose Home > Close & Load. After changing the BOM table, use Data > Refresh All to update the result.

Review flattened BOM tables in WPS Spreadsheets
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and Excel format compatibility, making it easy to move and review BOM workbooks. Use Excel Power Query for the recursive transformation described above.

Frequently Asked Questions
What does the Level column mean?
Level 1 is a direct child of the starting parent. Level 2 is a child of that child, and each subsequent level represents another step down the path.
Why does the same descendant appear more than once?
A part can be reached through different branches. Check the Path column to see the distinct routes from the parent to that descendant.
How do I update the flattened BOM after adding parts?
Add the relationships to the Excel table named BOM, then select Data > Refresh All to rerun the query.
Does the query calculate component quantities?
No. It lists relationships and paths only. Quantity rollups require additional logic to multiply quantities along each path and aggregate the results.
How does the query handle circular references?
If a child already appears in the current path, the query skips that link to prevent endless recursion. It does not flag the skipped link in the output.




