FILTER function (DAX)
Returns a table that represents a subset of another table or expression.
|table||The table to be filtered. The table can also be an expression that results in a table.|
|filter||A Boolean expression that is to be evaluated for each row of the table. For example,
A table containing only the filtered rows.
You can use FILTER to reduce the number of rows in the table that you are working with, and use only specific data in calculations. FILTER is not used independently, but as a function that is embedded in other functions that require a table as an argument.
The following example creates a report of Internet sales outside the United States by using a measure that filters out sales in the United States, and then slicing by calendar year and product categories. To create this measure, you filter the table, Internet Sales USD, by using Sales Territory, and then use the filtered table in a SUMX function.
In this example, the expression
FILTER('InternetSales_USD', RELATED('SalesTerritory'[SalesTerritoryCountry])<>"United States") returns a table that is a subset of Internet Sales minus all rows that belong to the United States sales territory. The RELATED function is what links the Territory key in the Internet Sales table to SalesTerritoryCountry in the SalesTerritory table.
The following table demonstrates the proof of concept for the measure, NON USA Internet Sales, the formula for which is provided in the code section below. The table compares all Internet sales with non- USA Internet sales, to show that the filter expression works, by excluding United States sales from the computation.
To re-create this table, add the field, SalesTerritoryCountry, to the Row Labels area of the PivotTable.
Table 1. Comparing total sales for U.S. vs. all other regions
|Row Labels||Internet Sales||Non USA Internet Sales|
The final report table shows the results when you create a PivotTable by using the measure, NON USA Internet Sales. Add the field, CalendarYear, to the Row Labels area of the PivotTable and add the field, ProductCategoryName, to the Column Labels area.
Table 2. Comparing non- U.S. sales by product categories
|Non USA Internet Sales||Column Labels|
|Row Labels||Accessories||Bikes||Clothing||Grand Total|
SUMX(FILTER('InternetSales_USD', RELATED('SalesTerritory'[SalesTerritoryCountry])<>"United States") ,'InternetSales_USD'[SalesAmount_USD])