Sub topic 11
Percent of parent and percent of grand total
This chapter assumes you can build a matrix in Power BI Desktop, write a basic measure, and use DIVIDE. It also assumes you have created a visual calculation and know that it works on the rows and columns of one visual. The chapter covers two common calculations, share of the parent and share of the grand total. Both rely on the hierarchy functions COLLAPSE and COLLAPSEALL.
Why these calculations are a good fit
A "percent of" calculation compares a value with a larger value in the same visual. Suppose a matrix shows Sales Amount by Category and, under each category, by Product. The questions are usually these:
- What share of its category does each product make up? This is the percent of parent.
- What share of all sales does each product make up? This is the percent of grand total.
In a model measure you would build the denominator with CALCULATE, ALL, or ALLSELECTED, and you would have to choose which columns to remove from the filter context. In a visual calculation you work from the visual's own hierarchy. You tell DAX to move up one level, or all the way to the top, and read the value found there.
The sample visual
The examples use a matrix with these fields:
- Rows: Category, then Product (a two-level hierarchy).
- Values: Sales Amount.
The matrix has three kinds of rows: product rows, category subtotal rows, and a grand total row.
| Row | Level | Sales Amount |
|---|---|---|
| Bikes | Category | 600 |
| Mountain Bike | Product | 400 |
| Road Bike | Product | 200 |
| Clothing | Category | 400 |
| Jersey | Product | 300 |
| Socks | Product | 100 |
| Total | Grand total | 1000 |
The figures are small on purpose so you can check the results by hand.
How COLLAPSE and COLLAPSEALL work
COLLAPSE and COLLAPSEALL evaluate an expression at a higher level of the axis hierarchy. The visual calculation functions take the axis as their second argument. Here the axis is ROWS, the row hierarchy of the matrix.
COLLAPSE(expression, ROWS)evaluates the expression one level up. For a product row, the result is the value of its category. For a category row, the result is the grand total.COLLAPSEALL(expression, ROWS)evaluates the expression at the top of the hierarchy. For every row, the result is the grand total.
The diagram shows where each function reads from.
Percent of parent
Create a visual calculation with this formula:
Percent of parent = DIVIDE([Sales Amount], COLLAPSE([Sales Amount], ROWS))The numerator is the value on the current row. The denominator is the value one level up. DIVIDE returns blank instead of an error when the denominator is zero or blank.
Here are the results for the sample matrix:
| Row | Sales Amount | Percent of parent |
|---|---|---|
| Bikes | 600 | 60% (600 / 1000) |
| Mountain Bike | 400 | 66.7% (400 / 600) |
| Road Bike | 200 | 33.3% (200 / 600) |
| Clothing | 400 | 40% (400 / 1000) |
| Jersey | 300 | 75% (300 / 400) |
| Socks | 100 | 25% (100 / 400) |
| Total | 1000 | 100% |
Two points to notice:
- The parent depends on the row's level. A category's parent is the grand total, so its percent of parent equals its percent of grand total.
- The grand total row has no level above it.
COLLAPSEreturns the grand total itself, so the result is 100%.
Percent of grand total
Replace COLLAPSE with COLLAPSEALL:
Percent of grand total = DIVIDE([Sales Amount], COLLAPSEALL([Sales Amount], ROWS))Now every row divides by 1000.
| Row | Sales Amount | Percent of grand total |
|---|---|---|
| Bikes | 600 | 60% |
| Mountain Bike | 400 | 40% |
| Road Bike | 200 | 20% |
| Clothing | 400 | 40% |
| Jersey | 300 | 30% |
| Socks | 100 | 10% |
| Total | 1000 | 100% |
The product rows under one category add up to that category's share. Mountain Bike (40%) and Road Bike (20%) add up to 60%, which is the Bikes row.
Putting both in one visual
You can add both calculations to the same matrix. Each is a separate visual calculation, and each appears as a new field in the Values well. Add a third calculation if you want to see the difference at a glance:
Share of parent in grand total = [Percent of grand total] - [Percent of parent]This is optional. The reason to include it is that visual calculations can refer to other visual calculations in the same visual, so you can build results in steps instead of writing one long formula.
Format the results
A division returns a decimal such as 0.667. Set the format of the calculation to Percentage so it displays as 66.7%. In the calculation editing experience, use the format options for the calculation and choose a percentage with one decimal place. The formatting is stored with the calculation, so it stays when you change the visual's other fields.
What changes when the visual changes
A visual calculation reads the visual's current layout. This has consequences you should test for.
- Reordering the hierarchy. If you swap the rows so Product is above Category, the parent of a category row is now a product. The formula is the same but the numbers change.
- A single-level rows axis. If the matrix has only Category on rows,
COLLAPSEreturns the grand total for every category, and the result equals theCOLLAPSEALLresult. - Expanded and collapsed state. The calculation uses the data in the visual's structure, not the expand and collapse buttons the viewer clicks. When you expand a row, the new rows get the same calculation.
- Filters. Slicers and filters change which rows exist, so the denominators change too. A product's percent of grand total is its share of the filtered total, not of the whole model. If you need a share of the unfiltered model total, use a measure.
- Hidden fields. If you hide a field in the visual, the calculation can still use it. This is covered in a later chapter.
Common errors
- Using
COLLAPSEwhen you want the grand total. On a three-level hierarchy,COLLAPSEstops one level up. OnlyCOLLAPSEALLalways reaches the top. If a Product row under Category and Subcategory gives the wrong percentage, check which function you used. - Forgetting the axis argument. The function needs the axis (
ROWShere). If your hierarchy is on columns, useCOLUMNS. Using the wrong axis gives a result of the value itself, or an error, depending on the layout. - Dividing with
/instead ofDIVIDE. The slash gives an error on rows where the denominator is zero.DIVIDEreturns blank. - Adding the percentages across levels. A category row and its products both show percentages, and summing the whole column counts each amount twice. Only sum percentages within one level.
- Expecting the numbers to match a measure after a filter. A measure written with
ALLignores slicers. A visual calculation does not. Differences between the two usually come from this.
Choosing between a visual calculation and a measure
| Question | Visual calculation | Measure |
|---|---|---|
| Is the logic only for this one visual? | Good fit | Works, but adds to the model |
| Do you need to reuse it across visuals? | Not possible | Good fit |
| Should it ignore slicers and report filters? | Not directly | Use `ALL` or `REMOVEFILTERS` |
| Do you want to avoid writing filter-context logic? | Good fit | More work |
For a quick share-of-total column on one matrix, a visual calculation is shorter and easier to read. For a figure used in many places, or one that must ignore filters, use a measure.
Exercises
Exercise
A matrix shows Region on rows, then City below it, with Sales Amount in the Values well. The Region West has 800 in sales, and the city Seattle in West has 200. The grand total is 2000. What do Percent of parent and Percent of grand total show on the Seattle row, and which function is responsible for each denominator?
Show answer
Percent of parent is 200 / 800 = 25%, because COLLAPSE reads the value one level up, which is the West region. Percent of grand total is 200 / 2000 = 10%, because COLLAPSEALL reads the value at the top of the hierarchy, which is the grand total.
Exercise
You remove City from the matrix in the previous exercise, so only Region is on rows. What happens to the Percent of parent value on the West row, and why?
Show answer
It becomes 800 / 2000 = 40%. With one level on the rows axis, the parent of a Region row is the grand total, so COLLAPSE and COLLAPSEALL return the same value.
Exercise
Write a visual calculation that shows each row's percent of parent, and explain why it uses DIVIDE and not the / operator.
Show answer
Percent of parent = DIVIDE([Sales Amount], COLLAPSE([Sales Amount], ROWS)). DIVIDE returns blank when the denominator is zero or blank, while / returns an error on those rows.