Sub topic 6
Building measures from other measures
This chapter assumes you can open Power BI Desktop, select a table in the Fields pane, and create a measure from the formula bar. It also assumes you have a Sales table with a numeric SalesAmount column, where each row is one order line. You should already know how SUM and COUNTROWS work. Here you will learn how one measure can call another, why that is worth doing, and what can go wrong.
Why reference one measure from another
A measure is a named DAX expression. Once it has a name, any other measure can use that name in its own formula. You write the name inside square brackets, such as [Total Sales].
Suppose you have two measures:
Total Sales = SUM(Sales[SalesAmount])Order Lines = COUNTROWS(Sales)You want the average sales amount per order line. You could write the whole calculation again:
Average Sales per Order Line (inline) = SUM(Sales[SalesAmount]) / COUNTROWS(Sales)This works, but it copies the logic of the first two measures. If you later change what "total sales" means, for example by excluding returns, you must find and edit every copy. Referencing the existing measures avoids that:
Average Sales per Order Line = DIVIDE([Total Sales], [Order Lines])Now Total Sales is defined in one place. Every measure that uses it picks up a change automatically.
The benefits are:
- One definition per business rule. You fix a rule once.
- Shorter formulas.
DIVIDE([Total Sales], [Order Lines])is easier to read than the expanded form. - Consistent numbers. Two reports cannot disagree about "total sales" if both use the same measure.
- Easier testing. You can put each building block in a card visual and check it on its own.
Reference syntax
DAX distinguishes between columns and measures by how you write them.
| What you reference | How you write it | Example |
|---|---|---|
| A column | Table name, then column name in brackets | `Sales[SalesAmount]` |
| A measure | Measure name in brackets, no table name | `[Total Sales]` |
Power BI lets you write Sales[Total Sales] for a measure that lives in the Sales table, but that style is discouraged. A reader cannot tell whether it is a column or a measure. Writing [Total Sales] alone signals a measure, and writing Sales[SalesAmount] signals a column. Keep that convention in every formula you write.
Measure names must be unique across the whole model. Column names only need to be unique within their own table.
A worked example
Here is the chain of measures built so far in this tutorial, with each step depending on the earlier ones.
Total Sales = SUM(Sales[SalesAmount])Order Lines = COUNTROWS(Sales)Average Sales per Order Line = DIVIDE([Total Sales], [Order Lines])To see this in action:
- Create the three measures in the order shown. Select the
Salestable in the Fields pane, choose New measure on the Home ribbon, and type each formula. - Add a Card visual and drop
Total Salesinto it. Repeat forOrder LinesandAverage Sales per Order Line. - Add a Slicer on a field such as
Sales[Year]or a product category, if your model has one. - Click different slicer values. All three cards change together.
The third card is not storing a number. It recalculates each time because the two measures it references recalculate first. This leads to an important idea.
References inherit the current filter context
When a measure calls another measure, the called measure is evaluated in the same filter context as the caller. Filter context is the set of filters from slicers, visuals, and report filters that apply to a calculation at that moment.
Consider a table visual with Product[Category] in the rows and Average Sales per Order Line as a value. For the row "Bikes":
- Power BI applies the filter
Category = Bikes. [Total Sales]is evaluated under that filter and returns the sum for bikes.[Order Lines]is evaluated under that same filter and returns the bike row count.DIVIDEdivides the two results.
The same formula gives a different answer in every row, because the filter context differs in every row.
The result is a ratio of two numbers computed over the same set of rows. That is almost always what you want. Dividing a total for one filter by a count for a different filter produces a number that means nothing. Later chapters show how to change the filter context deliberately. For now, the key point is that a plain reference does not change it.
Measures that build on measures that build on measures
References can nest. A measure can call a measure that calls another measure. You might extend the model like this:
Large Order Lines = COUNTROWS(FILTER(Sales, Sales[SalesAmount] > 1000))Large Order Line Share = DIVIDE([Large Order Lines], [Order Lines])Large Order Line Share depends on Large Order Lines and Order Lines. Each of those depends on the Sales table. This forms a chain of dependencies:
Chains like this are normal in real models. They let you build a complex result out of small, named, testable parts. Keep two habits:
- Give each step a clear name so the formula reads like a sentence.
- Test each step in a card before you build on it. If the final number looks wrong, you can then check each input.
Common errors
Circular dependencies
A measure cannot depend on itself, directly or through other measures. If A references B and B references A, Power BI reports a circular dependency error and refuses to save the formula. The fix is to redraw your dependencies so they flow in one direction, from simple measures at the bottom to composed measures at the top.
Misspelled or renamed measures
If you type [Total Sale] instead of [Total Sales], DAX cannot find a column or measure with that name and shows an error in the formula bar. As you type, Power BI offers a list of matching names. Select from it instead of typing the whole name.
If you rename a measure in the Fields pane or the formula bar, Power BI updates the formulas of other measures that reference it. Renaming in the model is therefore safe. Editing a name by hand inside another formula is not.
Mixing up a column and a measure
If you write [SalesAmount] while meaning the column, DAX looks for a measure with that name and fails. If you write Sales[SalesAmount] alone as a measure, DAX raises an error because a column reference cannot be turned into a single value without an aggregation such as SUM. Write SUM(Sales[SalesAmount]) or reference a measure that already does so.
Wrapping an already aggregated value in another aggregation
[Total Sales] returns one number for the current filter context. Writing SUM([Total Sales]) is an error, because SUM expects a column, not a value. Use the measure's name directly wherever you need its value.
Averaging averages
A ratio measure does not combine correctly across groups by plain addition or averaging. Suppose Bikes has an average of 500 per line and Accessories has an average of 50. The overall average is not 275, because the groups have different numbers of lines. The overall average per line is total sales divided by total lines. Because Average Sales per Order Line divides the two totals under whatever filter applies, the grand total row of a table visual is correct. If you instead wrote a separate average for each group and combined them yourself, the result would be wrong. This is another reason to compose ratios from base measures.
Why DIVIDE fits composed measures
Composed measures often divide one measure by another, and the denominator can be zero or blank when a filter leaves no rows. Using DIVIDE handles that safely. By default it returns BLANK when the denominator is zero or blank, which keeps visuals clean. You can set a different result with the third argument:
Average Sales per Order Line (0 if none) = DIVIDE([Total Sales], [Order Lines], 0)The next chapters cover this behavior in detail. For now, remember that a referenced measure can itself return BLANK, and DIVIDE treats a blank denominator the same as zero.
Practical guidance
- Build from the bottom up. Create base measures (
SUM,COUNTROWS) first, then ratios and shares. - Name by meaning.
Total Salesis better thanSum1. A name that describes the business concept makes dependent formulas readable. - Do not reference a measure when a simple base is clearer. If a measure is used by only one other formula and adds no meaning, an inline expression may be easier to follow. Reuse is the goal, not nesting for its own sake.
- Keep the chain shallow enough to follow. Chains of three or four levels are common. If you need to open ten measures to understand one number, consider simplifying.
- Watch performance only after correctness. Referencing a measure does not copy a stored number. The engine evaluates it each time it is used, but the engine is designed for this pattern, and the clarity benefit usually outweighs any cost for the measures in this tutorial.
Summary of the idea
A measure can use another measure by writing its name in square brackets. The inner measure is evaluated in the same filter context as the outer one, so a ratio of two measures compares two numbers over the same set of rows. Define each business rule once, build on it, and avoid circular references.
Exercise
You have the measures Total Sales = SUM(Sales[SalesAmount]) and Order Lines = COUNTROWS(Sales). A colleague writes Average Sales per Order Line = DIVIDE(SUM(Sales[SalesAmount]), COUNTROWS(Sales)). The result is correct. Give two reasons to rewrite it as DIVIDE([Total Sales], [Order Lines]).
Show answer
The rewrite keeps each business rule in one place, so a later change to Total Sales (for example excluding returns) automatically flows into the average. It is also shorter and easier to read, and the inputs can be checked separately in card visuals.
Exercise
A table visual shows Product[Category] in rows and Average Sales per Order Line as a value. In the "Bikes" row, which filter context is used when [Total Sales] and [Order Lines] are evaluated, and why does that matter for the result?
Show answer
Both measures are evaluated under the filter Category = Bikes, plus any slicers and report filters. This matters because the numerator and denominator then cover the same set of rows, so the ratio is a meaningful average for bikes.
Exercise
Write a measure named Large Order Line Share that returns the fraction of order lines with a SalesAmount above 1000, using the measure Large Order Lines = COUNTROWS(FILTER(Sales, Sales[SalesAmount] > 1000)) and the measure Order Lines. Explain what it returns when a slicer leaves no rows.
Show answer
Large Order Line Share = DIVIDE([Large Order Lines], [Order Lines]). When a slicer leaves no rows, [Order Lines] is zero or blank, so DIVIDE returns BLANK instead of an error. Adding a third argument, such as 0, would return that value instead.