Sub topic 11
Where to go next: ALLSELECTED, KEEPFILTERS, and VALUES
This chapter assumes you have read the earlier chapters. You should know how CALCULATE changes filter context. You should also know that ALL, REMOVEFILTERS, and ALLEXCEPT clear filters, and that FILTER returns a table of rows. The three functions here cover cases those four do not handle well. Each is introduced briefly with one working pattern.
ALLSELECTED: respect the slicer, ignore the visual
REMOVEFILTERS and ALL clear every filter on a table, including filters from slicers and page filters. A percent-of-total built on them always divides by the grand total of the whole model. Sometimes you want the total of what the user has selected instead.
ALLSELECTED removes the filters that come from the visual itself, such as rows and columns. It keeps the filters that come from outside the visual, such as slicers and report filters.
Sales % of Selected =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], ALLSELECTED ( 'Product' ) )
)Suppose a slicer limits the report to two categories. With REMOVEFILTERS, the percentages add up to less than 100%, because the denominator still includes the hidden categories. With ALLSELECTED, the visible rows add up to 100%.
KEEPFILTERS: add a filter without replacing the existing one
You saw that a boolean filter argument in CALCULATE replaces any existing filter on the same column. In Red Sales, a slicer that selects Blue is overwritten, and the measure still returns red sales.
KEEPFILTERS changes this. It adds the new filter on top of the existing one, so the result is the intersection of the two.
Red Sales Within Selection =
CALCULATE (
[Total Sales],
KEEPFILTERS ( 'Product'[Color] = "Red" )
)If the slicer selects Red and Blue, the measure returns red sales. If the slicer selects only Blue, no row is both Blue and Red, so the measure returns blank. Use KEEPFILTERS when the measure should narrow the user's selection and never widen it.
VALUES: read the current selection
VALUES takes a column and returns a one-column table of the distinct values that are visible in the current filter context. It answers the question "what is selected right now?". ALL answers a different question: "what exists in the model?".
Selected Category Count =
COUNTROWS ( VALUES ( 'Product'[Category] ) )Compare this with Category Count from the ALL chapter, which uses COUNTROWS ( ALL ( 'Product'[Category] ) ). With a slicer on two categories, Category Count still returns every category in the model, while Selected Category Count returns 2.
A table with one row and one column can also be used as a single value. This lets you compare the current selection with a constant:
Is Bikes Selected =
IF ( VALUES ( 'Product'[Category] ) = "Bikes", "Yes", "No" )This measure raises an error when two or more categories are visible, because VALUES then returns more than one row. For this purpose, SELECTEDVALUE is safer. It returns the value when exactly one is visible and returns blank, or a default you supply, otherwise.
Selected Category =
SELECTEDVALUE ( 'Product'[Category], "Multiple" )VALUES can also return an extra blank row. This happens when the fact table has keys with no match in the dimension table. The blank row stands for those unmatched rows. If your counts are one higher than expected, check for this.
Comparing the functions
| Function | Returns a table | What it does to filters |
|---|---|---|
| ALL | Yes | Ignores all filters on a table or column |
| REMOVEFILTERS | No | Clears filters, only inside CALCULATE |
| ALLEXCEPT | Yes | Clears filters except on the columns you name |
| ALLSELECTED | Yes | Clears visual filters, keeps outside filters |
| KEEPFILTERS | No | Adds a filter without replacing the existing one |
| VALUES | Yes | Returns the values currently visible |
Common errors with these functions
- Using ALLSELECTED when you need a fixed grand total. Slicers will change the denominator, and users may read the percentages as shares of everything.
- Forgetting that a plain boolean filter overwrites the slicer. If the result ignores a slicer you expected to apply, add KEEPFILTERS.
- Putting VALUES where a single value is expected, then seeing an error once the user selects several items. Use SELECTEDVALUE.
- Expecting VALUES to ignore filters. It reflects them, and that is its purpose.
Where to go from here
Read the DAX documentation for each function in this chapter. After that, look at time-intelligence functions such as DATESYTD. They build on the same CALCULATE and filter-context model.
Exercise
A report has a slicer limited to the categories Bikes and Accessories, and a matrix showing one row per category. You write a percent-of-total measure with the denominator CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product' ) ). The two visible percentages add up to 62%. Which change makes them add up to 100%, and why?
Show answer
Replace REMOVEFILTERS ( 'Product' ) with ALLSELECTED ( 'Product' ). REMOVEFILTERS also clears the slicer, so the denominator includes the categories the slicer hid. ALLSELECTED clears only the filters from the matrix rows and keeps the slicer, so the denominator covers only Bikes and Accessories.
Exercise
A measure CALCULATE ( [Total Sales], 'Product'[Color] = "Red" ) returns red sales even when a slicer selects Blue. How do you change it so that it returns blank in that case?
Show answer
Wrap the filter in KEEPFILTERS: CALCULATE ( [Total Sales], KEEPFILTERS ( 'Product'[Color] = "Red" ) ). The red filter now intersects with the Blue slicer selection instead of replacing it, and no row matches both.