Sub topic 12
Aggregation Tables to Speed Up Large DirectQuery Models
This chapter assumes you know how Import and DirectQuery tables behave, what a star schema is, and how composite models and Dual tables work. You should also be able to write basic DAX measures.
Why aggregations exist
A DirectQuery fact table with billions of rows sends every visual query to the source. Even a well-tuned warehouse takes seconds to scan and group that much data. Most report visuals, however, ask for totals by year, category, or region. They never need individual order lines.
An aggregation table is a small, pre-summarized copy of the fact table. Power BI keeps it in memory in Import mode. When a visual asks for a total that the summary can answer, the engine reads the in-memory table and never contacts the source. When a visual needs detail, such as a single order line, the engine falls back to the DirectQuery fact table.
This is why aggregations belong with composite models. The detail table stays in DirectQuery, and the summary table uses Import mode, so the model mixes two storage modes.
How the engine chooses
Power BI decides at query time, per visual. The user and the report author do not pick a table. The engine checks whether the request can be answered from the aggregation table. If it can, the engine uses that table. If it cannot, the engine sends a query to the detail table at the source.
The following diagram shows that decision.
A worked example
Suppose FactSales is a DirectQuery table with one row per order line. It has these columns: OrderDate, ProductKey, Quantity, and SalesAmount. It relates to a Date dimension and a Product dimension.
Most visuals show sales by day or by product category. So you build FactSales_Agg, which has one row per OrderDate and ProductKey. It stores the summed SalesAmount, summed Quantity, and a count of rows.
You can build this table in the source as a view or table, which is the usual choice. Here is a SQL definition.
CREATE VIEW dbo.FactSales_Agg AS
SELECT
OrderDate,
ProductKey,
SUM(SalesAmount) AS SalesAmountSum,
SUM(Quantity) AS QuantitySum,
COUNT_BIG(*) AS SalesRowCount
FROM dbo.FactSales
GROUP BY OrderDate, ProductKey;Load this view into the model with Import storage mode. Then relate it to the same Date and Product dimensions as the detail table.
Configuring the Manage aggregations dialog
In Power BI Desktop, right-click the aggregation table in the Data pane and choose Manage aggregations. For each column of the aggregation table, you set three things:
- Summarization function: Sum, Count, Min, Max, GroupBy, or Count table rows.
- Detail table: the table the summary stands in for, here
FactSales. - Detail column: the column in the detail table that this column summarizes.
For the example, the mapping looks like this.
| Aggregation column | Summarization | Detail table | Detail column |
|---|---|---|---|
| `SalesAmountSum` | Sum | `FactSales` | `SalesAmount` |
| `QuantitySum` | Sum | `FactSales` | `Quantity` |
| `SalesRowCount` | Count table rows | `FactSales` | (none) |
| `OrderDate` | GroupBy | `FactSales` | `OrderDate` |
| `ProductKey` | GroupBy | `FactSales` | `ProductKey` |
Power BI then hides the aggregation table from report authors. Users write measures against FactSales as usual, and the engine redirects them when it can.
The measure stays the same
You do not write special measures for the aggregation. Take the measure from the topic examples.
Total Sales = SUMX ( FactSales, FactSales[Quantity] * FactSales[UnitPrice] )This measure multiplies two columns per row. The aggregation holds only sums of each column, so it cannot reproduce the row-level product. This measure always goes to the detail table. A measure that the aggregation can answer uses a plain summarization over a mapped column.
Sales Amount = SUM ( FactSales[SalesAmount] )
Order Lines = COUNTROWS ( FactSales )Sales Amount maps to the Sum on SalesAmountSum. Order Lines maps to Count table rows. If you need Total Sales to be fast, add a precomputed LineTotal column to the source, and aggregate that column instead.
Which queries hit the aggregation
A query uses the aggregation only if all of the following hold.
- Every measure column in the query has a mapped summarization, and the function matches (Sum for SUM, and so on).
- Every grouping column is either a GroupBy column in the aggregation table, or comes from a dimension that relates to the aggregation table through the same relationships.
- The filters in the query use only those same columns.
In the example, a visual with Date[Year] and Product[Category] on the axes hits the aggregation. The aggregation holds OrderDate and ProductKey, and the relationships carry the year and category. A visual that groups by FactSales[OrderID] does not, because that column is not in the aggregation.
Dimension tables and Dual mode
For the match to work, the dimensions must be reachable from the aggregation table. If the dimensions are DirectQuery tables from the same source, a query that groups by a dimension column can still be resolved in memory for the facts. The dimension values, however, must come from somewhere. Set dimension tables to Dual storage mode. Dual tables exist in memory and at the source. Power BI uses the in-memory copy when the rest of the query is answered from the cache, and uses the source copy when the query is pushed down to the source. This avoids a mismatch where an in-memory summary joins to a dimension that needs a source query.
A Dual dimension also keeps relationships regular. A relationship between an Import table and a DirectQuery table is a limited relationship, which does not guarantee that keys match one to one. Aggregations work correctly with regular relationships, so Dual dimensions help here.
Aggregation through a dimension
You can also aggregate at a higher grain, for example by Product[Category] rather than ProductKey. This gives a much smaller table. Power BI supports a related pattern, called a relationship-based aggregation. The aggregation table contains the foreign key to a dimension, and the group-by columns come from that dimension. Use it when most queries group by an attribute of one dimension.
A smaller grain brings a trade-off. If you aggregate to year and category, a visual by month cannot use it. Choose a grain just below the level of your most common visuals.
Keeping the aggregation in sync
An Import aggregation table is a snapshot. It reflects the source at the last refresh, while the DirectQuery detail table reflects the source now. After new rows reach the source, the two can disagree. A card that hits the aggregation shows the old total, and a detail table visual shows the new total.
Handle this in one of these ways:
- Refresh the aggregation on a schedule that matches your freshness needs.
- Use incremental refresh on the aggregation table, so only recent partitions reload.
- Accept the lag and document it for report users.
Aggregation tables with Import mode never reflect real-time data. If the report needs data to the minute, aggregations may be the wrong tool, or you may need a hot-and-cold design where only older periods are aggregated.
Verifying the aggregation is used
Do not assume the redirect works. Check it in two places.
- Performance Analyzer. Run a visual and look at the DAX query. A visual served from the aggregation has no Direct query entry. A visual that falls back shows a DirectQuery duration.
- SQL Server Profiler or Extended Events. Connect to the model and watch the
Aggregate Table Rewrite Queryevent. Its text shows whether a match happened and which table answered.
Compare timings. An aggregation hit usually returns in tens of milliseconds, while a miss takes as long as the source needs.
Common errors
- Mismatched data types. A GroupBy column must have the same data type as its detail column. A mismatch makes the dialog reject the mapping.
- Detail table in Import mode. Aggregations require the detail table to be DirectQuery. If the detail table is Import, the dialog does not apply the feature.
- Aggregation table in DirectQuery. An aggregation in DirectQuery mode works if it is a smaller table at the same source, but it gives no memory speedup. For a strong gain, use Import.
- Missing relationships. If the aggregation table lacks relationships to the dimensions, grouped queries miss it.
- Row-level security gaps. If RLS filters the dimensions, queries filter correctly through relationships. If RLS filters only the detail table, the aggregation table bypasses that filter, which leaks totals. Apply RLS to dimensions or to both tables.
- Wrong function. Mapping an average as Sum gives wrong results. Store a sum and a count, then compute the average in a measure.
Exercise
A DirectQuery table FactSales has an Import aggregation table FactSales_Agg at the grain of OrderDate and ProductKey. A card visual shows SUM(FactSales[SalesAmount]) and responds in milliseconds. A table visual that shows FactSales[OrderID] and SalesAmount takes eight seconds. Explain both timings, and name one change that would make the first visual show current data without waiting for the next scheduled refresh.
Show answer
The card query needs only a Sum over a mapped column and no grouping, so the engine answers it from the in-memory aggregation table. The table visual groups by OrderID, which the aggregation does not contain, so the engine sends a SQL query to the DirectQuery detail table, and the source scan takes eight seconds. To make the card current, refresh the aggregation table more often, or use incremental refresh so that recent partitions reload quickly. If the card must be real-time, the aggregation cannot be used for that period.