Sub topic 6
DirectQuery mode: querying the source at report time
This chapter assumes you can build a star-schema model in Power BI Desktop, write basic DAX measures, and read simple SQL. Earlier chapters covered Import mode, where VertiPaq holds a copy of the data. Here you will see what changes when the model holds no data at all.
What DirectQuery stores
In DirectQuery mode, the semantic model keeps only metadata: table and column definitions, relationships, measures, and security roles. The rows stay in the source database. Every time a visual needs data, Power BI asks the source for it.
Consider the difference from Import:
- With Import, a refresh copies rows into memory. Visuals then read that copy.
- With DirectQuery, no refresh copies rows. Visuals read the source as it is at the moment of the query.
A DirectQuery table is "live" in that sense. The data in a visual is only as old as the last query that fed it.
What happens when a visual renders
A report page produces one DAX query per visual. The engine does not run that DAX against stored data. It translates the query into the source's native language, usually SQL, sends it, and shapes the returned rows into the visual.
A page with eight visuals can send eight or more queries to the source, often at the same moment. A slicer change sends them again. The source database pays for every one.
The same measure, two engines
This measure is defined once, whatever the storage mode:
Total Sales = SUMX ( Sales, Sales[Quantity] * Sales[UnitPrice] )On an Import table, VertiPaq computes it in memory. On a DirectQuery table, the engine pushes the computation to the source. For a visual that shows total sales by year, the generated SQL looks roughly like this (illustrative, not exact, since the real text depends on the source and the model):
SELECT
d.[Year],
SUM(s.[Quantity] * s.[UnitPrice]) AS [Total Sales]
FROM [dbo].[FactSales] AS s
INNER JOIN [dbo].[DimDate] AS d
ON s.[DateKey] = d.[DateKey]
GROUP BY d.[Year];The aggregation runs where the data lives, so only a few rows travel back. This is the main reason DirectQuery can work on tables too large to import. The cost is that the source must be fast at exactly this kind of query.
Reproducing the query a visual sends
To study the behavior of one visual, run the DAX query it generates. You can copy it from Performance Analyzer, or write an equivalent one and run it in DAX Studio:
EVALUATE
SUMMARIZECOLUMNS (
'Date'[Year],
"Total Sales", [Total Sales]
)
ORDER BY 'Date'[Year]With DAX Studio's server timings enabled, DirectQuery queries show the SQL that was sent. You can then paste that SQL into your database's query tool and read its execution plan. A slow visual is often a slow SQL statement, and the fix lies in the source: an index, a columnstore, a precomputed table, or a view.
Setting up a DirectQuery model
You choose the mode at connection time or later in Model view.
- In Power BI Desktop, choose Get data and pick a source that supports DirectQuery, such as SQL Server, Azure SQL Database, Azure Synapse, Snowflake, or Databricks.
- In the connection dialog, select DirectQuery instead of Import.
- Select the tables. Power BI loads only their structure.
- Create relationships, measures, and roles as usual.
You can check or change a table's mode in Model view under Properties, then Advanced, then Storage mode. Direction matters here:
- Switching a table from DirectQuery to Import is allowed. It is a one-way change, and Power BI warns you.
- Switching from Import to DirectQuery is not supported for a table. If you might need DirectQuery later, start with it or plan to rebuild the table.
When the data updates
DirectQuery has no data refresh, but several things can still be stale:
- Visuals show results from the last query they ran. A report that stays open does not re-query by itself.
- The service caches query results for dashboard tiles. The model's refresh schedule controls how often tile caches update. This is a cache refresh, not a data load.
- A user can refresh a visual or the report to run the queries again.
- Automatic page refresh re-queries visuals at an interval you set. Capacity administrators can set a minimum interval. Short intervals multiply source load, so use them only on reports that need them.
For near-real-time dashboards over an operational database, DirectQuery plus automatic page refresh is the usual pattern. It works only if the source can answer every refresh from every open report.
Credentials and gateways
The service needs permission to read the source. There are two common approaches:
- Stored credentials. The model owner saves one account on the data source. Every viewer queries as that account. Source-side permissions do not vary per user.
- Single sign-on (SSO). The service passes each viewer's identity to the source, so the database applies its own permissions per user. This must be supported and enabled for the source.
If the database sits on a private network, a gateway relays the queries. Each DirectQuery request then passes through the gateway machine, so its capacity and network distance add to latency. A later chapter covers row-level security and credentials in all three modes.
Settings that reduce query volume
Desktop has Query reduction options under File, Options and settings, Options, Current file:
- Add an Apply button to slicers and filters, so the page does not re-query after every click.
- Turn off cross-highlighting between visuals by default, so a click in one visual does not trigger queries in all others.
You should also keep pages light. Fewer visuals per page, fewer high-cardinality fields, and fewer wide tables all reduce the number and cost of queries.
Limits you will meet
The next chapter treats these in detail. Know the headlines now:
- Query timeout. The service cancels DirectQuery queries that run too long (about four minutes).
- Row limit. A query that returns more than about one million rows to the model fails by default. Visuals that group by a very high-cardinality column can hit it.
- Power Query. Every transformation must fold into the source query. A step that cannot fold may be unavailable or may force an error.
- DAX. Some functions, especially time intelligence and complex iterators, can be slow or restricted because they must translate to SQL.
- Calculated tables and some calculated columns. Their support is limited because there is no in-memory data to compute from.
When DirectQuery is a good fit
DirectQuery makes sense when at least one of these holds:
- The data is too large to import, and the source can aggregate it quickly.
- The report must show data seconds or minutes old.
- Policy forbids copying the data out of its source, or you need the source's own per-user security through SSO.
It is a poor fit when the source is slow, shared with heavy transactional work, or when you rely on rich DAX that translates poorly.
Exercise
A report page has six visuals and a date slicer. The source is a busy SQL database. Users complain that the page is slow, and the DBA says the database spikes whenever people change the slicer. Name the cause and two changes you could make in Power BI Desktop or the model that lower the load.
Show answer
Each slicer change makes every affected visual send a new DAX query, which the model translates into SQL against the database. Six visuals mean at least six queries per click, multiplied by the number of concurrent users. To lower the load, enable Query reduction options such as an Apply button on the slicer and disabled cross-highlighting, and reduce the number or complexity of visuals on the page. You can also fix the source side with indexes or a pre-aggregated table, or move part of the model to Import in a composite model.