Sub topic 9
Letting users set thresholds with a parameter table
This chapter assumes you can write basic DAX measures, build a table or matrix, and use Format by field value to connect a color measure to a visual. It also assumes you have the Variance % and Variance Color measures from earlier chapters. If you need a refresher, review the chapters on returning colors and on SWITCH(TRUE()).
Why make a threshold adjustable
A hard-coded threshold like 0.05 works until someone asks, "What if green meant 10% above target?" Each change then means editing the measure, republishing, and telling everyone. Report consumers cannot do this themselves.
A parameter table solves the problem. It is a small table that holds the candidate values. A slicer lets the user pick one. A measure reads the pick and uses it in the color logic. The formatting then responds to the slicer like any other visual.
This works because of evaluation context, which you met earlier. A slicer filters the parameter table. A measure that reads that table sees only the selected row. That value then flows into the color measure.
Step 1: Create the parameter table
You can type the values into a table by hand, but GENERATESERIES is faster for a numeric range. In Power BI Desktop, go to Modeling > New table and enter:
Threshold = GENERATESERIES ( 0, 0.2, 0.01 )This creates a table named Threshold with a single column named Value. It holds 21 rows: 0, 0.01, 0.02, up to 0.2. The arguments are the start, the end, and the step.
A few details matter here:
- The table has no relationship to any other table. This is intentional. The slicer should filter only the parameter table, not your sales data.
- The column is a decimal number. In the Column tools ribbon, set its Format to percentage so the slicer shows
5%instead of0.05. - The column name is
Value. Later formulas refer toThreshold[Value].
You can also use Modeling > New parameter > Numeric range. It builds the same kind of table and a matching measure for you. Writing the DAX yourself gives you control over names and defaults, so this chapter uses that approach.
Step 2: Add a slicer
Add a slicer visual and drag Threshold[Value] into it. Choose a style that suits the number of values. A dropdown is compact. A single-select list is easier to scan.
Set the slicer to Single select in the Slicer settings > Selection section. If you leave multi-select on, the user can pick several values. The next step then has no single answer to return, and the measure falls back to its default.
Step 3: Read the selection with SELECTEDVALUE
Create a measure that reads the slicer:
Green Threshold =
SELECTEDVALUE ( Threshold[Value], 0.05 )SELECTEDVALUE returns the value of the column when exactly one value is visible in the current filter context. In every other case it returns the second argument, which is the default. Here the default is 0.05.
The default is used in two situations:
- The user has not selected anything, so all 21 values are visible.
- The user selected more than one value.
The default is what keeps your report predictable. Without it, SELECTEDVALUE returns blank, and every comparison against blank behaves in ways that surprise you. Pick a default that matches your original business rule.
Step 4: Use the threshold in the color measure
Now replace the hard-coded 0.05 in the color measure with the variable limit:
Variance Color (Dynamic) =
VAR v = [Variance %]
VAR limit = [Green Threshold]
RETURN
SWITCH (
TRUE (),
ISBLANK ( v ), BLANK (),
v >= limit, "#2E7D32",
v >= 0, "#F9A825",
"#C62828"
)Reading the threshold into a variable has two benefits. The name limit explains the logic to the next person who opens the measure. The measure also evaluates [Green Threshold] only once, however many branches use it.
The order of the branches still matters. SWITCH(TRUE()) returns the result of the first condition that is true. The blank check comes first so that a blank variance never reaches the numeric comparisons. The limit test comes before the v >= 0 test, because any value that clears the limit also clears zero. If you swapped them, nothing would ever be green.
Finally, select the visual, open its formatting options for the field you want to color, choose Format by field value, and pick Variance Color (Dynamic) in the Based on field list. The slicer now controls the color.
What the slicer does and does not filter
The most common surprise is that the slicer affects more than you intended. A slicer on a disconnected table filters only that table. Because Threshold has no relationships, your sales visuals do not change when the user moves the slicer. Only measures that reference Threshold see the selection.
This is the behavior you want. Check it by confirming that Total Sales does not change when you move the slicer, and that the cell colors do.
One related point concerns the Edit interactions feature. If you place the threshold slicer on a page with several visuals, the slicer also tries to cross-filter each one. Because the table is disconnected, nothing visibly changes in the data. Still, if a visual shows a measure that references Threshold, it will respond. That is the intended outcome, and it means one slicer can drive the colors in a table, a matrix, and a chart at once.
Showing the active threshold
Users should be able to see which rule is in force. Add a card visual or a title that displays the current value:
Threshold Label =
"Green at or above " & FORMAT ( [Green Threshold], "0%" ) & " over target"Use this measure as a card or as a dynamic visual title, which a later chapter covers. Without a label, a viewer who opens a screenshot of the report cannot tell why a cell is green.
Edge cases and failure modes
The slicer is cleared. With no selection, all rows are visible, SELECTEDVALUE returns its default, and the report keeps working with 0.05. The slicer shows nothing selected, though, so the label measure above matters.
The user picks several values. Same result: the default applies. If you want to block this, set the slicer to single select and consider turning on Single select with Ctrl off, so a plain click selects one value.
The threshold is zero. At 0, the v >= limit test and the v >= 0 test are identical, so no cell is ever amber. That is a valid outcome, but it surprises people. Mention it in the label, or start the series at 0.01.
Row-level security or slicer syncing. If you sync the slicer across pages, a choice on one page carries to the others. This is usually helpful. If you want pages to have different thresholds, create a separate parameter table for each.
Thresholds that are not a simple range. If the user needs to choose a named policy, such as "Strict", "Standard" or "Relaxed", create the table by hand with the DATATABLE function:
Policy =
DATATATABLE (
"Policy", STRING,
"Green Limit", DOUBLE,
{
{ "Strict", 0.10 },
{ "Standard", 0.05 },
{ "Relaxed", 0.02 }
}
)Then read the choice with SELECTEDVALUE ( Policy[Green Limit], 0.05 ). The user sees friendly names, and the measure sees numbers.
Verifying the setup
Test the parameter in three steps:
- Put
Variance %andVariance Color (Dynamic)in a table. Seeing the hex string next to the number is the fastest check. - Move the slicer through several values and watch the hex string change on rows near the boundary.
- Clear the slicer and confirm the colors match the default.
If the colors do not change, check that the slicer's field is Threshold[Value] and not a column from another table. Also check that Green Threshold returns a number, not text. You can do this by placing it in a card.
Summary table
| Piece | Purpose | Common mistake |
|---|---|---|
| Parameter table | Holds the allowed threshold values | Connecting it to the model by a relationship |
| Slicer | Lets the user choose a value | Leaving multi-select on |
| `SELECTEDVALUE` measure | Reads the choice, with a default | Omitting the default |
| Color measure | Compares variance to the threshold | Putting the `limit` test after the `0` test |
| Label measure | Shows the active rule | Forgetting it, so screenshots are unclear |
Exercise
A colleague builds a Policy table with three rows and a slicer on Policy[Policy]. The color measure uses SELECTEDVALUE ( Policy[Green Limit], 0.05 ). Users report that when they select both "Strict" and "Relaxed", the cells turn green at 5%. Why does this happen, and how do you prevent the confusion?
Show answer
With two policies selected, two rows of the table are visible, so SELECTEDVALUE does not have a single value to return. It returns its default argument, 0.05, which is the "Standard" limit that was not selected. To prevent it, set the slicer to single select so only one policy can be chosen. You can also show a label measure that states the active limit, so users can see when the default is in effect.
Exercise
You change Threshold to GENERATESERIES ( 0, 0.2, 0.01 ) and set the slicer to 0. Which colors can still appear in the table, and why?
Show answer
Green and red can appear, and amber cannot. With the limit at 0, any variance of 0 or more passes v >= limit and is green. Any negative variance fails both numeric tests and is red. The v >= 0 branch is never reached by a non-blank value, so no cell is amber. Blank variances still return no color because the blank check comes first.