Gomaa Intelligence.Data Hub
عربي

Foundations › Power BI › DAX: Calculate Smartly

DAX: Calculate Smartly

FILTER, ALL, VALUES and SELECTEDVALUE

Lesson 8 / 14· 10 min

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.

FunctionReturnsUse it for
FILTERA table of rows meeting a conditionComplex conditions on several columns
ALLAll rows/values with no filtersGrand totals or removing filters
VALUESThe distinct values in the current contextCounting or looping over visible values
SELECTEDVALUEOne value if exactly one is selectedDynamic 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.

▶ Try DAX
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

▶ Try DAX
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

▶ Try DAX
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».

▶ Try DAX
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

Summary
  • 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.

Request an analytics service ↗
Get your data analyzed by an expert ↗