Sub topic 2
Query folding: the requirement behind incremental refresh
This chapter assumes you can connect Power BI Desktop to a relational source in Import mode, and that you know the Power Query editor and the Applied Steps pane. Incremental refresh depends on one behavior of Power Query called query folding. If folding fails, the feature may still run, but it loses most of its benefit.
What query folding is
Query folding is Power Query's ability to translate your transformation steps into a single native query, such as SQL, and send it to the source. The source does the filtering, joining, and grouping. Power Query receives only the result.
When a step cannot be translated, Power Query stops folding at that step. It downloads the data produced so far and runs the remaining steps on the Power BI side, in its own engine.
Why incremental refresh needs it
Incremental refresh works by filtering the table on RangeStart and RangeEnd. Each partition runs that filter with its own date boundaries. If the filter folds, the source returns only the rows for that partition.
If the filter does not fold, every partition refresh reads the whole table from the source and discards the rows outside its range. You then pay for many full reads instead of one. A refresh can end up slower than a plain full refresh.
The diagram shows the two paths.
Which sources fold
Relational sources such as SQL Server, Azure SQL Database, Azure Synapse, and other databases with native query support can fold. Flat files such as CSV and Excel, and most web or API sources, cannot. For those, Power Query has no engine that can run a filter on its behalf.
Some sources need extra care. For a source without folding, incremental refresh can still be configured, but you should not expect a reduction in the data read per refresh.
Checking whether a step folds
Use the Applied Steps pane in Power Query:
- Right-click the step you want to check, such as the filter step.
- Look at View Native Query.
- If the option is enabled, the step folds, and the dialog shows the generated SQL.
- If the option is grayed out, the step does not fold, or an earlier step already broke folding.
Here is a query that folds. The filter on OrderDate becomes a WHERE clause.
let
Source = Sql.Database("myserver.database.windows.net", "SalesDb"),
FactSales = Source{[Schema = "dbo", Item = "FactSales"]}[Data],
Filtered = Table.SelectRows(
FactSales,
each [OrderDate] >= RangeStart and [OrderDate] < RangeEnd
)
in
FilteredFor this query, View Native Query on the Filtered step shows a statement similar to select ... from [dbo].[FactSales] where [OrderDate] >= @start and [OrderDate] < @end. The exact text varies, but the where clause on the date column is what you look for.
Common causes of broken folding
Folding is lost when a step has no equivalent in the source query language. Once one step breaks it, every later step runs locally too.
| Cause | Example | Better approach |
|---|---|---|
| A step with no native equivalent | Adding an index column, or pivoting some sources | Move the step after the date filter, or do it in the source |
| Custom M functions | `Table.AddColumn` calling your own function | Compute the value in a view |
| Type or text operations the source cannot express | Some `Text.` functions on columns | Use simpler operations or push the logic into a view |
| Filter placed after a non-folding step | Date filter after an index step | Put the date filter first |
The order of steps matters. Place the RangeStart and RangeEnd filter as early as you can, ideally right after the source and navigation steps.
Practical guidance
- Filter on the date range before any step that might not fold.
- If a transformation cannot fold, move it into a database view and connect to the view instead.
- Re-check View Native Query each time you add a step to the table.
- If you write a native SQL query in
Value.NativeQuery, folding of later steps may be lost unless you pass the option[EnableFolding = true]. Even then, test the result.
Exercise
You add the RangeStart and RangeEnd filter to a SQL table, then notice that View Native Query is grayed out on that step. An earlier step in the query adds an index column. What should you try first, and why?
Show answer
Move the date filter so it runs before the index step, or remove the index step from Power Query and do the work in a source view. Once a step breaks folding, all later steps run locally, so the date filter must come before the step that breaks folding. After reordering, confirm that View Native Query is enabled on the filter step.