Foundations › Power BI › DAX: Calculate Smartly
DAX: Calculate SmartlyFILTER, ALL, VALUES and SELECTEDVALUE
In the last lesson we changed a filter on a single column. Now we take finer control: filter with a condition on several columns, calculate a share of the total, and show smart text based on what the user selected.
| Function | Returns | Use it for |
|---|---|---|
| FILTER | A table of rows meeting a condition | Complex conditions on several columns |
| ALL | All rows/values with no filters | Grand totals or removing filters |
| VALUES | The distinct values in the current context | Counting or looping over visible values |
| SELECTEDVALUE | One value if exactly one is selected | Dynamic titles and KPIs |
FILTER: when the condition is not one column
A simple CALCULATE filter handles one column. When the condition spans two columns (large-quantity orders that are also expensive), use FILTER over the table and test every row.
Total Sales = SUM(FactSales[Sales])
Bulk Premium Sales = CALCULATE([Total Sales], FILTER(FactSales, FactSales[Quantity] >= 3 && FactSales[UnitPrice] >= 5000))
Electronics Bulk = CALCULATE([Total Sales], FILTER(FactSales, RELATED(DimProduct[Category]) = "Electronics" && FactSales[Quantity] >= 3))The first condition uses two FactSales columns. The second uses RELATED to fetch the category from the product table.
Advanced Advanced: FILTER(ALL(column)) versus a direct condition
CALCULATE([m], DimProduct[Category] <> "Home") is internally FILTER(ALL(DimProduct[Category]), …). Writing it out helps when the condition needs a measure, such as every product whose sales exceed one million.
ALL: share of the total
Total Sales = SUM(FactSales[Sales])
Category Total = CALCULATE([Total Sales], ALL(DimProduct[Category]))
Category Share = DIVIDE([Total Sales], [Category Total])Each row is a category: the numerator is the category, the denominator is all categories. Notice the year filter stayed because ALL removed the category filter only.
SELECTEDVALUE: a smart title
City Label = SELECTEDVALUE(DimCustomer[City], "Multiple Cities")We selected one city, so the result is its name. Try two cities or remove the filter to see «Multiple Cities».
City Label = SELECTEDVALUE(DimCustomer[City], "Multiple Cities")With no city filter there are four cities, so the fallback value came back.
Exercise 1 — Build a KPI named Answer: show the city name if exactly one city is selected, otherwise show the text Multiple Cities.
Exercise 2 — Write a measure named Answer for each category's share of the total of all categories (keeping other filters such as year or channel).
Exercise 3 — Write a measure named Answer that counts the products whose sales exceed one million in the current context.
Try it in real Power BI Desktop
- FILTER tests every row against conditions on several columns.
- ALL removes the filters of a column or table; use it for the denominator of a share.
- VALUES returns the values visible in the context.
- SELECTEDVALUE returns one value or a fallback: great for dynamic KPIs.
Need a real Power BI dashboard for your business?
I design a data model and an executive dashboard fitted to your business and your real data.