Sub topic 15
Performance and maintenance tips
This chapter assumes you can tell a calculated column from a measure. It also assumes you know that columns are evaluated at refresh time and measures at query time, and that you have used Table view, Model view and Performance analyzer in Power BI Desktop. Here you apply those ideas to keep a model small, fast to refresh, quick to query and easy to change.
Two different budgets
A calculated column and a measure spend different resources.
- A calculated column spends refresh time and memory. It is computed for every row at each refresh, and the result is stored in the model.
- A measure spends query time. It is computed when a visual asks for it, for the filter context of each cell, and nothing is stored.
You tune each one against its own budget. A slow refresh or a large file points at columns. A slow visual points at measures.
Keeping calculated columns cheap
Count what each column costs
Power BI's storage engine, VertiPaq, stores each column separately and compresses it. How well a column compresses depends mostly on cardinality, the number of distinct values.
Price Bandhas two distinct values. It is tiny.Line Amount = Sales[Quantity] * Sales[Unit Price]can have a distinct value for almost every row in a large Sales table. It uses far more memory.- A text column built by concatenating several fields is usually the worst case. Nearly every value is unique.
Calculated columns are computed after the data has loaded, so they often compress less well than columns that arrive already in the source or in Power Query. Treat that as a reason to prefer upstream columns for large tables, not as a fixed rule. Measure it in your own model.
Ask whether the column is needed
A column is justified when you need it as a slicer, axis, filter or sort field, or when it feeds a relationship key. If a column exists only to be summed, you can often replace it with an iterator measure:
Total Sales (no column) = SUMX(Sales, Sales[Quantity] * Sales[Unit Price])This removes the stored Line Amount column from the largest table. It moves the cost to query time, which is the right trade for a model that is memory-bound. Keep the column instead when many measures reuse it and the table is small enough that memory is not a concern.
| Situation | Better choice |
|---|---|
| Large fact table, column only summed | Iterator measure such as SUMX |
| Many measures reuse one row-level product | Column, if the memory cost is acceptable |
| Column feeds a slicer or axis | Column, ideally built upstream |
| Column has near-unique values per row | Avoid, or build it in the source |
Push work upstream when the table is big
Power Query columns and source-side columns are loaded as ordinary data. A column defined in a SQL view, for example, costs no DAX evaluation during refresh. Power Query steps that fold back to the source also run on the database server. If the logic only needs the row's own values, do it as far upstream as you can.
Use a calculated column when the logic needs something only the model has, such as RELATED across a relationship:
Product Category = RELATED(Product[Category])On a very large Sales table, a better design is often to leave Category on the Product dimension and use it directly on visuals. The relationship already filters Sales. Copying the category onto every sales row duplicates data the model already has.
Put dimension-level columns on the dimension
Compare these two placements:
Price BandonProduct, with thousands of rows, computes thousands of times at refresh.- A column with the same logic on
Sales, with millions of rows, computes millions of times and stores millions of values.
Define a column on the smallest table that has the information.
Keeping measures fast
Prefer plain aggregations
SUM(Sales[Line Amount]) is evaluated by the storage engine directly and is very cheap. A SUMX over a large table with a multi-column expression is usually still fast. Cost grows when the iterator body gets complicated, for example nested iterators or RELATED calls on every row over millions of rows. Start with the simplest expression that gives the right answer, and only add complexity when you must.
Use variables to avoid repeating work
A variable is evaluated once and reused. This also makes a ratio measure easier to read:
Margin % =
VAR TotalProfit = SUM(Sales[Profit])
VAR TotalSales = SUM(Sales[Line Amount])
RETURN
DIVIDE(TotalProfit, TotalSales)DIVIDE handles a zero denominator by returning a blank result, so you do not need an IF test around the division.
Build measures from measures
Reference an existing measure instead of copying its expression:
Red Sales = CALCULATE([Total Sales], Product[Color] = "Red")If you later change how Total Sales is computed, for example from SUM to SUMX, every measure built on it changes with it. Each calculation lives in one place.
Be careful with context transition
When a measure is referenced inside a row context, such as inside an iterator, CALCULATE runs implicitly and turns the current row into a filter. This is correct, but it costs more than a plain column reference, and it grows with the row count. If a measure over a large table is slow, check whether an iterator is calling another measure on every row. Rewriting the inner calculation as a direct column expression often helps.
Reduce what the visual asks for
Each visual sends queries for its measures across all of its fields. Many measures, many categories and high-cardinality axes (such as a table of every order ID) all make queries heavier. Often a slow report is a design problem, not a DAX problem.
Finding the real cause
Do not guess. Use tools to confirm.
- Performance analyzer (View tab in Power BI Desktop). It records how long each visual takes, split into the DAX query and the visual rendering. Copy the query for a slow visual.
- DAX Studio. Paste the copied query to inspect timings and query plans. It also reads model metadata, including the size of each column.
- Model metrics. VertiPaq Analyzer, available in DAX Studio, lists the memory used by each table and column. Sort by size to find columns that are costing the most.
- Refresh duration. In the Power BI service, refresh history shows total time. Compare it before and after you remove or move a column.
Match the symptom to the likely cause
| Symptom | Likely cause | First action |
|---|---|---|
| Refresh takes much longer after adding logic | Calculated columns on a large table | Move the logic to Power Query or the source |
| File size grows sharply | High-cardinality columns | Check column sizes in VertiPaq Analyzer, remove or reshape |
| One visual is slow, others fine | Heavy measure or too many fields in that visual | Check it in Performance analyzer, simplify |
| All visuals are slow | Model design, such as a wide flat table or many-to-many relationships | Review the model in Model view |
| Totals look wrong | Ratio stored as a column and summed | Replace it with a measure, as in `Margin %` above |
Maintenance habits
Performance is only half of the job. A model that nobody understands is also expensive.
- Name for the reader. Use clear, business-style names such as
Total Sales, notMeasure 1. Keep column naming consistent so people can tellSales[Line Amount]from a similarly named measure. - Use a consistent style for references. Write column references with the table name,
Sales[Line Amount], and measure references without one,[Total Sales]. This makes it obvious which is which. - Group measures. Keep them in a dedicated table or in display folders, so report authors find them quickly.
- Hide what authors should not use. Hide helper columns and the raw numeric columns that you replaced with explicit measures, so users do not drag them in as implicit measures with unpredictable aggregation.
- Write descriptions. In the model's properties, add a short description to each measure and to any column whose logic is not obvious. The description appears as a tooltip in the field list.
- Remove unused objects. Columns that nothing references still take memory and refresh time. Check before deleting, because reports, row-level security rules and other measures may depend on them.
- Keep one definition of each business rule. If "Red products" is defined in a column, a measure and a Power Query step, a change will need three edits and one will be missed. Put the rule in one place and reference it.
A short review routine
Run this before publishing a model:
- List the calculated columns. For each, decide whether it must be a column, and whether it could live upstream.
- Sort columns by size and look at the biggest ones.
- Replace any ratio column that gets summed or averaged with a measure.
- Check the slowest visuals in Performance analyzer.
- Confirm that measures reuse other measures instead of copying expressions.
- Hide helper fields and add descriptions.
Exercise
A Sales table with 40 million rows has a calculated column Line Amount = Sales[Quantity] * Sales[Unit Price]. The only use of the column is Total Sales = SUM(Sales[Line Amount]). The model is close to its memory limit. What would you change, and what do you give up?
Show answer
Remove the calculated column and define the measure as Total Sales (no column) = SUMX(Sales, Sales[Quantity] * Sales[Unit Price]). This frees the memory that a near-unique 40-million-row column uses and shortens refresh. The cost moves to query time, because the multiplication runs when a visual asks for it. For simple row-by-row arithmetic this is usually fast, but check the affected visuals in Performance analyzer. An even better option, if the data source allows it, is to bring the value in as a source-side column, although with high cardinality it will still use memory.
Exercise
Your report has three measures that each repeat the expression SUMX(Sales, Sales[Quantity] * Sales[Unit Price]). The business changes the definition of a sale to exclude returns. What is the maintenance problem, and how do you fix it?
Show answer
The rule is defined in three places, so each must be edited and one may be missed, leaving the report inconsistent. Define a single measure such as Total Sales and have the other measures reference it, for example Red Sales = CALCULATE([Total Sales], Product[Color] = "Red"). Then change the definition once.