Sub topic 8
Controlling totals, subtotals, and matrix levels
This chapter assumes you can build a matrix in Power BI Desktop, write measures with SUM and DIVIDE, and use "Format by field value" to attach a color measure to a visual. It also assumes you have the Variance % and Variance Color measures from earlier chapters. Here you will learn why totals and subtotals get colors you did not intend, and how to control them with ISINSCOPE.
Why totals get colors
A measure is evaluated once for every cell in a visual. Each cell has its own filter context. In a matrix, the Grand Total row is a cell too. It is evaluated with no filter from the row fields, so the measure sees all the data at once.
The Variance Color measure does not know whether it is running on a product row or on a total row. It computes [Variance %], compares the result to the thresholds, and returns a color. On the total row, the variance is the variance of the whole business. That number is valid, but it is often not what you want to judge by the same rule.
There are three common problems:
- A total is colored with thresholds designed for single items. A company-wide variance of 2% might be good, while the same value for one product might be a warning.
- A subtotal is colored as if it were a leaf row. The reader cannot tell which colors are decisions and which are summaries.
- The total of a ratio is not the sum of its parts. The colors of the child rows may not add up to the color of the parent, which confuses readers.
You decide, per level, whether a color should appear. Then you write that decision into the measure.
Seeing the levels of a matrix
Suppose a matrix has Product[Category] and Product[Subcategory] on rows, in that order, with [Variance %] as the value. The matrix has four kinds of rows:
| Row type | Example | Fields grouped on this row |
|---|---|---|
| Grand total | Total | none |
| Category subtotal | Bikes | Category |
| Subcategory row | Mountain Bikes | Category, Subcategory |
| Column totals (if columns are used) | Total column | depends on the column fields |
Power BI evaluates the same measure on each of these. The only difference is which columns are filtered or grouped. Your measure can ask about that difference.
Detecting the level with ISINSCOPE
ISINSCOPE ( column ) returns TRUE when the column is the one being grouped by at the current level of the visual. On the row for "Bikes" in a matrix that groups by Category and then Subcategory, ISINSCOPE ( Product[Category] ) is TRUE, and ISINSCOPE ( Product[Subcategory] ) is FALSE. On a subcategory row, both are TRUE. On the grand total, both are FALSE.
This is different from a filter check such as HASONEVALUE. HASONEVALUE asks "is there exactly one value in the filter context?", and a slicer or page filter can make it TRUE on a total row. ISINSCOPE asks about the visual's own grouping, so slicers do not change the answer.
Here is the measure from the topic examples. It colors only the category rows:
Variance Color (Matrix) =
IF (
ISINSCOPE ( Product[Category] ),
[Variance Color],
BLANK ()
)On the "Bikes" row, ISINSCOPE ( Product[Category] ) is TRUE, so the measure returns the color. On the grand total it is FALSE, so the measure returns BLANK and no color is applied.
Notice a limit of this version. Product[Category] is also in scope on the subcategory rows, because the subcategory sits inside a category. So this measure colors both category rows and subcategory rows. It hides only the grand total. Many people expect it to color only categories, and that is a common surprise.
Choosing the level you want
To pick exactly one level, test the deepest field that should be colored, and exclude the deeper ones.
| Goal | Condition |
|---|---|
| Hide the grand total only | `ISINSCOPE ( Product[Category] )` |
| Color only the deepest rows | `ISINSCOPE ( Product[Subcategory] )` |
| Color only category subtotals | `ISINSCOPE ( Product[Category] ) && NOT ISINSCOPE ( Product[Subcategory] )` |
The deepest-level test works because a field can only be in scope if every field above it in the hierarchy is also in scope. When the subcategory is in scope, the category is too.
This measure colors only the leaf rows:
Variance Color (Leaf Only) =
IF (
ISINSCOPE ( Product[Subcategory] ),
[Variance Color],
BLANK ()
)And this one colors only the category subtotals:
Variance Color (Category Only) =
IF (
ISINSCOPE ( Product[Category] )
&& NOT ISINSCOPE ( Product[Subcategory] ),
[Variance Color],
BLANK ()
)You can keep several of these measures in the model and choose which one to attach to which visual. A better approach in a large model is to keep one core color measure and let a thin wrapper decide scope, as shown above. Then the threshold logic lives in one place.
Giving totals their own rule
Sometimes a total should be colored, but with a different rule. A grand total might use a tighter band. You can branch on scope and return a different color for each branch.
Variance Color (Per Level) =
VAR v = [Variance %]
RETURN
SWITCH (
TRUE (),
ISBLANK ( v ), BLANK (),
ISINSCOPE ( Product[Subcategory] ),
IF ( v >= 0.05, "#2E7D32", IF ( v >= 0, "#F9A825", "#C62828" ) ),
ISINSCOPE ( Product[Category] ),
IF ( v >= 0.03, "#2E7D32", IF ( v >= 0, "#F9A825", "#C62828" ) ),
IF ( v >= 0.01, "#2E7D32", IF ( v >= 0, "#F9A825", "#C62828" ) )
)SWITCH ( TRUE (), ... ) returns the result for the first condition that is TRUE, so the order matters. The deepest level must be tested first. If you put ISINSCOPE ( Product[Category] ) first, it would also catch the subcategory rows, and the deeper branch would never run. The last argument, with no condition, is the default. Here it applies to the grand total.
Where the formatting is applied
Conditional formatting in a matrix can target several parts of the visual: the values, the row headers, and the column headers. For each one you choose the field and the format style, and you can pick "Apply to: Values only", "Values and totals", or "Total only" in the dialog for the values. The measure's output is what decides the color. If you attach a color measure and set "Apply to" to "Values only", Power BI skips the total row, and your measure never gets a chance to color it. If the total row stays uncolored when you expect a color, check this setting before you edit the DAX.
The reverse is also true. If "Values and totals" is chosen and the measure returns BLANK for the total, the total shows no color. Both settings together give you two ways to control the same result. Use one as the main control and treat the other as a safety net, so that a future change does not color a total by accident.
Column fields and multiple scopes
If the matrix also has a field on columns, such as Calendar[Year], the grand total column and the row total column have their own scopes. The test ISINSCOPE ( Calendar[Year] ) is TRUE on a year column and FALSE on the total column. Combine tests when you need to hide totals in both directions:
Variance Color (Both Axes) =
IF (
ISINSCOPE ( Product[Category] ) && ISINSCOPE ( Calendar[Year] ),
[Variance Color],
BLANK ()
)The cell at the intersection of the "Total" row and the "Total" column is not colored, because neither field is in scope there.
Common errors
- The grand total is still colored. The measure may not test scope at all, or the test uses a column that is in scope on the total. Check that the field in
ISINSCOPEis actually a row field of this visual. A column that is only used in a slicer is never in scope. - The test uses a field that is not in the visual.
ISINSCOPEon a field that the matrix does not group by is always FALSE, so every cell is blank. If the whole visual loses its colors, this is the first thing to check. - The test uses a hierarchy level through a hierarchy object. Use the underlying column in
ISINSCOPE. It takes a column reference, not a hierarchy. - Different results in a table and a matrix. A table visual has no subtotals, so only the grand total is out of scope. A measure written for a three-level matrix may behave differently when placed in a table.
- A ratio looks wrong on the total. The total of
DIVIDEis the ratio of the totals, not the average of the row ratios. That is correct behavior, but it can surprise readers. Decide whether to color the total at all.
A working pattern
- Write the threshold measure once, as in the earlier chapter, so that it returns a color or BLANK.
- Decide which levels should show color. Write that decision as a short wrapper measure that calls
ISINSCOPEand returns the threshold measure or BLANK. - Attach the wrapper to the visual with Format by field value.
- Set "Apply to" so that the dialog does not block the levels you want.
- Check every level: grand total, subtotal, leaf row, and the intersection of totals if you use columns.
Exercise
A matrix has Region on rows, then City below it. You attach IF ( ISINSCOPE ( Geo[Region] ), [Variance Color], BLANK () ) and expect only region rows to be colored. City rows are colored too. Why, and how do you fix it?
Show answer
City rows sit inside a region, so Geo[Region] is in scope on them as well. The test hides only the grand total. To color only region rows, add a condition that excludes the deeper field: ISINSCOPE ( Geo[Region] ) && NOT ISINSCOPE ( Geo[City] ).
Exercise
You wrote a measure that returns BLANK on the grand total, but the total row of the matrix still shows a color. Name two places you should check.
Show answer
First, check that the measure attached in Format by field value is the wrapper that tests scope, not the base color measure. Second, check the "Apply to" setting and confirm that the field used in ISINSCOPE is a row field of this matrix. A different measure or a field that is not grouped in the visual would explain the color.