Sub topic 10
Moving between hierarchy levels with COLLAPSE and EXPAND
This chapter assumes you can add a visual calculation to a matrix, know that a visual calculation runs on the visual's own rows and columns, and have used the DIVIDE function. Here you learn how to read a value from a different level of the visual's hierarchy.
What a visual hierarchy is
When you place several fields on the rows of a matrix, such as Year, Quarter, and Month, the order of those fields defines a hierarchy. Year is the top level and Month is the lowest. The hierarchy comes from the visual, not from the data model, so it changes when you reorder or remove fields in the visual.
Visual calculations normally evaluate on the current row. The hierarchy functions let a calculation look at the same measure on a different level.
| Function | Direction | Result |
|---|---|---|
| `COLLAPSE` | Up | Value one level higher (or more, if you ask for it) |
| `COLLAPSEALL` | Up | Value at the top level |
| `EXPAND` | Down | Value one level lower |
| `EXPANDALL` | Down | Value at the lowest level |
The syntax
The first argument is the expression to evaluate, such as a measure. The second is the axis, which names the direction of the hierarchy. ROWS uses the fields on the rows of the visual, and COLUMNS uses the fields on its columns. Both functions also accept an optional argument for how many levels to move.
Percent of parent = DIVIDE([Sales Amount], COLLAPSE([Sales Amount], ROWS))In this calculation, each Month row divides its sales by the value for its Quarter. Each Quarter row divides by the value for its Year. The same formula works at every level, because COLLAPSE moves one level up from wherever the current row sits.
To compare against the top of the hierarchy, use COLLAPSEALL:
Percent of grand total = DIVIDE([Sales Amount], COLLAPSEALL([Sales Amount], ROWS))Here every row, whatever its level, is divided by the value at the top of the row hierarchy.
Worked example
Suppose a matrix has Year and Quarter on rows and Sales Amount as the value. Year 2025 totals 1,000 and its Q1 totals 250.
- On the Q1 row,
COLLAPSE([Sales Amount], ROWS)returns 1,000, so Percent of parent is 25%. - On the Q1 row,
COLLAPSEALL([Sales Amount], ROWS)also returns 1,000 here, because Year is the top level. Percent of grand total is therefore the same as Percent of parent. - If you add Month as a third level, the Month rows give different results for the two functions.
COLLAPSEreturns the Quarter value, whileCOLLAPSEALLreturns the Year value.
Set the data type and format of the result to a percentage so that 0.25 appears as 25%.
Common errors
- Collapsing past the top. The top level has no parent, so check what your calculation shows on the Year and total rows. Do not assume it shows 100%.
- Assuming the model defines the hierarchy. If you remove Quarter from the rows, the parent of Month becomes Year, and the numbers change.
- Wrong axis. If your fields are on columns,
ROWShas no hierarchy to move through. UseCOLUMNSinstead. - Reaching for EXPAND to get parent values.
EXPANDgoes down, so it suits calculations on higher rows that need the detail below them. Parent shares needCOLLAPSE.
Exercise
A matrix has Region, Country, and City on rows. Which visual calculation shows each row's share of its Region's total, and how does it differ from share of the grand total?
Show answer
Use DIVIDE([Sales Amount], COLLAPSEALL([Sales Amount], ROWS)) for share of the top level. For a share of the Region level, collapsing to the top is the same as using Region, so COLLAPSEALL gives the Region value on the Region row only. On City rows, COLLAPSE moves up just one level to Country, and COLLAPSEALL goes up to Region. The share-of-Region calculation therefore uses COLLAPSEALL when Region is the top level, and COLLAPSE gives share of Country.