Sub topic 7
Handling blanks and missing targets
A conditional format that looks right on complete data can mislead the moment a target is missing. This chapter covers how DAX treats blanks, why a missing target often turns green or amber by accident, and how to write a color measure that handles each case on purpose.
This chapter assumes you can write measures with SUM and DIVIDE. It also assumes you have a working color measure that uses VAR and SWITCH ( TRUE (), ... ), and that you know how to apply it with Format by field value.
What a blank does to a format
Power BI applies the color your measure returns. If the measure returns BLANK (), or any string that is not a valid color, the visual falls back to its default formatting for that cell. No error appears. The cell just looks unformatted.
Returning BLANK () is therefore a deliberate way to say "do not color this cell". The risk is the opposite one: a blank that sneaks into your comparisons and produces a color you did not intend.
Where blanks come from
Blanks appear in a variance measure in four common ways:
SUM ( Sales[Amount] )returns blank when no sales rows exist in the current filter context. It does not return 0.SUM ( Targets[TargetAmount] )returns blank when a product, month, or region has no target row.DIVIDE ( numerator, denominator )returns blank when the denominator is 0 or blank, unless you pass a third argument.- A relationship that does not match a row, such as a target stored at a different grain, makes the target blank for that row.
These are different business situations. "We sold nothing against a target of 100" is a bad result. "We sold 500 and nobody set a target" is an unknown result. A single color rule cannot treat them the same way.
The trap: DAX compares blank as zero
In a comparison, DAX treats BLANK () as 0. Both of these are true:
Blank Equals Zero = BLANK () = 0
Blank Is At Least Zero = BLANK () >= 0This matters for the color measure from earlier chapters:
Variance Color (Naive) =
VAR v = [Variance %]
RETURN
SWITCH (
TRUE (),
v >= 0.05, "#2E7D32",
v >= 0, "#F9A825",
"#C62828"
)If v is blank, v >= 0.05 is false but v >= 0 is true, so the cell turns amber. A row with no target is shown as "on target, but only just". That is misleading. The ISBLANK ( v ) test must come before any numeric comparison, because SWITCH ( TRUE (), ... ) returns the result for the first condition that is true.
The same problem appears if you "fix" the blank at the source:
Variance % (Zero Fallback) =
DIVIDE ( [Total Sales] - [Sales Target], [Sales Target], 0 )The third argument replaces a blank result with 0. A missing target now yields a variance of exactly 0, which the color rule reads as amber. It also hides the problem in any table that shows the number. Leave the alternate result out unless 0 is a real, correct answer.
Four cases to separate
Before writing the measure, decide what each combination of sales and target should look like. One reasonable policy:
| Sales | Target | Meaning | Color |
|---|---|---|---|
| Blank | Blank | Nothing to report | None |
| Present | Blank or 0 | No target to compare against | Neutral grey |
| Blank | Present | Target set, nothing sold | Red |
| Present | Present | Normal comparison | Green, amber, or red |
Your policy may differ. Some teams prefer no color for every missing-data case, and some want a warning color for a missing target. What matters is that you pick the behavior and write it down, instead of letting comparison rules pick it for you.
A blank-aware color measure
This version stores each input in a variable once, then tests the cases in the order shown in the diagram. It uses the base measures [Total Sales], [Sales Target], and [Variance %] from earlier in the tutorial.
Variance Color (Blank-Aware) =
VAR sales = [Total Sales]
VAR target = [Sales Target]
VAR v = [Variance %]
RETURN
SWITCH (
TRUE (),
ISBLANK ( sales ) && ISBLANK ( target ), BLANK (),
ISBLANK ( target ) || target = 0, "#9E9E9E",
ISBLANK ( sales ), "#C62828",
v >= 0.05, "#2E7D32",
v >= 0, "#F9A825",
"#C62828"
)Some details of how this works:
- Each variable is evaluated once, in the filter context of the cell being formatted. The tests then reuse that value.
ISBLANK ( target ) || target = 0is partly redundant, because a blank equals 0 in a comparison. The explicitISBLANKis still worth keeping, so the next reader sees the intent.- When sales are blank and the target is present,
[Variance %]is already-1(a 100% shortfall), because the subtraction treats blank as 0. TheISBLANK ( sales )line makes that case explicit instead of relying on that arithmetic. - The final
"#C62828"is the default result ofSWITCH, used when no earlier condition is true. Here it means "below target".
A grey cell and an unformatted cell look different in a table, which is the point. Grey says "we checked and there is no target". No color says "nothing to report".
Check the cases with a state measure
When a color looks wrong, you cannot tell which branch produced it by looking at a hex code. A companion measure that returns a readable label for the same branches makes that visible:
Variance State =
VAR sales = [Total Sales]
VAR target = [Sales Target]
VAR v = [Variance %]
RETURN
SWITCH (
TRUE (),
ISBLANK ( sales ) && ISBLANK ( target ), "No data",
ISBLANK ( target ) || target = 0, "No target",
ISBLANK ( sales ), "No sales",
v >= 0.05, "Ahead",
v >= 0, "On target",
"Behind"
)Add [Total Sales], [Sales Target], [Variance %], [Variance State], and [Variance Color (Blank-Aware)] to a table next to each other. Every row now shows the inputs, the branch that was chosen, and the color that was returned. Keep the two measures in step: if you change the order or thresholds in one, change the other.
Blank rows and blank cells are different things
A blank color result only affects formatting. It does not remove the row. Whether a row appears at all depends on the visual: a table or matrix shows a row when at least one value measure in it returns a non-blank result. If your color measure is the only field that would be non-blank for a row, that row will not appear.
Two consequences follow:
- A product with a target but no sales shows up only if
[Sales Target]or another measure in the visual is non-blank for it. Add the target to the visual, or the "No sales" case never appears. - Setting a colored cell to blank does not hide its value. The number is still there, just unformatted.
Rows missing from the visual cannot receive a color at all, so check that the rows exist before debugging the color logic.
Common mistakes
- Testing for zero when you mean blank.
v = 0is true for a blank. UseISBLANK ( v )when you need to tell them apart. A real variance of exactly 0 is rare, but a real target of 0 is not. - Putting
ISBLANKlast. Earlier numeric tests catch the blank first, so the blank branch never runs. - Using a third argument in
DIVIDEto avoid blanks. This replaces "unknown" with a real-looking 0, which then gets a real color. - Returning a text such as
"None"for no color. It is not a valid color, so Power BI ignores it. This usually works by accident, but it hides intent. ReturnBLANK ()instead. - Checking blanks in the wrong context. The variables are evaluated per cell. A test that works in a table of products can give a different result in a total row, which the next chapter covers.
Exercise
A table shows products with Total Sales, Sales Target, and a cell color from Variance Color (Naive), which has no blank test. One product has sales of 800 and no target row. What color does its Variance % cell get, and why? What single change makes the cell grey?
Show answer
It turns amber. Variance % is blank because DIVIDE returns blank when the denominator is blank. The test v >= 0.05 is false, but v >= 0 is true because DAX treats a blank as 0 in comparisons, so SWITCH returns the amber branch. To make it grey, store the target in a variable and add the test ISBLANK ( target ) || target = 0, "#9E9E9E" before any numeric comparison on v.
Exercise
A colleague changes the variance measure to DIVIDE ( [Total Sales] - [Sales Target], [Sales Target], 0 ) so that the table shows 0% instead of empty cells. What happens to the color for products with no target, and what should they do instead?
Show answer
Those products now have a variance of exactly 0, so v >= 0 is true and they turn amber, which reads as "on target". The missing data is hidden. They should remove the third argument so the result stays blank, then handle missing targets explicitly in the color measure with an ISBLANK ( target ) check that returns grey or BLANK ().