Sub topic 13
Testing measures and fixing formats that do not appear
This chapter assumes you can build table and matrix visuals in Power BI Desktop and write simple DAX measures. It also assumes you have used the Format pane and have a color measure such as Variance Color from earlier chapters. Here you learn to find out why a measure-driven format shows nothing, the wrong color, or a color in the wrong place.
Why these failures are hard to see
A conditional format has no error message. If the measure returns something Power BI cannot read as a color, the cell is drawn with its default formatting. A format that is missing looks the same as one whose logic is wrong, so you have to inspect the measure's output directly.
Most failures fall into four groups:
- The measure returns the wrong data type.
- The measure returns a string that is not a valid color.
- The measure returns blank where you expected a color, or a color where you expected blank.
- The format is wired to the wrong field, or something in the visual overrides it.
The core technique: look at the measure's output
Never debug a color measure by staring at the formatted visual. Put the measure in a plain table and read what it returns.
- Create a table visual with the same row field as the visual that is failing, such as
Product[Category]. - Add the inputs:
[Total Sales],[Sales Target], and[Variance %]. - Add the color measure,
[Variance Color], as the last column. - Compare each row against what you expect.
The color column shows the exact string for each row. You can see #2E7D32, a number, an empty cell, or text you did not intend. The table must have the same row grouping, filters, and slicers as the failing visual. The same measure can return different values in different filter contexts.
Query the model with DAX query view
DAX query view in Power BI Desktop lets you run the measure outside any visual. This removes visual-level filters and formatting from the picture.
EVALUATE
SUMMARIZECOLUMNS (
Product[Category],
"Total Sales", [Total Sales],
"Sales Target", [Sales Target],
"Variance %", [Variance %],
"Color", [Variance Color]
)
ORDER BY Product[Category]SUMMARIZECOLUMNS produces one row per category plus the measure values for each. Check that Color is a hex string for every row that should have one. Rows with blank inputs should show a blank color.
Build a diagnostic measure
A validity check can tell you what is wrong with a returned string. This measure reports a reason instead of a color:
Color Check =
VAR c = [Variance Color]
RETURN
SWITCH (
TRUE (),
ISBLANK ( c ), "blank",
LEFT ( c, 1 ) <> "#", "missing #",
LEN ( c ) <> 7, "wrong length",
"ok"
)Add [Color Check] next to [Variance Color] in the test table. Any row that is not ok or an intended blank needs attention. This check assumes six-digit hex colors, which is what the earlier chapters used. If you also return named colors, adjust it.
Symptom checklist
| Symptom | Likely cause | How to confirm | Fix |
|---|---|---|---|
| No color anywhere | The measure returns a number, or the format is not set to Field value | Check the measure's data type in Measure tools. Open the format dialog. | Return text. Set Format style to Field value and choose the measure. |
| No color anywhere | The string is not a valid color, for example `2E7D32` without `#` | Use `Color Check` | Add the `#` and keep the six digits |
| Some rows have no color | The measure returns blank for them | Compare against `Variance %` in the test table | Decide whether blank is intended. If not, fix the logic. |
| Blank rows show green or red | Blank is treated as zero in a comparison | Check the order of the tests in `SWITCH` | Put `ISBLANK` first |
| Total row has a color you did not want | The measure does not check scope | Look at the Total row in the test table | Wrap with `ISINSCOPE` |
| Total row has no color but should | `ISINSCOPE` is false at that level | Check which column is in scope | Test the correct column, or remove the test |
| Color is right in the table but not in the chart | The wrong element is formatted, or the measure is not in the chart's field list context | Check which Format pane card holds the setting | Set the format on the correct card |
| Color is the same for every row | The measure ignores row context, or it reads a slicer selection that has not changed | Compare `Variance %` per row | Fix the base measures |
Blank behavior: the most common logic bug
In DAX, a blank compared with a number acts like zero in many comparisons. Consider this version of the color measure:
Variance Color (Wrong Order) =
VAR v = [Variance %]
RETURN
SWITCH (
TRUE (),
v >= 0.05, "#2E7D32",
v >= 0, "#F9A825",
ISBLANK ( v ), BLANK (),
"#C62828"
)When v is blank, v >= 0 evaluates to true. The row gets amber, so a missing target looks like a business that is slightly on track. The ISBLANK test never runs because an earlier test matched first. SWITCH(TRUE()) returns the result of the first test that is true, so order matters. Put the ISBLANK test first, as in the original Variance Color.
To confirm this is your problem, find a row where [Sales Target] is blank in the test table. If [Variance Color] is amber there, you have this bug.
Format wiring problems
If the measure output is correct in the test table and the visual still shows no color, the problem is in the formatting setup. Work through these steps in order.
- Open the Format pane for the visual and find the element you want colored, such as Cell elements, Values, or Columns.
- Click the fx button next to the color. The dialog opens.
- Check that Format style is set to Field value. If it says Gradient or Rules, your measure is not being used.
- Check that "What field should we base this on?" names your color measure and not
[Variance %]or another measure. - Check that the element is turned on. For table and matrix cells, the Background color, Font color, or Data bars switch must be on.
A few things cause confusion:
- A color measure returns a color for one property. If you set it as Background color, the cell background changes and the font does not.
- Font and background of the same color make text invisible. If a cell looks empty, check whether the text and fill match.
- Formatting set on one column does not carry to another. In a table, the setting applies to the field named in the Format pane, so select the correct field in the "Apply settings to" or series dropdown.
- A conditional format from the fx dialog overrides the manual color for that element. If you set a manual color and nothing changes, a conditional format may already be in place.
Filter context differences
A color measure depends on the filter context of the visual. A measure that works in a table can behave differently in a matrix or chart for these reasons:
- A visual-level, page-level, or report-level filter removes the rows you were testing with.
- A slicer sync or an interaction setting means a slicer you expected to affect the visual does not.
- A relationship does not propagate filters from the dimension to the target table. Then
[Sales Target]returns the same value for every category, or blank.
To check this, add the failing visual's filters to your test table, or select the visual and use Performance Analyzer. Performance Analyzer lets you copy the query that Power BI sent for the visual. You can paste that query into DAX query view and see the exact values the visual received.
Matrix and total issues
Matrix visuals add a complication because the same measure is evaluated at several levels: each row level, each subtotal, and the grand total. If a color appears on a subtotal you did not want, or is missing from a row you did, test what ISINSCOPE returns at each level.
This debug measure shows which level a cell is on:
Level Check =
SWITCH (
TRUE (),
ISINSCOPE ( Product[Subcategory] ), "Subcategory",
ISINSCOPE ( Product[Category] ), "Category",
"Total"
)The order runs from the deepest level to the shallowest. A row grouped by subcategory is also inside a category, so testing Category first would label every row Category. Add [Level Check] as a value in the matrix and read it at each row. This assumes your matrix has Product[Category] above Product[Subcategory]. Adjust the names to match your hierarchy.
A repeatable test routine
Use the same steps each time you add a color measure:
- Build the base measures and test each in a table.
- Write the color measure and test it in the same table.
- Check the first and last row, a row with blank inputs, and the Total row.
- Wire the format. Then check the visual against the table.
- Change a slicer and confirm the colors respond.
If you test blank, boundary, and total cases before wiring the format, most failures show up early. Boundary cases are values exactly at a threshold, such as a variance of exactly 0 or 0.05. Test them with the comparison operators in mind: >= includes the boundary, > does not.
Edge cases
- Smart quotes. If you paste DAX from a document, curly quotes can replace straight quotes and cause an error in the measure. The editor will usually flag this.
- Spaces. A string such as
" #2E7D32"with a leading space is not a valid color. UseLENto find it. - Measure format string. A format string on a text measure does not affect the color. Do not use it to try to fix color problems.
- Data type of the field. A column used in Field value must be text. A measure returning a number needs to be changed to return a string.
- Performance. A color measure runs once per cell. Keep it built on existing measures through variables instead of repeating heavy calculations.
Exercise
A matrix shows amber on a row where the sales target is missing. The measure is SWITCH ( TRUE (), v >= 0.05, "#2E7D32", v >= 0, "#F9A825", ISBLANK ( v ), BLANK (), "#C62828" ), where v is [Variance %]. Why does the row get amber, and what is the fix?
Show answer
When v is blank, v >= 0 is true because blank behaves like zero in the comparison. SWITCH(TRUE()) returns the first matching test, so the amber result is returned before the ISBLANK test runs. Move ISBLANK ( v ), BLANK () to the first position in the SWITCH.
Exercise
Your [Variance Color] column in a test table shows correct hex strings for every row, but the table visual you wired it to shows no colors. Name two things in the Format pane you would check.
Show answer
Check that Format style in the fx dialog is set to Field value, and that "What field should we base this on?" names the color measure rather than a number measure. Also check that the element (for example Background color) is switched on and that you are formatting the correct field in the table.