Sub topic 18
Understanding query folding and performance
This chapter assumes you can already build a multi-step query in the Power Query Editor, read the Applied Steps pane, and recognize basic M code such as Table.SelectRows. You don't need database experience, but it helps to know what a SQL WHERE clause does.
What query folding is
Query folding means Power Query translates your steps into one query in the source's own language, usually SQL, and sends it to the source. The source does the filtering, grouping, and sorting. Excel receives only the finished result.
Without folding, Power Query downloads the source rows and runs each step on your computer. That works, but it is slow and memory-hungry on large tables.
| Row | Folded | Not folded |
|---|---|---|
| Where steps run | On the source server | In the Power Query engine on your PC |
| Data transferred | Only the result | All rows the steps need to read |
| Typical sources | SQL Server, other relational databases | Excel tables, CSV files, folders |
Which sources can fold
Folding needs a source that can run queries. Relational databases such as SQL Server, Oracle, and PostgreSQL support it. Whether a particular step folds depends on the connector.
Excel tables, CSV files, and folders never fold. They are plain files, so Power Query must read them in full. For these sources, query folding doesn't apply. Performance comes from reducing work inside the engine, as described later in this chapter.
Which steps fold
These steps usually fold against a database:
- Filtering rows
- Removing or selecting columns
- Renaming columns
- Sorting
- Group By with common aggregations such as sum, count, min, and max
- Merging two tables from the same database
- Changing data types
These steps often break folding:
- Adding an index column
- Custom columns that use M functions with no SQL equivalent
- Merging tables from different sources
Table.Buffer- Pivoting, in many connectors
A step that doesn't fold also stops everything after it from folding. The engine can't hand a half-processed table back to the server. Step order therefore matters.
Checking whether a step folds
Right-click a step in Applied Steps. If View Native Query is available, the step folds, and clicking it shows the generated SQL. If the option is greyed out, the step doesn't fold, or an earlier step already broke folding.
Click through the steps from top to bottom. The last step where the option is still enabled is the last one that folded. The step after it is the one to fix.
Example: step order decides folding
This query points at a SQL Server database. Table and column names are examples.
let
Source = Sql.Database("localhost", "SalesDB"),
Orders = Source{[Schema = "dbo", Item = "Orders"]}[Data],
Recent = Table.SelectRows(Orders, each [OrderDate] >= #date(2025, 1, 1)),
Slim = Table.SelectColumns(Recent, {"OrderID", "CustomerID", "Amount"}),
Grouped = Table.Group(Slim, {"CustomerID"}, {{"Total", each List.Sum([Amount]), type number}}),
Ranked = Table.AddIndexColumn(Grouped, "Rank", 1, 1, Int64.Type)
in
RankedThe filter, column selection, and grouping can all fold. The server returns one row per customer, and only AddIndexColumn runs locally on that small result.
Here is the same logic with the index added first:
let
Source = Sql.Database("localhost", "SalesDB"),
Orders = Source{[Schema = "dbo", Item = "Orders"]}[Data],
Indexed = Table.AddIndexColumn(Orders, "RowNum", 1, 1, Int64.Type),
Recent = Table.SelectRows(Indexed, each [OrderDate] >= #date(2025, 1, 1)),
Grouped = Table.Group(Recent, {"CustomerID"}, {{"Total", each List.Sum([Amount]), type number}})
in
GroupedHere the index step breaks folding, so Power Query pulls the entire Orders table to your PC and then filters it. The result is the same, but the work is much larger.
Native queries
You can paste your own SQL when connecting to a database, which creates a Value.NativeQuery step. Later steps usually can't fold on top of a native query, so Power Query downloads its output and processes the rest locally. Put the filtering and aggregation in your SQL. Check View Native Query on later steps to see whether folding continued.
Performance when folding isn't available
For Excel, CSV, and folder sources, these habits help:
- Remove unneeded columns and filter rows as early as possible. Later steps then handle less data.
- Avoid duplicating a slow source. A query that references another query can re-evaluate it, so set the shared base query to Connection only and keep the work in one place.
- Merge on columns with matching data types. Mismatched types fail to match rows or force extra conversions.
- Use
Table.Buffersparingly. It holds a table in memory, which can speed up repeated lookups in a custom function. It also blocks folding and uses memory. - Turn off background refresh while testing, so queries don't refresh repeatedly as you edit.
Common errors
- Assuming Excel sources fold. They don't. Don't look for a native query on an Excel table.
- Changing step order without rechecking. A reordered step can silently stop folding.
- Hiding a custom function in an early step. One unfoldable function can pull the entire table.
- Judging speed from the preview. The Editor shows only a sample of rows. Time a full refresh to measure real performance.
Exercise
A query connects to a SQL Server table. Steps 1 to 3 (Source, Filter Rows, Remove Columns) have View Native Query enabled. Step 4 is Add Index Column, which is greyed out. Steps 5 and 6 are Sort Rows and Group By, which are also greyed out. Why are steps 5 and 6 greyed out, and what is the simplest fix?
Show answer
Add Index Column doesn't fold, and once folding stops, no later step can fold. Steps 5 and 6 run locally even though Sort Rows and Group By could fold on their own. The simplest fix is to move Add Index Column after Group By, so the server filters, sorts, and groups first. Then confirm that View Native Query is enabled on the earlier steps.