In DAX (Data Analysis Expressions), which is used in Power BI, Excel Power Pivot, and SQL Server Analysis Services, ALL
and ALLExcept
are functions used for manipulating the context of calculations, particularly in measures and calculated columns. These functions are primarily used to remove or modify filters in a DAX expression. Let’s delve into each function in detail.
ALL Function
The ALL
function is used to remove filters from one or more columns or tables. It essentially resets the filters on the specified columns or tables, making calculations ignore the current filter context for those columns or tables. This is particularly useful for creating measures like totals, percentages, and averages that are independent of the current filter context.
Syntax
DAX
ALL([<TableNameOrColumnName>])
Parameters
<TableNameOrColumnName>
: The name of the table or column from which you want to remove filters. This can be a single column, multiple columns, or an entire table.
Examples
- Removing Filters from a Single ColumnDAX
TotalSales := CALCULATE(SUM(Sales[Amount]), ALL(Sales[Region]))
This measure calculates the total sales amount ignoring any filters applied to the Sales[Region]
column.
Removing Filters from an Entire Table
DAX
TotalSales := CALCULATE(SUM(Sales[Amount]), ALL(Sales))
This measure calculates the total sales amount ignoring any filters applied to the entireSales
table.
ALLExcept Function
The ALLExcept
function is used to remove all filters from a table except for the ones explicitly specified. This is useful when you want to keep certain filters while removing others.
Syntax
DAX
ALLExcept(<Table>, <Column>[, <Column>[, ...]])
Parameters
<Table>
: The table from which you want to remove filters.<Column>
: One or more columns to retain their filters.
Example
- Retaining Filters on Specific ColumnsDAX
TotalSalesByRegion := CALCULATE(SUM(Sales[Amount]), ALLExcept(Sales, Sales[Region]))
This measure calculates the total sales amount while keeping any filters on theSales[Region]
column and removing filters from all other columns in theSales
table.
Use Cases and Differences
- ALL is typically used when you want to completely ignore filters on certain columns or an entire table. This is useful for creating measures that need a full data context, such as grand totals or percentages relative to the entire dataset.
- ALLExcept is used when you want to ignore all filters except for specific ones. This is helpful in scenarios where you need to maintain some filtering context (e.g., maintaining a regional filter while ignoring product categories).
Practical Examples
- Percentage of Total SalesDAX
PercentOfTotalSales :=
DIVIDE(
SUM(Sales[Amount]),
CALCULATE(SUM(Sales[Amount]), ALL(Sales))
)
This measure calculates the percentage of total sales for the current filter context compared to the total sales without any filters.
Sales Contribution by Region Ignoring Product Filters
DAX
SalesByRegion := CALCULATE( SUM(Sales[Amount]), ALLExcept(Sales, Sales[Region]) )
This measure calculates sales by region while ignoring any filters on product categories or other columns, focusing solely on regional contributions.
Understanding ALL
and ALLExcept
functions allows you to create more flexible and powerful measures in DAX by manipulating the filter context to meet specific analytical needs.
give some data to test thisChatGPT
Certainly! Here is a simple dataset to illustrate how you can use ALL
and ALLExcept
in DAX.
Sample Data
Imagine you have the following Sales
table:
Region | Product | Amount |
---|---|---|
North | A | 100 |
North | B | 200 |
South | A | 150 |
South | B | 300 |
East | A | 50 |
East | B | 100 |
West | A | 200 |
West | B | 400 |
Calculations
Total Sales Ignoring All Filters
Using the ALL
function to ignore all filters:
DAX
TotalSales := CALCULATE(SUM(Sales[Amount]), ALL(Sales))
This measure calculates the total sales amount across all regions and products, ignoring any filters applied.
Expected Result:
- TotalSales = 1500 (sum of all amounts in the table)
Total Sales by Region Ignoring Product Filter
Using the ALLExcept
function to ignore product filters but keep region filters:
DAX
SalesByRegion := CALCULATE(SUM(Sales[Amount]), ALLExcept(Sales, Sales[Region]))
This measure calculates the total sales amount for each region, ignoring any filters on the Product
column.
Expected Results:
- For Region “North”: SalesByRegion = 300 (100 + 200)
- For Region “South”: SalesByRegion = 450 (150 + 300)
- For Region “East”: SalesByRegion = 150 (50 + 100)
- For Region “West”: SalesByRegion = 600 (200 + 400)
Percentage of Total Sales
Using ALL
to calculate the percentage of total sales for each row:
DAX
PercentOfTotalSales :=
DIVIDE(
SUM(Sales[Amount]),
CALCULATE(SUM(Sales[Amount]), ALL(Sales))
)
This measure calculates the percentage of total sales for the current filter context compared to the total sales without any filters.
Expected Results (for each row in the Sales table):
- For (North, A): 100 / 1500 = 0.0667 (or 6.67%)
- For (North, B): 200 / 1500 = 0.1333 (or 13.33%)
- For (South, A): 150 / 1500 = 0.10 (or 10%)
- For (South, B): 300 / 1500 = 0.20 (or 20%)
- For (East, A): 50 / 1500 = 0.0333 (or 3.33%)
- For (East, B): 100 / 1500 = 0.0667 (or 6.67%)
- For (West, A): 200 / 1500 = 0.1333 (or 13.33%)
- For (West, B): 400 / 1500 = 0.2667 (or 26.67%)
Applying Filters in a Pivot Table
Let’s assume you create a Pivot Table in Power BI or Excel Power Pivot using the Sales
table.
- TotalSales Measure: Drag the
TotalSales
measure into the Values area. This will show 1500 for all rows and columns because it ignores all filters. - SalesByRegion Measure: Drag the
Region
column to Rows and theSalesByRegion
measure to Values. This will show the sales totals for each region, ignoring product filters. - PercentOfTotalSales Measure: Drag the
Region
column to Rows andProduct
column to Columns, and then drag thePercentOfTotalSales
measure to Values. This will show the percentage contribution of each region and product combination to the total sales.
By setting up these calculations and visualizations, you can test and verify how ALL
and ALLExcept
work to manipulate filter contexts in your DAX expressions.