Sub topic 13
A first measure with DAX
This chapter shows how to write a first measure in Power BI Desktop, where it appears, and how it differs from a column. It assumes you have already loaded a table named Sales that has a numeric SalesAmount column. It also assumes you can switch between Report, Table, and Model views and add a visual to the canvas. No prior DAX knowledge is needed.
What a measure is
DAX (Data Analysis Expressions) is the formula language of Power BI. A measure is a named DAX formula that calculates a single value, such as a total or a count. Power BI evaluates the formula when a visual needs it, not when the data loads.
Compare this with a column. A column holds one stored value per row. A measure holds no rows. Each time a visual is drawn, Power BI runs the formula once for every cell the visual needs, using the filters that apply to that cell.
| Feature | Column | Measure |
|---|---|---|
| Calculated when | Data is loaded or refreshed (calculated columns) | A visual is drawn or a filter changes |
| Result stored in the model | Yes, one value per row | No, only the formula is stored |
| Shown in Table view | Yes | No |
| Typical use | Grouping, slicing, row-level values | Totals, counts, ratios |
Why not just drag the column?
If you drag a numeric column such as SalesAmount into a visual, Power BI sums it for you. This is an implicit measure, and the Σ icon beside the column shows that default summarization is on. It works, but it has limits:
- You can't reuse it inside other formulas.
- Its name and number format are tied to one visual.
- It can only apply the aggregation that the field menu offers.
An explicit measure is defined once, named, formatted, and reused everywhere. Other measures can also build on it.
Creating the measure
You can create a measure from any of the three views. The result is the same.
- Open Report view and select the
Salestable in the Data pane. - On the Home ribbon, select New measure. The formula bar opens above the canvas.
- Type the formula and press Enter.
Total Sales = SUM(Sales[SalesAmount])The formula has three parts:
Total Salesis the measure name, followed by=.SUMis an aggregation function. It takes one column as its argument.Sales[SalesAmount]is a column reference in the formTable[Column].
The measure appears under Sales in the Data pane with a calculator icon. To check it, drag it into a Card visual. The card shows the total of every SalesAmount value in the table.
Create a second measure in the same way:
Order Count = COUNTROWS(Sales)COUNTROWS takes a table and returns the number of rows in it, after any filters have been applied. If you create this measure in Table view or Model view, it still shows up in the Data pane in every view. Measures belong to the model, not to a view.
How a measure gets its answer
A measure returns different numbers in different places because of filter context. This is the set of filters that apply to the cell being calculated. Filters come from the rows and columns of a visual, from slicers, from the Filters pane, and from relationships in the model.
Suppose you place Total Sales in a table visual next to a product category. For the "Bikes" row, Power BI filters Sales down to Bikes rows and then runs SUM. For the "Accessories" row, the same formula runs against a different filtered set.
This explains why the model's relationships matter for measures. A category filter on a Product table can only reduce Sales rows if a relationship connects the two tables, as covered in the Model view chapters.
Building on existing measures
A measure can call another measure. You refer to a measure by its name in square brackets, with no table name:
Average Order Value = DIVIDE([Total Sales], [Order Count])DIVIDE returns blank instead of an error when the denominator is zero or blank. This is safer than writing [Total Sales] / [Order Count].
The bracket convention helps you read DAX:
Sales[SalesAmount]has a table name, so it is a column.[Total Sales]has no table name, so it is a measure.
Microsoft's guidance is to follow this convention, and the formula bar's suggestions rely on it.
Formatting the result
A new measure returns a plain number. To show it as currency or with a thousands separator, select the measure in the Data pane and use the Measure tools ribbon. There you can set the format (for example Currency), the number of decimal places, and the home table. Formatting belongs to the measure, so every visual that uses it picks up the change.
The home table is the table that lists the measure in the Data pane. A measure does not need to be tied to the table it calculates over. You can change its home table in the Properties pane of Model view. Many modelers keep measures in a dedicated table so they are easy to find.
Common errors and gotchas
- Using
SUMon an expression.SUMaccepts only a column.SUM(Sales[Quantity] * Sales[UnitPrice])fails. Use the iteratorSUMX(Sales, Sales[Quantity] * Sales[UnitPrice]), which evaluates the expression for each row and then adds the results. - Putting a measure where a column is expected. You can't use a measure as an axis or as a slicer field. Measures belong in the Values area.
- Counting the wrong thing.
COUNTROWS(Sales)counts rows. If each row is an order line, the result is the number of lines, not the number of orders. To count distinct orders, useDISTINCTCOUNT(Sales[OrderID]), assuming the table has anOrderIDcolumn. - Name clashes. Measure names must be unique across the whole model, even in different tables. A measure also can't share a name with a column in its own table.
- Expecting a measure in Table view. Table view shows stored data. A measure has no per-row values, so it does not appear as a column there.
- Blank results. A measure shows blank in a cell when no rows survive the filters. This is expected and not a fault in the formula.
Checking your work
A quick test confirms that a measure behaves as expected:
- Add a Card visual with
Total Sales. - Add a table visual with a category column and
Total Sales. - Check that the category values add up to the card value. They may not if some rows have a blank category.
- Select a slicer value and watch both visuals change. The change shows that the measure is following the filter context.
Exercise
Your Sales table has Quantity and UnitPrice columns but no revenue column. Write a measure named Revenue that returns total revenue, and explain why SUM(Sales[Quantity] * Sales[UnitPrice]) doesn't work.
Show answer
Use Revenue = SUMX(Sales, Sales[Quantity] * Sales[UnitPrice]). SUM accepts only a single column reference, so it can't evaluate a row-by-row multiplication. SUMX is an iterator. It walks through each row of Sales, evaluates the expression, and adds up the results.
Exercise
A table visual shows Total Sales by category, and the Bikes row shows a different number from the card visual on the same page. The formula is SUM(Sales[SalesAmount]) in both places. Why is this correct behavior?
Show answer
The two visuals have different filter contexts. The Bikes row filters Sales to Bikes rows before the formula runs. The card has no category filter, so it sums every row. The formula is the same, but the rows it sees are different.