Sub topic 1
The Model Both Objects Live in
Calculated columns and measures are both written in DAX, and both live inside the same semantic model. Before comparing them, you need a clear picture of that model: its tables, its relationships, and the two ways DAX can "look at" the data. This chapter builds that picture. Later chapters rely on it.
Prerequisites
This chapter assumes you can do the following:
- Move between Report, Table and Model views in Power BI Desktop.
- Describe tables, columns and relationships, and recognize a star schema (fact tables surrounded by dimension tables).
- Read basic DAX such as
SUM,IFand column references likeSales[Quantity].
You do not need to know CALCULATE or context transition yet. Those come later.
What the model is
When you load data into Power BI Desktop, it is stored in an in-memory columnar engine (VertiPaq) as a semantic model. The model has three parts that matter for this topic:
- Tables. Each table is stored column by column, and each column is compressed on its own.
- Relationships. A relationship links a column in one table to a column in another. Most are one-to-many: one row in a dimension table matches many rows in a fact table.
- DAX objects. These are calculated columns, measures, calculated tables and calculation groups. This tutorial covers the first two.
Throughout the tutorial we use a small star schema with one fact table and one dimension table.
| Table | Role | Example columns |
|---|---|---|
| Sales | Fact table, one row per order line | Quantity, Unit Price, Profit, ProductKey |
| Product | Dimension table, one row per product | ProductKey, Category, Color, List Price |
The relationship runs from Product[ProductKey] (the "one" side) to Sales[ProductKey] (the "many" side). The relationship's default filter direction is single: filters flow from Product to Sales, not the other way.
Where each object is stored
Where an object lives tells you what it costs.
- A calculated column is a column added to a table. Its values are computed once, during refresh, and stored in the model like any imported column. It takes memory.
- A measure is a stored formula only. It has no values until a report asks for them. It is evaluated each time a visual needs it, and the result is not kept in the model.
Both are defined on a table, but a measure's home table is only an organizing choice. A measure can reference any table in the model.
Two kinds of context
DAX never evaluates an expression in isolation. Every expression runs inside a context, which answers the question "which rows am I looking at?" There are two kinds, and this tutorial depends on telling them apart.
Row context
Row context means "the current row." It exists when DAX walks a table one row at a time. In that state, a bare column reference such as Sales[Quantity] returns that row's value.
A calculated column always has row context. The engine evaluates the formula once for each row of its table.
Line Amount = Sales[Quantity] * Sales[Unit Price]For the row with Quantity 3 and Unit Price 20, the formula returns 60. For the next row, it uses that row's values. Nothing in the formula says "for each row"; the calculated column provides that automatically.
Filter context
Filter context means "the set of rows that are currently visible." Slicers, filters, visual axes, rows and columns of a matrix, and CALCULATE filter arguments all narrow the data. An aggregation function such as SUM then works only on the rows that survive.
A measure has filter context, supplied by whatever the report is showing at that moment.
Total Sales = SUM(Sales[Line Amount])Put this measure in a card and it sums every row. Put it in a table visual with Category on the rows, and it is evaluated once per category, each time over only that category's rows. The formula does not change. The filter context does.
The table below summarizes the difference.
| Aspect | Row context | Filter context |
|---|---|---|
| Meaning | One current row | A set of visible rows |
| Typical source | Calculated column, iterator function such as `SUMX` | Slicers, visual fields, report filters, `CALCULATE` |
| Bare column reference | Allowed, returns the row's value | Not allowed on its own, needs an aggregation |
| Typical use | Per-row arithmetic | Aggregation |
How the context moves through relationships
Filter context follows relationships in their filter direction. If a slicer selects Category = "Bikes" on the Product table, the filter passes along the one-to-many relationship and Sales shows only rows for bike products. That is why a measure on Sales can respond to a slicer built on Product.
Row context does not follow relationships by itself. In a calculated column on Sales, the current row has a ProductKey, but Product[Category] is not directly available. Reaching across needs RELATED:
Product Category = RELATED(Product[Category])RELATED follows the relationship from the many side to the one side, for the current row. A later chapter covers it in detail. The point here is that the model's relationships govern both contexts, in different ways.
Notice that a measure can read a stored calculated column. The two objects often work together, as when Total Sales sums the stored Line Amount column.
One question, two answers
The same business question, "what are the total sales?", can be answered either way. The calculated column stores the per-row product, and the measure sums it:
Line Amount = Sales[Quantity] * Sales[Unit Price]Total Sales = SUM(Sales[Line Amount])Or the measure can skip the column entirely. SUMX is an iterator: it creates a row context over Sales, evaluates the expression on each row, and adds the results.
Total Sales (no column) = SUMX(Sales, Sales[Quantity] * Sales[Unit Price])Both versions give the same number in every cell of a report. The first spends memory on a stored column and makes refresh slightly longer. The second spends a little more work at query time and stores nothing. This trade-off is the thread through the rest of the tutorial.
Common errors at this stage
- Aggregating inside a calculated column without meaning to.
Total = SUM(Sales[Line Amount])as a calculated column gives the same grand total on every row, because no filter context narrows it. It is valid DAX, but rarely what you want. - Using a bare column in a measure.
Bad = Sales[Quantity]fails or errors, because a measure has no current row. Wrap the column in an aggregation or an iterator. - Expecting a filter to flow upstream. With a single-direction relationship, a filter on Sales does not narrow Product. Measures on Product-side columns do not react to Sales-side filters the way you might expect.
- Assuming a measure's home table matters. Moving a measure to another table changes where it appears in the field list, not how it calculates.
Summary of what to carry forward
- The model stores tables and relationships. Relationships carry filters from the "one" side to the "many" side.
- A calculated column is computed per row at refresh time and stored. It uses row context.
- A measure is computed at query time for each visual cell and is not stored. It uses filter context.
- Iterator functions such as
SUMXlet a measure use row context temporarily. RELATEDlets row context reach across a relationship to the "one" side.
Exercise
A calculated column on Sales is defined as Total = SUM(Sales[Line Amount]). Every row shows the same value, the grand total. Why, and which object would give a per-category total in a matrix?
Show answer
A calculated column is evaluated in row context, and row context does not filter the table. SUM therefore sees all rows of Sales, so every row gets the grand total. A measure such as Total Sales = SUM(Sales[Line Amount]) is evaluated in the filter context of each matrix cell, so with Category on the rows it returns the per-category total.
Exercise
Sales has a many-to-one relationship to Product. In a calculated column on Product, you want the number of sales rows per product. Can you reference Sales[Quantity] directly as you would in a column on Sales? Explain in terms of context.
Show answer
No. The row context of the Product row does not automatically filter Sales, and a bare Sales[Quantity] is not a valid single value there. You would need an aggregation over Sales, and such an aggregation only responds to the current Product row after row context is converted into filter context, which is the context transition covered in a later chapter. RELATED works in the opposite direction, from Sales to Product.