Sub topic 10
Moving between hierarchy levels with COLLAPSE and EXPAND
This chapter assumes you can build a matrix in Power BI Desktop and write a visual calculation from the earlier chapters. You should know what the axis parameter does and be comfortable with DAX measures and DIVIDE. Here you will learn four functions that read a value from a different level of the visual's row or column hierarchy: COLLAPSE, COLLAPSEALL, EXPAND, and EXPANDALL.
Why a visual needs level-aware functions
A matrix with Year, Quarter, and Month on its rows shows one value per row. Each Month row has a Quarter above it, and each Quarter row has a Year above it. A normal visual calculation sees only the current row. To answer "what share of its quarter is this month?", the calculation needs the quarter's value while it is evaluating the month's row.
COLLAPSE and its relatives provide that. They evaluate an expression at another level of the hierarchy and return the result to the current row.
In a visual calculation, a hierarchy means the order of fields on an axis. The fields can come from a model hierarchy, or you can simply drop Year, Quarter, and Month onto Rows in that order. The model does not need to define the hierarchy.
The four functions
| Function | Direction | Where it evaluates the expression |
|---|---|---|
| COLLAPSE | Up | A number of levels above the current row. The default is one level. |
| COLLAPSEALL | Up | The highest level of the axis, which is the grand total. |
| EXPAND | Down | A number of levels below the current row. The default is one level. |
| EXPANDALL | Down | The lowest level of the axis. |
The syntax follows one pattern.
COLLAPSE(<expression>, <axis> [, <levels>])
COLLAPSEALL(<expression>, <axis>)
EXPAND(<expression>, <axis> [, <levels>])
EXPANDALL(<expression>, <axis>)expressionis usually a measure or a field in the visual, such as[Sales Amount].axisnames the axis whose hierarchy you move along:ROWSorCOLUMNS. Some functions also accept both axes together. This is the same parameter described in the axis chapter.levelsis optional for COLLAPSE and EXPAND. It sets how many levels to move.
The ALL versions take no levels argument because they always go to the end of the hierarchy.
Percent of parent
The most common use of COLLAPSE is the share of a value within its parent. Set up a matrix with Year, Quarter, and Month on Rows and Sales Amount on Values. Then add this visual calculation.
Percent of parent = DIVIDE([Sales Amount], COLLAPSE([Sales Amount], ROWS))Here is how it evaluates in each row.
- On a Month row,
COLLAPSE([Sales Amount], ROWS)returns the Sales Amount of the Quarter that contains the month. DIVIDE gives the month's share of that quarter. - On a Quarter row, the same formula returns the quarter's share of its year.
- On a Year row, COLLAPSE has no parent level to move up to. Check the number your visual shows for this row, and don't assume it is meaningful.
DIVIDE returns blank instead of an error when the denominator is zero or blank. That is why you should use it here instead of the / operator, as you did in measures.
Percent of grand total
COLLAPSEALL skips every intermediate level and goes straight to the top.
Percent of grand total = DIVIDE([Sales Amount], COLLAPSEALL([Sales Amount], ROWS))Every row, whether Year, Quarter, or Month, is now divided by the same denominator, the grand total of the visual. Month shares across the whole matrix add up to 100%.
You can get the same result as COLLAPSE by passing a levels value large enough to reach the top. COLLAPSEALL states your intent more clearly and keeps working if someone later adds another field to the rows.
Jumping more than one level
Use the third argument of COLLAPSE when you want a level that is neither the parent nor the top. For a Year > Quarter > Month matrix, the following compares each month with its year, skipping the quarter.
Percent of year = DIVIDE([Sales Amount], COLLAPSE([Sales Amount], ROWS, 2))With three levels this is the same as COLLAPSEALL. With four levels, such as Year > Quarter > Month > Day, it is not. The two-level jump lands on the quarter, so the result is a day's share of its quarter. Counting levels from the current row means the same formula gives different answers on different rows. A Day row moves up two levels to Quarter, and a Month row moves up two levels to Year.
Going down with EXPAND and EXPANDALL
EXPAND and EXPANDALL are the mirror images. They evaluate the expression at a lower level than the current row. This is a harder case, because a parent row has several children and the calculation must return a single value for the parent.
A few points help.
- The expression you pass should produce one meaningful value when it is evaluated at the lower level, or you should combine it with an aggregating function.
- Downward functions are used much less often than COLLAPSE. Most reporting needs, such as shares, ratios to a parent, and variance to a total, look upward.
- If you are unsure what a downward call returns, add it to a table visual next to the plain value and compare rows before building anything on it.
How the visual's layout changes the answer
The functions depend on the fields currently in the visual, so the result changes when the layout changes.
- If you remove Quarter from the rows, the parent of a Month is now the Year. The formula is unchanged, but the denominator is different.
- If the matrix has only one field on Rows, such as Year, the parent and the grand total are the same thing. Both percent calculations then equal the share of the grand total.
- If you put the hierarchy on Columns, you must use
COLUMNSas the axis. UsingROWSthere points the function at a hierarchy that has no levels to move along. - Matrix drill-down and expand-collapse change which rows are shown, not which levels exist. A visual calculation is computed for the levels in the field wells, so the percentages stay the same when you expand or collapse a row.
Common errors
- Wrong axis. Using
ROWSwhen the hierarchy is on columns gives an unexpected result, or an error. Match the axis to where the fields sit. - Hard-coded
levels. As shown above, a fixed number is correct for some rows and wrong for others. - Dividing with
/. A zero or blank parent produces an error value instead of a blank. Use DIVIDE. - Expecting model-wide results. The grand total here is the grand total of the visual, after its filters and slicers. It is not the total of the whole table. If you need a percentage of an unfiltered total, a measure with ALL or ALLSELECTED is the right tool.
- Reading the top row. The Year row, and the Grand total row if shown, have no higher level. Do not treat their percentages as a meaningful share.
Exercise
A matrix has Region, Country, and City on Rows. You add the calculation DIVIDE([Sales Amount], COLLAPSE([Sales Amount], ROWS)). On a City row, what does the result represent? Which formula would give the City's share of all sales in the visual instead? What happens to the second formula if you later add a Store field below City?
Show answer
On a City row, COLLAPSE moves one level up, so the result is the city's share of its country. To get the share of all sales in the visual, use DIVIDE([Sales Amount], COLLAPSEALL([Sales Amount], ROWS)). Because COLLAPSEALL always goes to the highest level, adding Store below City does not change it. The denominator is still the grand total, and each row's value is its share of that total. The first formula would change meaning on City rows, because the parent of a city is still its country. Store rows would then show their share of the city.
Exercise
You use COLLAPSE([Sales Amount], ROWS, 2) in a Year > Quarter > Month matrix. What denominator does a Month row get, and what would you use if you wanted the same year-level denominator in a Year > Quarter > Month > Day matrix, where Day rows are also shown?
Show answer
A Month row moves up two levels, from Month to Quarter to Year, so its denominator is the year's Sales Amount. In the four-level matrix, a Day row moves two levels up and lands on the quarter, not the year. To keep a year-level denominator for every row, use COLLAPSEALL([Sales Amount], ROWS) when Year is the top level, since it always evaluates at the highest level regardless of the row's depth.