ALLSELECTED Function (DAX)

 

Removes context filters from columns and rows in the current query, while retaining all other context filters or explicit filters.

The ALLSELECTED function gets the context that represents all rows and columns in the query, while keeping explicit filters and contexts other than row and column filters. This function can be used to obtain visual totals in queries.

ALLSELECTED([<tableName> | <columnName>])  

Parameters

tableName
The name of an existing table, using standard DAX syntax. This parameter cannot be an expression. This parameter is optional.

columnName
The name of an existing column using standard DAX syntax, usually fully qualified. It cannot be an expression. This parameter is optional.

The context of the query without any column and row filters.

  • This function takes one or no arguments. If there is one argument, the argument is either tableName or columnName.

  • This function is different from ALL() because it retains all filters explicitly set within the query, and it retains all context filters other than row and column filters.

The following example shows how to generate different levels of visual totals in a table report using DAX expressions. In the report two (2) previous filters have been applied to the Reseller Sales data; one on Sales Territory Group = Europe and the other on Promotion Type = Volume Discount. Once filters have been applied, visual totals can be calculated for the entire report, for All Years, or for All Product Categories. Also, for illustration purposes the grand total for All Reseller Sales is obtained too, removing all filters in the report. Evaluating the following DAX expression results in a table with all the information needed to build a table with Visual Totals.

define  
measure 'Reseller Sales'[Reseller Sales Amount]=sum('Reseller Sales'[Sales Amount])  
measure 'Reseller Sales'[Reseller Grand Total]=calculate(sum('Reseller Sales'[Sales Amount]), ALL('Reseller Sales'))  
measure 'Reseller Sales'[Reseller Visual Total]=calculate(sum('Reseller Sales'[Sales Amount]), ALLSELECTED())  
measure 'Reseller Sales'[Reseller Visual Total for All of Calendar Year]=calculate(sum('Reseller Sales'[Sales Amount]), ALLSELECTED('Date'[Calendar Year]))  
measure 'Reseller Sales'[Reseller Visual Total for All of Product Category Name]=calculate(sum('Reseller Sales'[Sales Amount]), ALLSELECTED('Product Category'[Product Category Name]))  
evaluate  
CalculateTable(  
    //CT table expression  
    summarize(   
//summarize table expression  
crossjoin(distinct('Product Category'[Product Category Name]), distinct('Date'[Calendar Year]))  
//First Group by expression  
, 'Product Category'[Product Category Name]  
//Second Group by expression  
, 'Date'[Calendar Year]  
//Summary expressions  
, "Reseller Sales Amount", [Reseller Sales Amount]  
, "Reseller Grand Total", [Reseller Grand Total]  
, "Reseller Visual Total", [Reseller Visual Total]  
, "Reseller Visual Total for All of Calendar Year", [Reseller Visual Total for All of Calendar Year]  
, "Reseller Visual Total for All of Product Category Name", [Reseller Visual Total for All of Product Category Name]  
)  
//CT filters  
, 'Sales Territory'[Sales Territory Group]="Europe", 'Promotion'[Promotion Type]="Volume Discount"  
)  
order by [Product Category Name], [Calendar Year]  

After executing the above expression in SQL Server Management Studio against AdventureWorks DW Tabular Model 2012 you obtain the following results:

[Product Category Name][Calendar Year][Reseller Sales Amount][Reseller Grand Total][Reseller Visual Total][Reseller Visual Total for All of Calendar Year][Reseller Visual Total for All of Product Category Name]
Accessories200080450596.9823877006.798738786.018
Accessories200180450596.9823877006.798738786.018
Accessories2002625.793380450596.9823877006.798738786.01891495.3104
Accessories200326037.313280450596.9823877006.798738786.018572927.0136
Accessories200412122.911580450596.9823877006.798738786.018212584.4747
Accessories200580450596.9823877006.798738786.018
Accessories200680450596.9823877006.798738786.018
Bikes200080450596.9823877006.7987689287.7939
Bikes200180450596.9823877006.7987689287.7939
Bikes200273778.93880450596.9823877006.7987689287.793991495.3104
Bikes2003439771.413680450596.9823877006.7987689287.7939572927.0136
Bikes2004175737.442380450596.9823877006.7987689287.7939212584.4747
Bikes200580450596.9823877006.7987689287.7939
Bikes200680450596.9823877006.7987689287.7939
Clothing200080450596.9823877006.798795090.7757
Clothing200180450596.9823877006.798795090.7757
Clothing200212132.433480450596.9823877006.798795090.775791495.3104
Clothing200358234.221480450596.9823877006.798795090.7757572927.0136
Clothing200424724.120980450596.9823877006.798795090.7757212584.4747
Clothing200580450596.9823877006.798795090.7757
Clothing200680450596.9823877006.798795090.7757
Components200080450596.9823877006.798753842.2111
Components200180450596.9823877006.798753842.2111
Components20024958.145780450596.9823877006.798753842.211191495.3104
Components200348884.065480450596.9823877006.798753842.2111572927.0136
Components200480450596.9823877006.798753842.2111212584.4747
Components200580450596.9823877006.798753842.2111
Components200680450596.9823877006.798753842.2111

The columns in the report are:

Reseller Sales Amount
The actual value of Reseller Sales for the year and product category. This value appears in a cell in the center of your report, at the intersection of year and catergory.

Reseller Visual Total for All of Calendar Year
The total value for a product category across all years. This value appears at the end of a column or row for a given product category and across all years in the report.

Reseller Visual Total for All of Product Category Name
The total value for a year across all product categories. This value appears at the end of a column or row for a given year and across all product categories in the report.

Reseller Visual Total
The total value for all years and product categories. This value usually appears in the bottom rightmost corner of the table.

Reseller Grand Total
This is the grand total for all reseller sales, before any filter has been applied; you should notice the difference with [Reseller Visual Total]. You do remember that this report includes two (2) filters, one on Product Category Group and the other in Promotion Type.

**Note**: if you have explicit filters in your expression, those filters are also applied to the expression.  

Community Additions

Show: