Sub topic 5
Measures: aggregations computed in the current context
This chapter assumes you can navigate Power BI Desktop, know what a star-schema model is, and can read basic DAX such as SUM and IF. It also assumes you have met row context and filter context in the previous chapters.
What a measure is
A measure is a DAX formula that Power BI saves in the model as a definition. It stores no values. Power BI runs the formula each time a visual asks for it, and it runs it once for every filter combination in that visual.
A measure always returns one value for the context it is evaluated in. Place it in a card and it returns one number for the whole report. Place it in a table with a row per product and it runs once for each product.
A first measure
The earlier chapter created a calculated column called Line Amount. A measure can add that column up:
Total Sales = SUM(Sales[Line Amount])SUM reads the Line Amount values for whatever rows the current filter context leaves visible. In a visual grouped by Product[Category], the filter context for the "Bikes" row keeps only sales of bikes. The grand total row has no category filter, so it sums every row.
A measure without a stored column
You can get the same total without a calculated column. An iterator function such as SUMX creates a row context over a table and sums an expression for each row:
Total Sales (no column) = SUMX(Sales, Sales[Quantity] * Sales[Unit Price])Here the multiplication happens at query time. Nothing is added to the model's storage, so a later chapter on memory cost will favor this form.
The two formulas use both kinds of context. SUMX works row by row inside a row context. The filter context first decides which rows of Sales SUMX visits.
Changing the filter context with CALCULATE
A measure can also change the filter context it was given. CALCULATE evaluates an expression after applying extra filters:
Red Sales = CALCULATE([Total Sales], Product[Color] = "Red")In a visual by year, each row still filters by its own year. CALCULATE adds a second filter on color. The result is red sales for that year.
Reusing measures
Notice that Red Sales refers to [Total Sales]. A measure can call another measure, so you define the base calculation once and build on it. When you change the base, every dependent measure follows.
Implicit and explicit measures
If you drag a numeric column such as Sales[Quantity] onto a visual, Power BI summarizes it automatically. This is an implicit measure. It works, but you cannot reuse it in other DAX formulas, and it has no name you control.
An explicit measure is one you write yourself, as in the examples above. Prefer explicit measures for anything you will reuse or explain to others.
| Feature | Implicit measure | Explicit measure |
|---|---|---|
| Created by | Dragging a column into a visual | Writing a DAX formula |
| Reusable in other DAX | No | Yes |
| Can use `CALCULATE` and filters | No | Yes |
Common errors
- Referencing a column without an aggregation. Writing
Total = Sales[Line Amount]in a measure fails, because a measure has no row context. Wrap the column inSUM, or use an iterator. - Expecting a measure on a slicer or axis. A measure returns a calculated value, not a field with discrete values to group by. For that you need a column.
- Putting a measure in a column's place. A measure cannot be used as a row-level value in another table.
Exercise
A measure Total Sales = SUM(Sales[Line Amount]) shows 500 for the "Bikes" row and 1,200 for the grand total row of a table visual. Why does the same formula return different numbers, and is any value stored in the model for either row?
Show answer
The formula is evaluated separately for each row of the visual, and each evaluation has a different filter context. The "Bikes" row filters Sales to bikes, so the sum covers only those rows. The grand total row has no category filter, so it sums all rows. No result is stored. Only the formula definition is saved, and Power BI recalculates the values at query time.