Sub topic 11
Where to go next: ALLSELECTED, KEEPFILTERS, and VALUES
This chapter assumes you have read the earlier chapters. You should be comfortable with filter context, CALCULATE, and the four functions this tutorial centers on: FILTER, ALL, ALLEXCEPT, and REMOVEFILTERS. You should also know how slicers and visual groupings filter a Power BI or Analysis Services model.
Three more functions often appear next to those four in real models: ALLSELECTED, KEEPFILTERS, and VALUES. Each one solves a problem the earlier functions leave open.
ALLSELECTEDfixes percent-of-total measures that ignore the user's slicers.KEEPFILTERSstops aCALCULATEfilter from overwriting a filter that is already there.VALUESreads the filter context instead of changing it.
The chapter introduces each one briefly and shows when to reach for it. It does not cover every edge case.
ALLSELECTED: respect the slicer, ignore the visual
REMOVEFILTERS ( 'Product' ) clears every filter on the table. That includes filters from a slicer, so a percent-of-total measure built on it divides by the sales of the whole model. Sometimes that is correct. Often a report reader expects the percentages of the visible rows to add up to 100%.
ALLSELECTED removes the filters that come from inside the visual (the rows and columns of a table or matrix). It keeps the filters that come from outside the visual, such as slicers, page filters, and report filters.
Sales % of Selected =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], ALLSELECTED ( 'Product' ) )
)Suppose the model has three categories:
| Category | Total Sales |
|---|---|
| Bikes | 600 |
| Accessories | 300 |
| Clothing | 100 |
A slicer selects Bikes and Accessories, and a table visual shows one row per category. The two denominators differ.
| Row | Using REMOVEFILTERS ( 'Product' ) | Using ALLSELECTED ( 'Product' ) |
|---|---|---|
| Bikes | 600 / 1000 = 60.0% | 600 / 900 = 66.7% |
| Accessories | 300 / 1000 = 30.0% | 300 / 900 = 33.3% |
| Total row | 90.0% | 100.0% |
The REMOVEFILTERS version is not wrong. It answers "what share of all company sales is this?" The ALLSELECTED version answers "what share of what I am looking at is this?" Pick the one that matches the question the report is meant to answer.
Running totals with ALLSELECTED
ALLSELECTED also works as a table function, like ALL. A common use is a running total that respects the date range chosen in a slicer. This example assumes a 'Date' table with a Date column that is related to Sales.
Running Total =
CALCULATE (
[Total Sales],
FILTER (
ALLSELECTED ( 'Date'[Date] ),
'Date'[Date] <= MAX ( 'Date'[Date] )
)
)ALLSELECTED ( 'Date'[Date] ) returns the dates the user has selected, ignoring the row the visual is currently evaluating. FILTER then keeps only dates up to the current row's date. MAX is evaluated in the outer filter context, so it returns the date of the current row.
Where ALLSELECTED can surprise you
- It relies on the filter context that the visual itself creates, sometimes called the shadow filter context. In a simple table or matrix this behaves as described. When a measure is used in nested iterators or inside other calculations that change the context, results can be harder to predict. Test such measures in a real visual.
- You can call it with no arguments, which affects the whole model. Pass a table or column instead, so the measure says which filters it means to restore.
- It is not a substitute for
ALLEXCEPT. It does not keep a chosen column's filter. It keeps the filters that come from outside the visual.
KEEPFILTERS: add a filter without overwriting
The earlier chapters showed this measure:
Red Sales =
CALCULATE ( [Total Sales], 'Product'[Color] = "Red" )A boolean filter argument replaces any existing filter on 'Product'[Color]. If a slicer selects Blue, Red Sales ignores the slicer and still returns sales of red products. Sometimes that is exactly what you want. Sometimes you want the measure to respect the slicer and show only the overlap.
KEEPFILTERS changes how CALCULATE applies a filter argument. Instead of replacing the existing filter on the column, it intersects the new filter with the existing one.
Red Sales Kept =
CALCULATE (
[Total Sales],
KEEPFILTERS ( 'Product'[Color] = "Red" )
)The results depend on what the slicer selects:
| Color slicer | Red Sales | Red Sales Kept |
|---|---|---|
| No selection | Sales of red products | Sales of red products |
| Red | Sales of red products | Sales of red products |
| Blue | Sales of red products | Blank |
| Red and Blue | Sales of red products | Sales of red products |
With Blue selected, no product is both blue and red, so the intersection is empty and the measure returns blank. That is the honest answer when the user has asked to see only blue products.
When KEEPFILTERS matters
- Measures that must obey slicers. A measure labeled "Red Sales" may need to behave as "red items within my selection."
- Totals that should add up. If a visual lists colors in rows and a measure replaces the color filter, each row shows the same replaced value. KEEPFILTERS keeps each row tied to its own color.
- Filter arguments built from a table. A table filter built with
FILTERover a fact table is evaluated in the current filter context, so it already stays inside the current selection. A filter built fromALLignores the context and replaces the filter on that column. Wrap it inKEEPFILTERSwhen you want it to intersect instead.
KEEPFILTERS only affects the filter argument it wraps. It does not protect against a REMOVEFILTERS or ALL placed elsewhere in the same CALCULATE.
VALUES: read the filter context
FILTER, ALL, ALLEXCEPT, REMOVEFILTERS, and ALLSELECTED all change the filter context. VALUES does not. VALUES ( 'Product'[Category] ) returns a one-column table of the distinct categories that are visible in the current filter context.
- With no filter on
Category, it returns every category. - With a slicer selecting two categories, it returns those two.
- In a table row for Bikes, it returns one row, Bikes.
Compare it with ALL ( 'Product'[Category] ), which always returns every category and ignores the context. VALUES respects the context, and ALL does not.
Detecting a single selection
A common use is to check whether exactly one value is visible, and to read that value.
Selected Category =
IF (
HASONEVALUE ( 'Product'[Category] ),
VALUES ( 'Product'[Category] ),
"Multiple categories"
)When the table has one row and one column, DAX converts it to a single value. If the table has more rows, the conversion fails with an error, so the HASONEVALUE test guards against it. SELECTEDVALUE wraps the whole pattern in one call:
Selected Category Short =
SELECTEDVALUE ( 'Product'[Category], "Multiple categories" )Iterating over visible values
VALUES also supplies the table for an iterator. Each visible category becomes one row, and the measure is evaluated once per row. Inside an iterator the row context becomes a filter context when you call a measure, because measure references carry an implicit CALCULATE.
Average Sales per Category =
AVERAGEX ( VALUES ( 'Product'[Category] ), [Total Sales] )In a visual with a slicer on two categories, this measure averages the two categories' sales. Replace VALUES with ALL and it averages over every category in the model, whatever the slicer says.
Rebuilding percent of category with VALUES
You can also express the earlier percent-of-category measure without ALLEXCEPT.
Sales % of Category Using VALUES =
DIVIDE (
[Total Sales],
CALCULATE (
[Total Sales],
ALL ( 'Product' ),
VALUES ( 'Product'[Category] )
)
)The arguments of CALCULATE are evaluated in the original filter context. So VALUES ( 'Product'[Category] ) captures the category visible in the current row. ALL ( 'Product' ) clears the table's filters, and the captured categories are applied again as a filter. The result matches ALLEXCEPT ( 'Product', 'Product'[Category] ) in the common case. ALLEXCEPT says what you mean more directly, so prefer it when you only need to keep a column's filter. The VALUES pattern becomes useful when you want to restore a filter that comes from a different place than the one you are clearing.
VALUES and the blank row
If a fact table contains a key with no match in a related dimension table, DAX adds a blank row to the dimension table so that the fact rows are not lost. VALUES includes that blank row. DISTINCT does not. This explains why a VALUES count is sometimes one higher than you expect. If you see an unexplained blank category in a visual, look for unmatched keys in the fact table.
Choosing among the new functions
The diagram shows a rough decision order for the three functions in this chapter. The earlier chapters still apply for the cases where you do not want to keep any filters.
A quick reference table gives the same guidance in a different form:
| Function | What it does to filter context | Typical use |
|---|---|---|
| ALLSELECTED | Removes filters from the visual, keeps outside filters | Percent of the visible total, running totals within a slicer range |
| KEEPFILTERS | Intersects a new filter with the existing one | Measures that must respect slicers on the same column |
| VALUES | Reads the visible values, changes nothing | Single-selection checks, iterating over visible items |
| ALL | Removes filters, or returns all rows or values | Fixed denominators, counting every item |
| REMOVEFILTERS | Removes filters, as a CALCULATE modifier only | Clear intent without returning a table |
Common mistakes with these functions
- Using ALLSELECTED when you wanted all sales. If the denominator should be the whole company, use
REMOVEFILTERS. A report where the denominator shifts with every slicer change is confusing when the label says "share of company sales." - Forgetting that a boolean filter replaces slicers. If a measure named "Red Sales" returns a value while a Blue slicer is active, a replaced filter is the reason. Decide whether that behavior is intended, and add
KEEPFILTERSif it is not. - Using VALUES where a single value is expected.
VALUESreturns a table. Outside a context with one visible value, converting it to a scalar produces an error. UseSELECTEDVALUEor guard withHASONEVALUE. - Assuming VALUES and DISTINCT are identical. They differ in how they treat the blank row added for unmatched keys.
- Using ALLSELECTED with no arguments in a large model. It then affects every table in the model. Name the table or column that you mean to restore.
Where to go from here
These functions open up several topics for further study:
- Time intelligence functions such as
DATESYTD, which build onALLSELECTEDandFILTERpatterns. - Context transition, which explains why a measure reference inside an iterator behaves like a
CALCULATE. - Query plans and performance analysis, which show the cost of filtering whole tables when a single column would be enough.
Exercise
A table visual lists categories in rows. A slicer selects Bikes (600) and Accessories (300), while Clothing (100) is not selected. A measure divides [Total Sales] by CALCULATE ( [Total Sales], ALLSELECTED ( 'Product' ) ). What percentage does the Accessories row show, and what does the same row show if the denominator uses REMOVEFILTERS ( 'Product' ) instead?
Show answer
With ALLSELECTED the denominator is 600 + 300 = 900, so Accessories shows 300 / 900 = 33.3%. With REMOVEFILTERS the denominator is 1000, so it shows 300 / 1000 = 30.0%.
Exercise
A slicer on 'Product'[Color] has only Blue selected. What do Red Sales (a CALCULATE with the boolean argument 'Product'[Color] = "Red") and Red Sales Kept (the same filter wrapped in KEEPFILTERS) return?
Show answer
Red Sales returns the sales of red products, because the boolean argument replaces the slicer's filter on Color. Red Sales Kept returns blank, because it intersects Red with the existing Blue filter, and no product is both.
Exercise
Write a measure that returns the average of [Total Sales] across only the product categories that are visible in the current filter context. Say why ALL would give a different answer.
Show answer
Average Sales per Category = AVERAGEX ( VALUES ( 'Product'[Category] ), [Total Sales] ). VALUES returns only the visible categories, so a slicer on two categories averages those two. ALL ignores the filter context and returns every category, so the average would cover the whole model regardless of the slicer.