Sub topic 6
DirectQuery mode: querying the source at report time
This chapter assumes you can build a Power BI Desktop model with a star schema and write basic DAX measures. It also assumes you know basic SQL. You should already know that Import mode copies data into the VertiPaq engine.
What DirectQuery does
In DirectQuery mode, the semantic model stores metadata only. That means table names, columns, relationships, and measures. It holds no copy of the rows. When a user opens a report, each visual produces a DAX query. The engine translates that query into the source's native language, usually SQL. It sends the SQL to the source and shapes the returned rows into the visual.
Storage mode is set per table. In Power BI Desktop, you pick DirectQuery in the connection dialog. You can also change it later in Model view under the table's Advanced properties.
The same measure, a different engine
A measure does not change between modes. The engine that runs it does.
Total Sales = SUMX ( Sales, Sales[Quantity] * Sales[UnitPrice] )For Import and Direct Lake tables, VertiPaq evaluates this measure in memory. For a DirectQuery table, the model translates it into a SQL query against the source. The query is roughly a SUM of Quantity * UnitPrice with a GROUP BY on the columns in the visual.
To see how a visual behaves, run a query like this one in DAX Studio:
EVALUATE
SUMMARIZECOLUMNS (
'Date'[Year],
"Total Sales", [Total Sales]
)
ORDER BY 'Date'[Year]Against a DirectQuery table, this one DAX query can trigger one or more SQL queries at the source.
What you gain
- Freshness. Each query reads the current source data. There is no refresh schedule to maintain.
- Size. The data is not limited by the memory of the model. This matters when the source holds billions of rows.
- Control at the source. Existing source-side security, views, and indexes still apply.
What you give up
DirectQuery has costs that Import mode does not have. The next chapter covers them in detail. In short:
- Speed depends on the source. A slow database makes a slow report.
- Source load. Every user action sends queries. Many users can overload an operational database.
- Modeling limits. Some DAX functions and Power Query transformations are restricted or unavailable. Transformations that cannot be folded into a single native query are blocked.
Making DirectQuery behave
Several habits reduce problems:
- Use a star schema. Joins between a fact table and small dimension tables translate into efficient SQL.
- Do the heavy shaping in the source, in views or tables, not in Power Query.
- Index or partition the columns used for joins and filters.
- Reduce visuals per page, and use the "Apply" button for slicers so a query does not run on every click.
- Prefer simple measures. Row-by-row iterators over large tables can produce expensive SQL.
Checking the generated SQL
Open Performance Analyzer in Desktop, start recording, and refresh the visuals. Each visual shows time split into DAX query, direct query, and other. Copy the query to see what runs. If the direct query portion dominates, the source is the bottleneck, and tuning belongs there.
Exercise
A report uses a DirectQuery table with the measure Total Sales. A user changes a slicer. Where is the measure calculated, and what does the user's action cost the source?
Show answer
The model translates the DAX query into SQL, and the source database calculates the aggregate. Each slicer change sends new queries to the source, so it adds load to it and the speed of the visual depends on the source's response time.