PATH functions in DAX: parent-child hierarchies

How the DAX parent and child functions flatten a parent-child hierarchy such as an org chart or chart of accounts, PATH, PATHITEM, PATHITEMREVERSE, PATHLENGTH and PATHCONTAINS, level columns for a matrix hierarchy, and row-level security for managers.

Short answer: a parent-child table has one row per item and a column with the parent's ID: employees and their managers, accounts and their parent accounts. PATH ( id, parent_id ) returns the chain from the top down as text ("1|4|9"). From it, PATHLENGTH gives the depth, PATHITEM ( path, n ) the ID at level n, PATHITEMREVERSE counts from the bottom, and PATHCONTAINS ( path, id ) tests whether an ID is in the chain. Use them to build level columns (Level 1, Level 2, Level 3) for a matrix hierarchy, and for manager security.

Power BI hierarchies need one column per level. Org charts and account trees come as parent-child pairs with any depth. The PATH functions turn one into the other.

What do the functions return?

Employee IDNameManager IDPATHPATHLENGTHLevel 1Level 2Level 3
1Sara (CEO)11SaraSaraSara
4Omar11|42SaraOmarOmar
9Lena41|4|93SaraOmarLena
7Tom11|72SaraTomTom
An employee table with PATH columns
FunctionReturnsExample
PATH ( id, parent )the chain of IDs as text, top firstPATH ( Employee[Employee ID], Employee[Manager ID] )
PATHLENGTH ( path )the number of itemsPATHLENGTH ( Employee[Path] )
PATHITEM ( path, n [, type] )the item at position n from the topPATHITEM ( Employee[Path], 2, INTEGER )
PATHITEMREVERSE ( path, n [, type] )the item at position n from the bottomPATHITEMREVERSE ( Employee[Path], 1 )
PATHCONTAINS ( path, item )TRUE if the item is in the pathPATHCONTAINS ( Employee[Path], 4 )
The five functions

How do you build level columns?

-- calculated columns on Employee
Path = PATH ( Employee[Employee ID], Employee[Manager ID] )
Depth = PATHLENGTH ( Employee[Path] )

Level 1 =
LOOKUPVALUE ( Employee[Name], Employee[Employee ID], PATHITEM ( Employee[Path], 1, INTEGER ) )

Level 2 =
VAR Id = PATHITEM ( Employee[Path], 2, INTEGER )
RETURN IF ( ISBLANK ( Id ), Employee[Level 1], LOOKUPVALUE ( Employee[Name], Employee[Employee ID], Id ) )

Level 3 =
VAR Id = PATHITEM ( Employee[Path], 3, INTEGER )
RETURN IF ( ISBLANK ( Id ), Employee[Level 2], LOOKUPVALUE ( Employee[Name], Employee[Employee ID], Id ) )

Filling empty levels with the parent's name avoids blank rows in the matrix. Create a hierarchy Level 1, Level 2, Level 3 and put it on the rows.

To hide the repeated names when someone drills below their own level, blank the measure where the row is deeper than the person:

Revenue (hierarchy) =
VAR RowDepth = ISINSCOPE ( Employee[Level 1] ) + ISINSCOPE ( Employee[Level 2] ) + ISINSCOPE ( Employee[Level 3] )
VAR MaxDepth = MAX ( Employee[Depth] )
RETURN IF ( RowDepth <= MaxDepth, [Revenue] )

How are they used for security?

A role filter that lets a manager see everyone under them:

PATHCONTAINS (
    Employee[Path],
    LOOKUPVALUE ( Employee[Employee ID], Employee[Email], USERPRINCIPALNAME ( ) )
)

Details in dynamic row-level security.

What goes wrong?

  • Circular parents. An item that is its own ancestor makes PATH fail; fix the data.
  • Missing parents. A parent ID that does not exist in the table errors; clean or point it to a root.
  • Text paths. PATHITEM returns text by default; pass INTEGER to compare with numeric IDs.
  • Calculated columns only. PATH works on the whole table at refresh; it is not a measure function.
  • Very deep trees. One column per level; decide a maximum depth.

Where can you check this?

Where do you start?

Add Path, Depth and three Level columns to your employee or account table and build the hierarchy. ISINSCOPE explains the depth test.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.