Sub topic 5
Measures: aggregations computed in the current context
A measure is a named DAX expression that Power BI evaluates when a visual asks for it. It stores no values in the model. It stores the formula, and each cell of a visual gets its own result, calculated from the rows that cell can see. This chapter covers how a measure gets its answer, why one formula can return different numbers in different places, and what to watch for when you write measures.
This chapter assumes you can move around Power BI Desktop, you know a star-schema layout (fact table, dimension tables, one-to-many relationships), and you can read simple DAX such as SUM and IF. Row context and filter context have their own chapters. Here they are used as working ideas.
What a measure is
A measure is defined once and calculated many times. You write it in the model, and it appears in the Fields pane with a calculator icon. When you drop it into a visual, Power BI runs the formula once for every cell the visual needs.
Total Sales = SUM(Sales[Line Amount])SUM is an aggregator. It takes a column and adds up its values over the rows that are currently visible. The measure has no fixed answer. The answer depends on which rows are visible when the formula runs.
In the Sales model used throughout this tutorial, Line Amount is a calculated column on the Sales table. That is fine here. The measure only reads the stored column and adds it up.
Filter context decides which rows count
The set of rows a measure can see is its filter context. Power BI builds it from everything that narrows the data at that point:
- Rows and columns of a table or matrix, and the axis and legend of a chart.
- Slicers on the page.
- Visual-level, page-level and report-level filters.
- Filters passed along relationships from dimension tables to the fact table.
Take a matrix with Product[Category] on rows and Calendar[Year] on columns, and Total Sales as the value. For the cell "Bikes, 2025", the filter context is Category = Bikes and Year = 2025. Power BI keeps only the Sales rows that match, and SUM adds up those rows. The cell next to it has a different filter context, so the same formula gives a different number.
The Total row has no filter from the row headers, so SUM covers every row that survives the slicers and page filters. You do not write a separate formula for subtotals.
Evaluated at query time
A measure is calculated at query time, which means when a report page loads or a user changes a slicer. Nothing is calculated for it during data refresh. The model holds only the text of the formula.
This has three consequences:
- A new measure adds almost nothing to the file size, because no column of values is stored.
- A measure always reflects the current filters, including slicer choices made a second ago.
- A slow measure makes visuals slow, because its cost is paid every time a visual renders. Calculated columns shift that cost to refresh time instead.
The next chapter compares refresh time and query time directly. For now, keep the picture that a measure is a recipe and each cell is a separate cooking.
Iterators: a measure without a helper column
SUM works on one column. When the value you need comes from combining columns row by row, you have two options. One is a calculated column such as Line Amount, which you then sum. The other is an iterator, a function whose name ends in X (SUMX, AVERAGEX, MAXX). An iterator walks through a table one row at a time, evaluates an expression on each row, and then aggregates the results.
Total Sales (no column) = SUMX(Sales, Sales[Quantity] * Sales[Unit Price])SUMX takes two arguments. The first is the table to walk, which here is the Sales rows left visible by the filter context. The second is the expression to evaluate for each row. Inside that expression Sales[Quantity] means "the Quantity of the row being visited". That is row context, and SUMX creates it. The measure as a whole still runs in filter context, which decides which Sales rows SUMX visits in the first place.
Both Total Sales and Total Sales (no column) return the same figure for any cell. The second needs no stored column.
Ratios belong in measures
Measures are the right place for ratios, percentages and averages, because the ratio must be calculated from totals for the current filter context.
Margin % = DIVIDE(SUM(Sales[Profit]), SUM(Sales[Line Amount]))Take two products. Product A has profit 10 on sales of 100. Product B has profit 90 on sales of 900. Each has a margin of 10%. Now add a third row: Product C has profit 50 on sales of 100, a 50% margin. The correct overall margin is total profit (150) divided by total sales (1,100), which is about 13.6%. Averaging the three row-level percentages gives 23.3%. The average is wrong because it treats a 100-unit sale and a 900-unit sale as equally important.
The measure divides totals at every level. In the cell for Product C it gives 50%, and in the grand total it gives 13.6%.
DIVIDE is used instead of the / operator because it handles a zero denominator. By default it returns blank when the denominator is zero or blank. A blank cell is usually better than an error or infinity in a report.
Changing the filter context with CALCULATE
A measure can also adjust the context it runs in. CALCULATE evaluates an expression after changing the filters.
Red Sales = CALCULATE([Total Sales], Product[Color] = "Red")Two details matter here. First, [Total Sales] in square brackets with no table name is a reference to another measure. Measures can build on each other, so you define logic once and reuse it. Second, the Product[Color] = "Red" argument replaces any existing filter on Product[Color]. If a slicer has Blue selected, Red Sales still returns red sales, because the explicit filter overrides the slicer on that column. Filters on other columns, such as Year, still apply.
How CALCULATE interacts with row context is covered in the context transition chapter.
Implicit and explicit measures
If you drag a numeric column such as Sales[Quantity] into a visual, Power BI sums it for you. This is an implicit measure. It works, but it has limits:
- It is not named or reusable, and you cannot reference it from other DAX.
- The aggregation can be changed in the visual (Sum, Average, Count), so two report pages can show different results for the same field.
- It cannot be used in more elaborate logic.
An explicit measure is one you write yourself, like Total Sales. Prefer explicit measures for any number users rely on. Many teams also hide the raw numeric columns once the measures exist, so report authors pick the measure and not the column.
| Aspect | Implicit measure | Explicit measure |
|---|---|---|
| Created by | Dragging a numeric column into a visual | Writing a DAX formula in the model |
| Reusable in other DAX | No | Yes |
| Aggregation fixed by the author | No, can be switched per visual | Yes |
| Appears in the model | No | Yes, with a calculator icon |
Common errors
- Using a bare column in a measure.
Bad = Sales[Line Amount]fails with an error saying a single value cannot be determined. A measure has no row context, so a column reference outside an aggregator or iterator is ambiguous. Wrap it:SUM(Sales[Line Amount]). - Summing a ratio column. If
Margin %exists as a calculated column, summing or averaging it gives a wrong overall rate for the reason shown above. Write the ratio as a measure. - Expecting a measure on an axis or slicer. A measure returns a value per cell, not a field with a list of values, so you cannot use it to slice. A column is needed for that, which a later chapter covers.
- Repeating logic. Copying the same
SUMXformula into five measures makes changes risky. Define a base measure once and build on it with[Total Sales]. - Ignoring blanks. A measure returns blank when no rows match. Visuals normally hide blank rows, which is often helpful. Adding
+ 0forces a zero and can make a visual show every combination of categories, which slows it down.
Exercises
Exercise
A matrix shows Total Sales by Category. The cell for "Accessories" shows 40,000 and the Total row shows 250,000. You add a slicer for Year and pick 2025. Explain, in terms of filter context, why both numbers can change without anyone editing the formula.
Show answer
The measure runs again for each cell with a new filter context. Each cell now has the Year = 2025 filter from the slicer as well as its own row filter, so SUM only adds the 2025 Sales rows. The Accessories cell keeps its Category filter plus the year. The Total row has no category filter, so it totals all categories for 2025. The formula stays the same, and only the context changes.
Exercise
Write a measure called Average Line Amount that returns the average of Quantity times Unit Price over the visible Sales rows, without using the Line Amount column.
Show answer
Average Line Amount = AVERAGEX(Sales, Sales[Quantity] * Sales[Unit Price]) AVERAGEX walks the visible Sales rows, evaluates the product on each row using the row context it creates, and returns the average of those results.
Exercise
A colleague stored Margin % as a calculated column on Sales and uses Average of Margin % in a visual. The grand total looks too high. Why, and what is the fix?
Show answer
Averaging the column gives every row the same weight, so many small high-margin rows pull the result up, even though they contribute little profit. The fix is a measure: Margin % = DIVIDE(SUM(Sales[Profit]), SUM(Sales[Line Amount])). It divides the totals for the current filter context, so it is correct at every level.