ALLSELECTED in DAX: percent of what is selected

How ALLSELECTED works in DAX, removing the filters of the visual's rows and columns while keeping slicers and page filters, percent of visible total, running totals and ranks over the selection, ALL against ALLSELECTED side by side, and where ALLSELECTED surprises.

Short answer: ALLSELECTED removes the filters that come from the rows and columns of the visual but keeps the filters from outside: slicers, page and report filters, cross-filtering from other visuals. Inside CALCULATE it gives "the total of what the reader selected", so a share adds to 100% within the visual. With a column (ALLSELECTED ( 'Product'[Product] )) it also returns the selected values as a table, for ranks and averages over the selection.

ALL gives the share of everything that exists. Readers who pick three products in a slicer expect the three shares to add up to 100%. That is ALLSELECTED.

How do ALL and ALLSELECTED differ?

% of All = DIVIDE ( [Revenue], CALCULATE ( [Revenue], ALL ( 'Product' ) ) )

% of Selected = DIVIDE ( [Revenue], CALCULATE ( [Revenue], ALLSELECTED ( 'Product' ) ) )
ProductRevenue% of All% of Selected
Mountain-20090031%37%
Road-1501,20041%50%
Bottle32011%13%
Total2,42083%100%
A slicer selects three products; a table shows them

All products together make 2,920. % of All divides by 2,920; % of Selected by the 2,420 the slicer left.

What does Microsoft say it keeps?

From the reference: ALLSELECTED "retains all filters explicitly set within the query, and it retains all context filters other than row and column filters". With one argument it takes a table or a column; with more, columns of the same table. Without arguments, ALLSELECTED ( ) removes the row and column filters of every table, for the grand total of the visual.

Which other measures use the selection?

-- rank among the selected products
Rank in Selection = RANKX ( ALLSELECTED ( 'Product'[Product] ), [Revenue] )

-- average of the selected months
Avg Selected Month = AVERAGEX ( ALLSELECTED ( 'Date'[Month] ), [Revenue] )

-- running total over the selected dates
Running Revenue =
CALCULATE (
    [Revenue],
    FILTER (
        ALLSELECTED ( 'Date'[Date] ),
        'Date'[Date] <= MAX ( 'Date'[Date] )
    )
)

The running total starts at the first date in the date slicer, not at the first date in the model.

Where does ALLSELECTED surprise?

ALLSELECTED restores the filter context that was active outside the visual's iteration (the "shadow" filter context). Microsoft's reference describes it as above, and it behaves predictably in measures placed directly in visuals. It gets hard to reason about when:

  • it is used inside an iterator (SUMX with ALLSELECTED inside the expression), or
  • a measure with ALLSELECTED is called from another measure that iterates.

In those cases the "outer" context may be the iterator's, not the slicers'. Keep ALLSELECTED in top-level measures, and for complex cases use visual calculations (COLLAPSEALL), which refer to the visual's structure directly.

What goes wrong?

  • Using it in calculated columns. There is no visual or slicer at refresh; and in DirectQuery it is not supported in calculated columns or RLS rules.
  • ALLSELECTED on the wrong table. ALLSELECTED ( 'Product' ) restores product filters only; a region on the rows still filters.
  • Nesting in iterators. Results change in ways that are hard to follow; restructure.
  • Expecting slicers to be removed. They stay; use ALL or REMOVEFILTERS to ignore them.

Where can you check this?

Where do you start?

Put % of All and % of Selected side by side in a table with a product slicer, and select a few products. ALL, ALLEXCEPT and REMOVEFILTERS covers removing every filter.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.