Sub topic 7
Handling blanks and missing targets
This chapter assumes you can write basic DAX measures with SUM and DIVIDE, and that you know how to apply Format by field value to a table or matrix. The goal is to make sure a cell with no data gets no color, instead of a misleading one.
Why blanks need their own branch
A color measure that ignores blanks can mislead. In DAX, BLANK() compares as equal to zero in many expressions. A product with no target can therefore fall into your "red" branch and look like a failure when it simply has nothing to measure against.
Consider three cases for one cell:
| Situation | `[Sales Target]` | `[Variance %]` | Should the cell be colored? |
|---|---|---|---|
| Target exists, sales above it | 1000 | 0.08 | Yes, green |
| Target exists, no sales | 1000 | -1 | Yes, red |
| No target row at all | blank | blank | No |
The third row is the case this chapter protects against.
Where the blank comes from
DIVIDE returns blank when the denominator is zero or blank. That is what makes [Variance %] blank for a missing target, as the base measures show:
Total Sales = SUM ( Sales[Amount] )
Sales Target = SUM ( Targets[TargetAmount] )
Variance % =
DIVIDE ( [Total Sales] - [Sales Target], [Sales Target] )The plain / operator would raise a divide-by-zero error for a zero target. An error in the color measure can stop the format from applying. DIVIDE avoids that, and gives you a blank to test for.
One more detail: SUM over no rows returns blank, not zero. So [Sales Target] is blank when no target rows exist for the current filter context. If the target is stored as an explicit 0, DIVIDE still returns blank, because the denominator is zero.
Testing for blank first
Put the blank test first in SWITCH ( TRUE (), ... ). SWITCH returns the result of the first condition that is true, so a blank caught early never reaches the comparisons below it.
Variance Color =
VAR v = [Variance %]
RETURN
SWITCH (
TRUE (),
ISBLANK ( v ), BLANK (),
v >= 0.05, "#2E7D32",
v >= 0, "#F9A825",
"#C62828"
)Returning BLANK() tells Power BI there is no color for that cell. The cell keeps its default background.
If you move ISBLANK below v >= 0, a blank v can match that condition and receive amber. Branch order is the most common source of wrongly colored empty cells.
Choosing what a missing target should look like
Returning BLANK() is one choice. You may want a missing target to be visible instead. Two alternatives:
- Neutral gray. Return a gray hex such as
"#BDBDBD"so readers can tell "no target" from "not formatted by mistake." - Explicit zero handling. Treat a target of zero as meaningful, and test
[Sales Target]directly instead of the variance.
Testing the target directly is useful when a blank variance can have more than one cause:
Variance Color (Gray for Missing) =
VAR t = [Sales Target]
VAR v = [Variance %]
RETURN
SWITCH (
TRUE (),
ISBLANK ( t ), "#BDBDBD",
ISBLANK ( v ), BLANK (),
v >= 0.05, "#2E7D32",
v >= 0, "#F9A825",
"#C62828"
)Common errors
- A blank cell shows red. The blank test is missing or sits below a comparison. Move
ISBLANKto the first branch. - The whole column has no color. The measure may return an error for some rows. Check for
/in place ofDIVIDE. - A zero target shows green or red. If sales are positive and the target is
0,DIVIDEreturns blank, so the cell gets no color. Decide whether that is what you want, and add a branch for it if not.
Exercise
Your Variance Color measure puts v >= 0, "#F9A825" before ISBLANK ( v ), BLANK (). A product has no target row. What color might its cell receive, and how do you fix it?
Show answer
The cell can receive amber, because a blank v can satisfy v >= 0 when compared as if it were zero. Fix it by moving the ISBLANK ( v ), BLANK () branch to the first position in SWITCH ( TRUE (), ... ), so a blank returns no color before any threshold is tested.