Sub topic 4
Calculated columns: new columns computed row by row
A calculated column adds a new column to a table in your model. DAX evaluates its formula once for every row, and it stores the results. This chapter assumes you can move around Report, Table and Model views, understand relationships in a star schema, and know basic DAX such as SUM, IF and column references.
How a calculated column works
When you create a calculated column, Power BI runs your formula for each row of the table. Inside the formula, DAX has a row context. That means column references point to the values in the current row only.
Here is a column on the Sales table:
Line Amount = Sales[Quantity] * Sales[Unit Price]For each row, DAX reads that row's Quantity and Unit Price and multiplies them. The result is saved as a new column called Line Amount.
The values are computed when the data is refreshed, not when someone opens a report. After that they sit in the model like any imported column. A report visual reads them directly.
Using RELATED to read from another table
A calculated column can pull in a value from a table on the "one" side of a many-to-one relationship. The function for this is RELATED.
Product Category = RELATED(Product[Category])This goes on the Sales table. For each sales row, DAX follows the relationship to the matching Product row and returns its Category. RELATED works in the many-to-one direction only, from the many side to the one side.
Columns you can use as fields
A calculated column is a real column. You can place it on a slicer, a chart axis, a filter or a sort order. A measure cannot do this.
Price Band = IF(Product[List Price] >= 100, "High", "Standard")This column goes on the Product table. You can now drop Price Band on a slicer and let users filter by band. Grouping rows into categories is one of the best reasons to use a calculated column.
What it costs
Stored values take memory. Every calculated column increases the size of the model, and it adds work to every refresh because DAX must evaluate it for every row. On a table with millions of rows, a few extra columns can be noticeable. Columns with many distinct values compress poorly, so a column like Line Amount costs more than Price Band, which has only two values.
A common mistake: ratios
A row-level ratio is a poor fit for a column.
Margin % (column) = DIVIDE(Sales[Profit], Sales[Line Amount])If you sum or average this column, every row counts equally, no matter how large the sale was. A tiny order with a 50% margin pulls the result as hard as a large order with 5%. The overall margin comes out wrong. A measure that divides the totals gives the right answer at every level. Later chapters cover that fix.
Exercise
You want users to filter a report by whether a product's list price is at least 100. Should you write a calculated column or a measure, and when is the value computed?
Show answer
Write a calculated column, such as Price Band = IF(Product[List Price] >= 100, "High", "Standard"). A slicer needs a column to read values from, which a measure cannot provide. DAX computes it during data refresh and stores it in the model.
Exercise
A Sales table has a many-to-one relationship to Product. Which formula, placed on Sales, returns each row's product category?
Show answer
Product Category = RELATED(Product[Category]). RELATED follows the relationship from the many side (Sales) to the one side (Product).