Power BI How-To / Advanced Modeling

Power BI Parent-Child Hierarchy: Flattening Recursive Tables with PATH and Power Query

A power bi parent child hierarchy starts as one self-referencing table, such as employees and their managers or accounts and their rollup accounts, and it has to be flattened into level columns before a matrix can drill it. This article covers that recursive flattening step only: PATH in DAX, self-merges in Power Query, ragged levels, chart-of-accounts trees and the totals that break. Mapping accounts to statement lines is a separate job, covered in our guide to building a Power BI profit and loss statement.

What is a parent-child hierarchy in Power BI?

A parent-child hierarchy is a single table where each row stores its own ID and the ID of its parent: an employee and their manager, an account and its rollup account, a cost center and its division. Power BI visuals cannot drill a self-referencing table directly, so the chain must first be flattened into fixed level columns.

The source shape is often called an adjacency list. It is compact and easy for an HR or ERP system to maintain, because moving a branch means changing one parent ID. It is also why the table is awkward for reporting: Power BI hierarchies are built from columns, one column per level, and a relationship can’t point a table back at itself. The table below shows a small illustrative org chart before and after flattening.

Illustrative sample rows: a parent-child table and the columns added to flatten it.
Name ID Parent ID Path Depth Level 1 Level 2 Level 3 Level 4
CEO1(blank)11CEO———
VP Finance211|22CEOVP Finance——
VP Sales311|32CEOVP Sales——
Controller421|2|43CEOVP FinanceController—
AP Specialist541|2|4|54CEOVP FinanceControllerAP Specialist

The flattened table is still a dimension. Facts relate to it on the node ID, one-to-many, and the level columns sit on the dimension where a hierarchy can be built from them. If that fact-and-dimension shape is new, our Power BI star schema guide covers the relationship rules this table plugs into.

How do you flatten a hierarchy with PATH in DAX?

Add a calculated column with PATH(ID, ParentID) to get a pipe-delimited chain from the top node down to the current row. Then add one column per level with PATHITEM, look up each level’s name with LOOKUPVALUE, and build a Power BI hierarchy from those level columns. PATHLENGTH gives each row’s depth.

DAX has five parent and child functions (opens in new tab): PATH, PATHITEM, PATHITEMREVERSE, PATHLENGTH and PATHCONTAINS. A typical build on an Employee table takes four steps:

  1. Path: Path = PATH(Employee[EmployeeID], Employee[ManagerID]) returns values such as 1|2|4, oldest ancestor first.
  2. Depth: Depth = PATHLENGTH(Employee[Path]) counts the nodes in the path, including the row itself.
  3. Level columns: Level 2 = LOOKUPVALUE(Employee[Name], Employee[EmployeeID], PATHITEM(Employee[Path], 2, INTEGER)), repeated for each level. The INTEGER argument matters when the IDs are numbers, because PATHITEM returns text by default.
  4. Hierarchy: build Level 1 > Level 2 > Level 3 in the model, then hide the ID, Path and individual level columns so report authors only see the hierarchy.

Microsoft’s PATH function reference (opens in new tab) lists the rules the table must follow: the ID and parent columns must share a data type (text or integer), every parent value must exist as an ID, and an ID can’t contain the pipe character. It also notes that PATH isn’t supported in DirectQuery calculated columns or row-level security rules, which pushes DirectQuery models toward flattening upstream.

How do you flatten it in Power Query instead?

Power Query has no built-in recursive hierarchy function, so the usual approach merges the table to itself once per level: join ParentID to ID, expand the parent’s name and ParentID, and repeat until the deepest level is blank. The result loads as plain columns, with no calculated columns in the model.

Self-merging walks upward from each row, so the first merge returns the parent, the second the grandparent, and so on. The columns come out bottom-up and need to be reordered so Level 1 is always the top node; otherwise a deep branch and a shallow branch put different people in the same column. Some teams write a recursive custom function in M instead of fixed merges, which handles any depth but is harder to maintain and slower on large tables. When the data already sits in SQL Server or a Fabric warehouse, a view with a recursive common table expression usually does the job most cleanly, and it works for DirectQuery models too.

Three ways to flatten a parent-child table for Power BI, and the trade-offs of each.
Method Where it runs Depth handling DirectQuery Watch out for
DAX PATH and PATHITEM Calculated columns, after the table loads Path handles any depth; level columns are written by hand Not supported in calculated columns or RLS rules Errors on orphans or duplicate IDs with different parents; extra columns in the model
Power Query self-merge During refresh, before the table loads One merge per level, up to a fixed maximum Only if every step folds to the source Levels come out bottom-up and must be reordered; slow on large tables
SQL recursive view or warehouse table In the source database, before Power BI The recursive query walks any depth Yes Needs database access, and the view must be maintained alongside the model

Whichever method you use, the level columns should be identical in meaning: Level 1 is the top node for every row, and Depth is the row’s own level. Mixing conventions between reports is a common reason two dashboards show different subtotals for the same branch.

How do you handle ragged levels?

A ragged hierarchy has branches of different depths, so short branches leave blank level columns and the matrix shows empty rows beneath them. Either fill each blank level with the node’s own name, or keep the blanks and add a measure that returns BLANK when the row is deeper than the node.

In the sample table, VP Sales has no Level 3 or Level 4, so expanding the matrix under VP Sales shows a “(Blank)” row. The robust fix combines both ideas. First fill the gap, for example Level 3 = IF(Employee[Depth] < 3, Employee[Name], LOOKUPVALUE(…)), so the blank row becomes a repeat of the node’s name. Then hide those repeats with a measure that compares how deep the matrix is browsing with how deep the node really is:

  • Browse depth: ISINSCOPE(Employee[Level 1]) + ISINSCOPE(Employee[Level 2]) + ISINSCOPE(Employee[Level 3]), one term per level.
  • Node depth: MAX(Employee[Depth]) for the rows in the current cell.
  • Result: return BLANK() when browse depth is greater than node depth, otherwise the normal measure. A row where every measure is blank disappears from the matrix.

Use the wrapped measure in every visual that shows the hierarchy, and keep the base measure for cards and charts that never drill. Our data modeling and DAX service can review hierarchy measures that need the same treatment across many reports.

How does a chart of accounts hierarchy differ?

A chart of accounts is usually a parent-child table where only leaf accounts carry postings, and parent accounts exist to sum them. Flatten it the same way, but check whether any parent account also holds its own postings, decide how contra and sign-flipped accounts roll up, and keep statement line mapping as a separate step.

Postings on a parent account are the trap. The matrix subtotal for that parent includes its own amount, but with the ragged-level measure above there is no row that shows it, so the children no longer add up to the parent and users assume the numbers are wrong. The clean fix is upstream: give each posting parent a synthetic child, such as “Operating expenses (direct)”, and re-point its postings there so every amount lives on a leaf.

The account tree tells you how accounts roll up inside the ledger. It does not tell you which line of the income statement or balance sheet an account lands on, and it often doesn’t carry the sign conventions a statement needs. Keep that mapping in its own table, as the profit and loss statement guide explains. Groups with several entities add another layer, because each ERP may have a different tree; map each entity’s accounts to one group chart before flattening, which is the core of a multi-entity consolidation dashboard.

Why do hierarchy totals quietly break?

Most broken totals come from the source table, not DAX. A child listed under two parents makes PATH fail or double counts through a bridge, an orphan whose parent ID is missing breaks the path, a loop never reaches the top, and mixed text and integer keys stop the lookup. Validate the table before every refresh.

  • Orphans: a parent ID that isn’t present as an ID, often a terminated manager or a deleted account. Add a check query that lists parent IDs with no matching row.
  • Multiple parents: the same ID appears twice with different parents. PATH returns an error; if the business really has shared members, model them with a bridge table and decide deliberately how the total treats them.
  • Loops: a node that ends up as its own ancestor after a bad edit. There is no top node, so the path can’t be built.
  • Facts on the wrong key: the fact table stores a name or a code from a different system instead of the node ID, so some rows fall into the blank member.
  • Hierarchy changes over time: a reorganization moves a branch, and last year’s subtotals move with it. If history must stay where it was, the hierarchy table needs versioning, as described in our guide to the Power BI slowly changing dimension.
  • Depth growth: the source gains a level the model has no column for, and the new nodes fold silently into their ancestors.

Getting help with a hierarchy model

Flattening a clean single-parent table is usually a well-bounded task. Untangling orphans, shared members or a chart of accounts with posting parents takes longer, because the fixes belong in the source or a warehouse view. Easy Power BI Dashboards works from prepaid Flex hours, from $100/hour, with blocks of Starter at $2,500, Growth at $4,600 and Power at $8,000, and the hours never expire. You describe the hierarchy and the reports that depend on it, we come back with a scope and an hour estimate, and work starts only after you approve it.

FAQ

Frequently asked questions

Not with PATH. The DAX parent-child functions expect each ID to have exactly one parent, and PATH returns an error when the same ID appears with different parents. Shared members need a bridge table that lists every node and ancestor pair, plus measures written so a value reached through two parents is not counted twice in the total.

Create one more level column than the deepest branch you have today, so a new layer of management or a new rollup account still gets its own row in the matrix. Check the maximum PATHLENGTH after each refresh: if it exceeds your level count, the deepest nodes fold into their ancestors until you add a column.

Not in calculated columns or row-level security rules, according to Microsoft’s PATH reference. For a DirectQuery or composite model, flatten the hierarchy in the source instead: a SQL view with a recursive common table expression, or a warehouse table, can return the same level columns that PATHITEM would have produced in an import model.

Yes, in an import model. A common pattern stores each employee’s PATH in a column, then the role filter uses PATHCONTAINS to keep rows where the signed-in user’s employee ID appears anywhere in that path, so managers see their whole reporting line. Test it with the View as role feature before publishing.

Try us risk-free for 4 hours

If you’re unhappy within the first 4 hours of your block, we’ll refund you in full — no questions asked. Hours are deducted only for work you’ve reviewed and approved, and they never expire.

Subtotals that don’t match their rows?

That is usually an orphan, a posting parent or a ragged level showing through. Bring the model to a free demo and we’ll show you where the hierarchy breaks and what it takes to fix it.

Scope approved before any hours are used · Hours never expire

Book a free demo

See what your Power BI reporting would take

Tell us the task. We reply within one business day, and you get a scope estimate and the hours needed before any work begins.

Step 1 of 2: What do you need?

How can we help? Pick one

Tap to add, or type your own:

Step 2 of 2: Where should we reply?

Used only to contact you about this inquiry.

We reply within one business day. Scope approved before any hours are used. We don’t sell your information.

Tell us the task. We scope it, you approve, and a senior consultant delivers — typically in 3–5 business days. Prepaid hours that never expire.

  • Prepaid hours from $100/hour — and they never expire
  • Senior-led delivery — 20+ years on the Microsoft platform
  • Typical 3–5 business day turnaround
  • Full refund if you’re unhappy in your first 4 hours
Book my free demo