Sub topic 9
What Calculated Columns Cost in Memory and Refresh Time
This chapter assumes you know basic Power BI Desktop navigation, understand tables, columns and relationships in a star schema, and can read simple DAX such as SUM and IF. It also assumes you know that a calculated column is evaluated row by row and a measure is evaluated in the current filter context. This chapter covers what that difference costs you in memory and in refresh time.
Where the cost comes from
A calculated column is computed when the model is refreshed. Power BI evaluates the expression once for every row, then stores the results in the table like any imported column. The result is compressed and held in memory by the VertiPaq engine, the in-memory column store that Power BI uses for imported data.
A measure stores no results. Only its formula is saved. Power BI evaluates it when a visual asks for it, for whatever rows the filter context leaves visible.
Calculated columns therefore cost you in two places:
- Refresh time. Each calculated column adds work during every refresh, after the source data has loaded.
- Memory. Each calculated column adds a stored column to the model, and that column takes space in memory and in the saved file.
Measures cost neither. They use CPU time when a report page renders, and that time depends on the formula and on the number of rows involved.
Line Amount = Sales[Quantity] * Sales[Unit Price]If Sales has 50 million rows, this one line creates 50 million stored values. The measure below returns the same total and stores nothing.
Total Sales (no column) = SUMX(Sales, Sales[Quantity] * Sales[Unit Price])How VertiPaq stores a column
VertiPaq compresses each column on its own. Three things decide how large a column becomes.
- Cardinality. This is the number of distinct values in the column. A column with 5 distinct values compresses very well. A column with 50 million distinct values barely compresses.
- Data type. Integers and dates generally take less space than long text.
- Value distribution and sort order. Long runs of repeated values compress better.
Most storage techniques are variations on a dictionary. VertiPaq keeps a list of the distinct values and stores each row as a small reference into that list. It then applies run-length encoding, which replaces repeated references with a value and a count.
This explains why not every calculated column costs the same. A column like Price Band, with two possible values, is cheap even on a large table. A column like Line Amount is a product of two numbers and has many distinct values, so it is expensive.
Price Band = IF(Product[List Price] >= 100, "High", "Standard")Price Band sits on the Product table, which is usually small, and has two distinct values. Its cost is negligible.
Which tables make the cost visible
The row count of the table that holds the column matters most. In a star schema, the two table types behave very differently.
| Table type | Typical size | Cost of one calculated column |
|---|---|---|
| Dimension (Product, Customer, Date) | Thousands to a few million rows | Usually small |
| Fact (Sales, Transactions) | Millions to billions of rows | Can be large |
Put a calculated column on a dimension table when you can. A column on the Product table is computed once per product. A column on the Sales table is computed once per sale.
Compare these two ways to get a category onto each sale:
Product Category = RELATED(Product[Category])Placed on Sales, this stores one value per sales row, even though the same category repeats many times. Compression reduces the damage because the column has low cardinality, but the refresh still evaluates RELATED for every row. The category already lives on Product, and the relationship already lets a visual filter Sales by it. If a report only needs to slice by category, use Product[Category] directly and skip this column.
Refresh-time cost
During refresh, Power BI loads data from sources, applies Power Query steps, and then builds the calculated columns and rebuilds relationships. The order matters. Calculated columns are processed after the loaded tables, and they can depend on each other and on other tables. A calculated column that uses RELATED needs the relationship in place first.
Three effects add up:
- Expression cost. A simple arithmetic column is fast. A column that uses iterators, lookups or functions such as EARLIER, or one that triggers context transition through CALCULATE, is much slower per row.
- Row count. Cost grows with the number of rows on the table.
- Dependency chains. If column B uses column A, A is computed first. Long chains of calculated columns make refresh slower and harder to debug.
Calculated columns are recomputed on every refresh of their table, even when only one row of source data changed. If you use incremental refresh, which reloads only recent partitions, calculated columns on that table are still computed for the partitions being processed. Keep that in mind when a table is large and the column is complex.
Seeing the cost for yourself
Measure instead of guessing. Two tools help.
- VertiPaq Analyzer. It reads model metadata and reports each column's size, cardinality and share of the total. It is available through DAX Studio and Tabular Editor.
- Performance Analyzer. It is built into Power BI Desktop and records how long each visual's queries take. You use it to compare a measure-based design with a column-based one.
EVALUATE
SUMMARIZECOLUMNS(
Product[Category],
"Total", [Total Sales]
)Run a query like this in DAX Studio with Server Timings turned on. It shows the time spent in the storage engine and in the formula engine. A calculated column usually moves work out of the formula engine at query time, because the value is already stored. You pay for that at refresh time and in memory.
Calculated columns also appear in the model with a distinct data size. If one column accounts for a large share of the model, that column is the first candidate for removal or redesign.
The trade: memory for query speed
The cost is not pure loss. A stored column can make queries faster, because the storage engine scans it directly without evaluating an expression per row.
Total Sales = SUM(Sales[Line Amount])This measure reads a stored column. The version with SUMX evaluates the multiplication at query time. For simple arithmetic like this, the storage engine usually handles SUMX over a single table efficiently, and the speed difference is small. Do not assume the column is faster. Test it.
A stored column tends to help most when the logic is expensive per row and the answer never changes with filters. A stored column tends to hurt when it is cheap to compute on the fly, when it has high cardinality, or when the table is very large.
Common errors
- Adding a column for every intermediate step. Chains of helper columns such as Amount, Amount After Discount and Amount After Tax each cost memory. Combine them into one measure or one expression.
- High-cardinality text columns. Concatenating several keys into a long string creates a column with nearly one distinct value per row. This is one of the largest sources of bloat.
- Columns on the fact table that belong on a dimension. Move the logic to the dimension, where it is computed once per member.
- Row-level ratios. Margin percent as a column also gives wrong totals, as the earlier chapter showed.
Margin % = DIVIDE(SUM(Sales[Profit]), SUM(Sales[Line Amount]))The measure above stores nothing and gives the right answer at every level.
Reducing the cost
Work through these options in order:
- Ask whether a measure can do the job. If the value is only ever aggregated, it should be a measure.
- Move the logic upstream. A column in Power Query or in the source database is computed outside the model's calculation pass, and a source-side column can be compressed well and may be folded into the source query.
- Move the column to the smaller dimension table.
- Lower the cardinality. Round values, split a datetime into a date and a time, or use an integer key in place of text.
- Delete columns that no visual, relationship or measure uses.
Summary of the cost model
| Question | Calculated column | Measure |
|---|---|---|
| When is it computed? | At refresh | At query |
| Is the result stored? | Yes, in memory and the file | No |
| What grows with it? | Model size and refresh time | Query time |
| Row count that matters | Rows in its own table | Rows visible in the filter context |
Exercise
A Sales table has 80 million rows. A colleague adds the calculated column Line Amount = Sales[Quantity] * Sales[Unit Price] and a second column Order Key Text that concatenates OrderID, LineNumber and CustomerID into a text string. Report which column is likely to use more memory and why, and propose a cheaper design for each.
Show answer
Order Key Text is likely to use far more memory. It has close to one distinct value per row, so dictionary and run-length compression barely help, and it is stored as text. Line Amount also has many distinct values, but it is numeric and compresses better. For Line Amount, replace the column with the measure SUMX(Sales, Sales[Quantity] * Sales[Unit Price]), which stores nothing. For Order Key Text, drop it if it is only an identifier. If a unique key is needed, use an integer key built in Power Query or in the source, or keep the existing numeric columns.
Exercise
You need to show sales by Price Band (High or Standard, based on Product[List Price]) on a slicer. Should Price Band be a calculated column on Sales or on Product, and what does that choice save?
Show answer
Put it on Product. A slicer needs a column, so a measure cannot be used. On Product the expression is computed once per product, not once per sale, and the column has two distinct values, so its memory cost is negligible. The relationship from Product to Sales filters the sales rows for you.