Sub topic 1
What a measure is and how it differs from a calculated column
Prerequisites
This chapter assumes you have the following:
- You can find your way around Power BI Desktop: Report view, the Fields pane, and adding a visual.
- A data model with at least one loaded table. The examples use a
Salestable with numeric columns such asSalesAmountandTotalCost. - You know what tables, columns, and rows are.
You don't need any DAX (Data Analysis Expressions, the formula language of Power BI) yet.
Two ways to calculate in DAX
DAX lets you add calculations to a model in two main ways: calculated columns and measures. They look similar, because both use a name, an equals sign, and a formula. They behave very differently, and picking the wrong one is a common beginner mistake.
A calculated column adds a new column to a table. DAX evaluates the formula once for every row and stores the results in the model.
Line Margin = Sales[SalesAmount] - Sales[TotalCost]This column holds one value per row of Sales. It is calculated when you load or refresh the data, and the values then take up memory like any imported column. You can place the column on an axis, in a slicer, or in a filter, because it is real data in the table.
A measure is a formula that Power BI evaluates when a visual needs it. It does not add a column and does not store a result per row. It returns a single value for whatever slice of data the visual is showing.
Total Sales = SUM(Sales[SalesAmount])You will build this measure step by step in the next chapters. For now, note that Total Sales produces no column of values. It produces a result each time a visual asks for it.
When each one runs
The timing is the key difference.
The consequences are practical:
- A calculated column grows the file size and refresh time, because it stores one value for every row.
- A measure adds almost nothing to file size. It costs calculation time when a report page renders.
- A measure responds to slicers and other visuals. A calculated column cannot, because its values were fixed at refresh.
A side-by-side comparison
| Feature | Calculated column | Measure |
|---|---|---|
| Evaluated | At data refresh, once per row | At query time, when a visual needs it |
| Result | One value per row, stored in the table | One value per filter context, not stored |
| Reacts to slicers and filters | No | Yes |
| Can be used on an axis or in a slicer | Yes | No |
| Typical use | Row-level labels, categories, sort keys | Totals, counts, ratios, KPIs |
Seeing the difference
Suppose Sales has 10,000 rows and you add a table visual with a Region field and the Total Sales measure. The measure runs once per region, each time on only that region's rows. If you then select a year in a slicer, it runs again on the narrower set of rows.
Now suppose you had tried to do this with a calculated column. The column would have 10,000 stored values and would not respond to the slicer. Putting it in a visual would make Power BI aggregate it, usually by summing, which is really a measure applied behind the scenes. Chapter 10 returns to why you should write that measure yourself.
Choosing between them
A practical rule works for most cases:
- If you need a value for each row, or need to group or filter by the result, use a calculated column.
- If you need an aggregate that changes as the user filters the report, use a measure.
- If either would work, prefer a measure. It keeps the model smaller and the report more flexible.
Where measures live
A measure belongs to a table in the model, but it is not tied to that table's rows. You can store it in Sales or in a dedicated table for measures. In the Fields pane, measures show a calculator icon, which helps you tell them from columns. Chapter 9 covers naming and organizing them.
Exercise
A report has a Sales table with SalesAmount and TotalCost columns. You need a number that shows total profit and updates when the user picks a year in a slicer. Should you use a calculated column or a measure, and why?
Show answer
Use a measure, such as Total Profit = SUM(Sales[SalesAmount]) - SUM(Sales[TotalCost]). A measure is evaluated in the current filter context, so it recalculates when the slicer changes. A calculated column is fixed at refresh and does not respond to slicers.
Exercise
Name one situation where a calculated column is the better choice.
Show answer
When you need a per-row value to group or filter by, such as a category label built from other columns or a sort key. A measure cannot be placed on an axis or in a slicer, but a column can.