Sub topic 8
Controlling totals, subtotals, and matrix levels
This chapter assumes you can build a matrix in Power BI Desktop and that you already have the [Variance %] and [Variance Color] measures from earlier chapters. It covers why totals get colors you did not intend, and how to stop that.
Why totals get colored
A measure runs once for every cell, in the filter context of that cell. A total row has no filter on the row field, so the measure evaluates over all rows. [Variance %] still returns a number there, and [Variance Color] turns that number into a color.
The result can mislead. A total cell is colored with the same thresholds as a single category, even though a total is a different kind of number. Subtotals in a matrix have the same problem. You decide whether those cells should be colored, and you tell the measure how to tell them apart.
Detecting the level with ISINSCOPE
ISINSCOPE ( column ) returns TRUE when the column is a grouping level of the current row. On a category row it is TRUE for Product[Category]. On the grand total row it is FALSE.
Variance Color (Matrix) =
IF (
ISINSCOPE ( Product[Category] ),
[Variance Color],
BLANK ()
)This measure colors only rows grouped by Product[Category]. The total and every other level return blank, and a blank result means no color is applied.
Matrix with several levels
A matrix can have a hierarchy on rows, such as Category and then Product. Each level has its own scope. At the Category row, Product[Category] is in scope but Product[Name] is not. At a Product row, both are in scope.
The diagram shows which rows get a color when the measure tests for the Product level.
To color only the lowest level, test the lowest column:
Variance Color (Product Level) =
IF (
ISINSCOPE ( Product[Name] ),
[Variance Color],
BLANK ()
)To color categories but not products, you need the reverse. Test that the category is in scope and the product is not:
Variance Color (Category Only) =
IF (
ISINSCOPE ( Product[Category] ) && NOT ISINSCOPE ( Product[Name] ),
[Variance Color],
BLANK ()
)Choosing a different color for totals
Blank is not the only option. You can return a neutral color so totals stay visible but do not signal good or bad. Use SWITCH ( TRUE (), ... ) to separate the levels:
Variance Color (Neutral Totals) =
SWITCH (
TRUE (),
ISINSCOPE ( Product[Category] ), [Variance Color],
"#E0E0E0"
)The last argument is the default. Every row that is not a category row gets light gray.
Applying the measure
- Select the matrix and open the Format pane.
- Expand the formatting target, for example Cell elements, then Background color.
- Open the fx button and set Format style to Field value.
- Choose the level-aware measure in the field picker.
Totals are formatted through the same setting. If a total still shows a color, check that you picked the level-aware measure and not the plain [Variance Color].
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Total row is colored | Plain color measure used | Switch to the ISINSCOPE version |
| No row is colored | Tested column is not on the visual | Test a column that is in the matrix |
| Subtotals colored, products not | Wrong column tested | Test the lowest-level column |
Exercise
A matrix has Category on rows and Product[Name] nested under it. You want colors on Category rows only, and the grand total and product rows left uncolored. Which condition do you put around [Variance Color]?
Show answer
Use ISINSCOPE ( Product[Category] ) && NOT ISINSCOPE ( Product[Name] ). At a category row, only Category is in scope. At a product row, both are in scope, so the NOT clause excludes it. The grand total has neither in scope, so the first test fails. Return BLANK () otherwise.