Sub topic 6
Writing threshold logic with VAR and SWITCH(TRUE())
This chapter assumes you can write basic DAX measures such as SUM and DIVIDE, and that you know how to open the Format pane in Power BI Desktop. It also assumes you have seen the Format by field value option, which reads a color from a measure. Here you build the measure that decides which color to return.
The problem with nested IF
A three-color rule (green, amber, red) written with nested IF functions works, but it gets hard to read as you add bands. Each new band adds another level of nesting and another closing parenthesis.
Two DAX features keep the logic flat:
- A variable, declared with
VAR, stores a result once so you can reuse it by name. SWITCH ( TRUE (), ... )tests a list of conditions from top to bottom and returns the result for the first one that is true.
Start with base measures
The color measure should test a value that is already computed. Define the pieces as separate measures so you can check each one on its own.
Total Sales = SUM ( Sales[Amount] )
Sales Target = SUM ( Targets[TargetAmount] )
Variance % =
DIVIDE ( [Total Sales] - [Sales Target], [Sales Target] )DIVIDE returns blank instead of an error when the target is zero or missing. That blank becomes important in the color measure below.
Build the color measure
Variance Color =
VAR v = [Variance %]
RETURN
SWITCH (
TRUE (),
ISBLANK ( v ), BLANK (),
v >= 0.05, "#2E7D32",
v >= 0, "#F9A825",
"#C62828"
)The measure works in three steps:
VAR v = [Variance %]evaluates the variance once and stores it inv. Every test below reuses that value, so DAX does not recalculate the measure for each condition.SWITCH ( TRUE (), ... )comparesTRUE ()against each condition in order. The first condition that evaluates to true wins.- The last argument,
"#C62828", has no condition. It is the default, returned when nothing above matched.
The result is a hex color string: green at 5% or more above target, amber at or above target, red below target, and no color when the variance is blank.
Why the blank test comes first
In DAX, a blank compared with a number behaves like zero in many comparisons. If ISBLANK ( v ) is not tested first, a row with no target could satisfy v >= 0 and show amber, which suggests "on target" when the truth is "no data". Testing for blank at the top returns BLANK (), and Power BI applies no color.
Apply the measure
- Select the visual, then open the Format pane.
- Find the property you want to color, such as Cell elements > Background color, and select the fx button.
- Set Format style to Field value.
- In "What field should we base this on?", choose
Variance Color.
Power BI reads the hex string for each cell and paints it.
Common errors
| Symptom | Likely cause |
|---|---|
| Every cell is amber or red | Conditions are in the wrong order, or the measure compares the wrong value |
| Blank rows show a color | The `ISBLANK` test is missing or placed below other tests |
| No color appears anywhere | The measure returns text that is not a valid color, such as a missing `#` |
| Colors differ from what you expect | The measure is evaluated in each cell's filter context, so check the value in a table first |
Exercise
You want a fourth band: values of 15% or more should be dark green "#1B5E20". Where in the SWITCH list do you add it, and why?
Show answer
Add v >= 0.15, "#1B5E20" directly before the v >= 0.05 line, after the blank test. SWITCH returns the first true condition, so the strictest threshold must come before looser ones. Otherwise a 20% variance would match v >= 0.05 and return the lighter green.