Sub topic 3
Rules and gradient styles, and where they fall short
This chapter assumes you can build a table or matrix in Power BI Desktop, write a simple measure with SUM and DIVIDE, and find the Format pane. It covers the two formatting styles that work without DAX, and the cases where you need to move to a measure.
The two built-in styles
Power BI offers conditional formatting for cell backgrounds, font colors, data bars, and icons. Each of these can use one of three methods: Gradient, Rules, or Field value. This chapter covers the first two. Field value comes in the next chapter.
To open the dialog, select a table or matrix, open the Format pane, expand Cell elements, turn on Background color, and select the fx button.
Gradient
A gradient colors each cell on a continuous scale between a minimum and a maximum. You choose the field that supplies the value, pick a color for the lowest value and one for the highest, and optionally add a middle color. Each cell's color is blended according to where its value falls between the two ends.
Gradients suit a task like "show me where the big numbers are." They do not suit a task like "flag anything below target," because the colors have no fixed meaning. A value that is shaded red in one report may be shaded orange in the next if the minimum changes.
Rules
Rules assign a fixed color to a range of values. Each rule has a condition, such as "is greater than or equal to 0.05", and a color. Power BI checks the rules from top to bottom and applies the first one that matches.
A typical setup for variance percentage looks like this:
| Order | Condition | Color |
|---|---|---|
| 1 | Value >= 0.05 | Green |
| 2 | Value >= 0 | Amber |
| 3 | Value < 0 | Red |
Rules give you fixed thresholds with fixed meanings, so they fit status reporting better than gradients do.
Where they fall short
Rules and gradients work for a single visual with simple logic. They start to cause trouble in these situations.
- Repeated setup. The rules live inside one visual's formatting settings. If you have six tables that need the same traffic-light logic, you configure each one by hand. When the threshold changes from 5% to 8%, you edit all six.
- Hard-coded thresholds. The numbers are typed into the dialog. A user cannot change them with a slicer, and they cannot be read from a table in your model.
- Logic limited to one value. A rule can test the field it formats, or a field you select as the basis. It cannot easily combine conditions, such as "red if variance is negative and the product is not new."
- Blanks and totals. The dialog has limited control over blank values and over total rows. A blank can be treated as zero and fall into a rule you did not intend. Totals and subtotals often get colored with the same rules as the detail rows.
- Logic you cannot read or test. Rules sit in a dialog, so you cannot inspect their output or review them as code.
A measure addresses each of these. It is defined once in the model, it can read slicers and tables, it can combine any conditions DAX can express, and you can test it like any other measure.
A concrete limit
Take the variance example. The rules table above works for a table of products. Now suppose the report owner asks for the green threshold to be adjustable, and for the total row to stay uncolored. Neither request can be met in the rules dialog. Both can be met by a measure that returns a color, which is where the next chapters go.
The measures we will build on use these base definitions:
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 matters later, when the color logic has to decide what to do with a blank.
Exercise
A report has five tables that share the same three-color variance rules. The business changes the green threshold from 5% to 8%. Name two problems with rules-based formatting in this situation, and the approach that avoids them.
Show answer
The threshold is hard-coded in each visual, so you must edit all five tables by hand, and the setting cannot be changed by a slicer or read from a table. A DAX measure that returns a color, applied through Format by field value, holds the logic in one place in the model, so one edit updates every visual.