Sub topic 3
Rules and gradient styles, and where they fall short
This chapter covers the two formatting styles that are built into Power BI and need no DAX: Rules and Gradient. You will learn how each one works, which settings cause the most confusion, and which jobs they can't do. Those limits are the reason the rest of this tutorial moves the logic into measures.
Prerequisites
This chapter assumes you can:
- Build a table or matrix in Power BI Desktop and open the Format pane.
- Write a simple measure with
SUMandDIVIDE. - Tell a column from a measure in the Data pane.
The three format styles
Power BI offers three ways to color a cell or a chart element. Open the Format pane, find a color setting such as Cell elements > Background color in a table or matrix, and select the fx button. The dialog has a Format style list with three options.
| Format style | Colors come from | Thresholds are set | Typical use |
|---|---|---|---|
| Gradient | Interpolating between two or three colors | In the dialog, as minimum, center, and maximum | Heat maps, ranking intensity |
| Rules | Fixed color per value range | In the dialog, as numeric ranges | Red, amber, green bands |
| Field value | A color string returned by a measure or column | In your DAX | Anything the first two can't express |
This chapter covers the first two. Field value gets its own chapter.
Rules
A rule says: if the value falls in a range, use this color. You define one or more rules, and each rule has a lower bound, an upper bound, and a color.
A worked example
Suppose a matrix shows Variance % by product category. The base measures are the same ones used throughout this tutorial:
Total Sales = SUM ( Sales[Amount] )
Sales Target = SUM ( Targets[TargetAmount] )
Variance % =
DIVIDE ( [Total Sales] - [Sales Target], [Sales Target] )To color the Variance % column with rules, open the fx dialog for Background color. Set Format style to Rules, and set Based on field to Variance %. Then enter three rules:
| Order | If value | Bounds | Color |
|---|---|---|---|
| 1 | is greater than or equal to | Number 0.05 and is less than Highest value | Green |
| 2 | is greater than or equal to | Number 0 and is less than Number 0.05 | Amber |
| 3 | is greater than or equal to | Lowest value and is less than Number 0 | Red |
A variance of 8% is stored as 0.08, so the thresholds are written as decimals. Select OK, and every cell in the column picks up a band color.
How rules are evaluated
Power BI checks the rules in the order listed and applies the first one that matches. You can reorder rules with the arrows in the dialog. Three details cause most of the errors:
- The upper bound is exclusive. A rule of "from 0 up to 0.05" does not include 0.05 itself. Put the boundary value in the rule that should own it. In the table above, a value of exactly 0.05 falls to rule 1 because rule 2 stops just below it.
- Gaps are left uncolored. If no rule matches, the cell keeps its default color. Overlapping ranges are resolved by order, which can hide mistakes.
- Number and Percent are different units. With the Number type, 0.05 means 0.05. With the Percent type, the value is a percentage of the range between the lowest and highest values in the column. Choosing Percent when you meant a 5% variance is a common mistake. It produces bands that look plausible and are wrong.
Rules on text and other fields
Rules can also be based on a field other than the one displayed. For example, you can color a product name by a measure of its sales. This works, but the thresholds are still numeric ranges you typed into the dialog. The dialog gives you no way to say "color red when this value is under that other measure."
Gradient
A gradient blends between colors across a numeric range. You choose a color for the Minimum, one for the Maximum, and optionally a Center by selecting Diverging. Each cell gets a color interpolated by where its value sits between those points.
Setting the endpoints
Each endpoint can be:
- Lowest value or Highest value, which Power BI works out from the data.
- A Number you type.
- A Percent of the range.
- A Percentile.
For a variance column, a diverging gradient is a natural choice. Set the minimum to red, the center to white with the type set to Number and the value 0, and the maximum to green. Values near zero look pale, and large misses or beats look saturated.
Gotcha: the scale depends on the data in the visual
If the endpoints are Lowest value and Highest value, they are calculated from whatever the visual currently shows. When a slicer filters the data, the lowest and highest values change. The same cell value can therefore get a different color before and after a filter. A 3% variance can be deep green in one view and almost white in another.
To get stable colors, enter fixed Number endpoints. The trade-off is that you must pick the endpoints yourself, and a value outside them is clamped to the end color.
Where they fall short
Rules and gradients are quick, and they are the right choice for many reports. They fail in predictable ways.
Thresholds are hidden and copied by hand
The numbers live inside the formatting dialog of one visual, on one field. A reviewer can't see them in the model. If your definition of "good" changes from 5% to 7%, you must find every visual that uses it and edit each dialog. Format Painter can copy formatting between visuals, but nothing keeps the copies in sync afterward.
Logic can't depend on other values
A rule compares one value against fixed numbers. It cannot express conditions such as:
- Red if sales are below target and the region is in the priority list.
- A different threshold for each category, such as 10% for hardware and 3% for services.
- Color by one measure, with an exception when a second measure is blank.
Anything that needs a second measure, a lookup, or an IF belongs in DAX.
Users can't change the thresholds
Thresholds typed into a dialog can't respond to a slicer. A manager who wants to ask "what if green starts at 8%?" has to ask the report author to edit the visual. A parameter table can drive a measure, and a dialog cannot be driven that way. A later chapter covers this.
Totals and subtotals get the same treatment
Rules apply to cells, and a total row is one more cell. A total variance of 4% gets amber from the same ranges as a single product, even if you want totals left plain or judged against a different standard. The format dialogs also offer limited control over which matrix levels are colored. Fine control over scope is another job for a measure.
Blanks need care
A blank variance, for instance a category with no target, doesn't behave like zero in every case. How a blank is colored depends on the format style and on your version of Power BI Desktop. Test it, and don't assume. Handling blanks deliberately is much easier when the color is something you compute.
Reuse across visuals is weak
Imagine a dashboard with a table, a matrix, a bar chart, and a card all showing variance, and each needs the same red, amber, and green. With rules you set up four dialogs. With a measure you write the logic once and point four visuals at it.
The set of colors is limited by the dialog
The dialog lets you pick colors from a picker. A measure can return any hex color you compute, and it can also return other things, such as SVG icons, that the dialog can't produce.
Choosing a style
The decision tree below summarizes when the built-in styles are enough.
As a guide:
- Use Gradient for exploratory heat maps where relative intensity is the point and exact thresholds don't matter.
- Use Rules for a single visual with a few stable bands that nobody will change.
- Use a measure when the same logic is needed in more than one place, when the thresholds are a business definition, or when totals, blanks, and matrix levels must be controlled.
A preview of the measure-driven version
The three-band rule table from earlier becomes one measure. Don't build it yet, because the next chapters explain how a measure reaches a visual and how to write the logic:
Variance Color =
VAR v = [Variance %]
RETURN
SWITCH (
TRUE (),
ISBLANK ( v ), BLANK (),
v >= 0.05, "#2E7D32",
v >= 0, "#F9A825",
"#C62828"
)The thresholds now sit in one place that you can read, review, and reuse. The measure also handles the blank case explicitly, which the dialog left to chance.
Exercise
A matrix shows Variance % with three rules. Rule 1 is "greater than or equal to 0.05 and less than Highest value" in green. Rule 2 is "greater than or equal to 0 and less than 0.05" in amber. Rule 3 is "Lowest value to less than 0" in red. A cell shows exactly 0.05. Which color does it get, and why? Then name two requirements that rules alone could not meet.
Show answer
The cell is green. Rule 2 has an exclusive upper bound of 0.05, so 0.05 does not belong to it, and rule 1 includes 0.05 as its lower bound. Rules can't meet requirements such as a different threshold per category, a condition based on a second measure, thresholds that users change with a slicer, or different treatment for the total row. Any two of these is a correct answer.
Exercise
A diverging gradient on Variance % uses Lowest value, a center of Number 0, and Highest value. A user filters the matrix to one region, and a 3% variance that was dark green now looks pale. What caused this, and how do you fix it?
Show answer
With Lowest value and Highest value as endpoints, the scale is recalculated from the data currently shown. After filtering, the highest value in the visual is different, so 3% sits at a different point between the center and the maximum. To fix it, replace the endpoints with fixed Number values, such as -0.2 and 0.2, so the same value always gets the same color.