Power Query course · Lesson 16 of 17
Performance, query folding and refresh options
Make queries refresh faster: query folding and View Native Query, step order, Table.Buffer, loading to the Data Model, and every refresh setting.
A query that takes three seconds on a sample file can take ten minutes on a full year of data, and a workbook with twenty queries can freeze Excel during Refresh All. Most slow queries are slow for a handful of predictable reasons: too much data travels from the source, the same data is read several times, or the result is loaded somewhere it does not need to go. This lesson explains how Power Query executes steps, how to tell whether the database does the work for you, where to load results, and which refresh settings to use.
How a refresh runs
When you refresh, Power Query starts from the query's last step and works backwards to find what it needs, then reads the sources. Two things follow. First, it streams data where it can: a filter step processes rows as they arrive rather than loading everything first. Second, some steps need all rows before they can return anything: sorting, grouping, pivoting, merging, removing duplicates and Table.Buffer. These are the expensive steps on large data, and their position in the query matters.
Query folding
With sources that understand a query language, mostly databases (SQL Server, Oracle, PostgreSQL, MySQL, Access) and to some extent OData feeds and SharePoint lists, Power Query tries to fold your steps: it translates them into one native query, such as a SQL statement, and lets the server do the work. A filter on Year = 2026 followed by a Group By by region becomes a single SELECT … WHERE … GROUP BY statement, and only a few summary rows travel over the network instead of millions of detail rows.
| Usually folds | Usually stops folding |
|---|---|
| Choose and remove columns | Adding an Index column |
| Filters on values and dates | Custom columns using functions with no SQL equivalent |
| Sorting | Table.Buffer |
| Group By with Sum, Count, Min, Max, Average | Merging with a query from a different source (for example an Excel table) |
| Merges between tables of the same database | Changing types with a locale, some text transformations |
| Simple custom columns with arithmetic | Keep or Remove Top/Bottom Rows after non-folding steps |
Once a step cannot fold, every step after it runs locally on your computer, even if those later steps could have folded on their own. So the rule is: put foldable steps (column selection, filters, grouping) first, and non-foldable steps (index, complex text work, merges with local tables) as late as possible.
Checking whether a step folds
- In the editor, right-click a step in Applied Steps.
- If View Native Query is available, the query up to that step folds; click it to see the SQL that will run on the server.
- If it is greyed out, folding stopped at or before that step. Walk up the list to find the first step where it becomes greyed out.
| A | B | |
|---|---|---|
| 1 | Step | View Native Query |
| 2 | Source (SQL Server SalesDW) | available |
| 3 | Added Index | greyed out |
| 4 | Removed Other Columns | greyed out |
| 5 | Filtered Rows (Year = 2026) | greyed out |
| 6 | Grouped Rows | greyed out |
Before: an early Index step breaks folding, so 4 million rows are downloaded and then filtered and grouped on the PC.
| A | B | |
|---|---|---|
| 1 | Step | View Native Query |
| 2 | Source (SQL Server SalesDW) | available |
| 3 | Removed Other Columns | available |
| 4 | Filtered Rows (Year = 2026) | available |
| 5 | Grouped Rows | available |
| 6 | Added Index | greyed out |
After: the same steps reordered. The server filters and groups; only the summary rows are downloaded and numbered locally.
Avoid reading the same source many times
Each query is evaluated on its own. If five queries reference a Sales_Raw query, Power Query may read the source files five times during Refresh All. Some practical consequences:
- Prefer one query that cleans the data and several light queries that Reference it, rather than five duplicates that each start from the file.
- Inside one query, a step used repeatedly, such as a lookup list scanned for every row by a custom function, can be loaded into memory once with
Table.BufferorList.Buffer. - Do not buffer by reflex. Buffering stops folding and holds the whole table in memory; use it for small lookup tables used many times, not for the main fact table.
let
Source = Sales_Clean,
Rules = Table.Buffer(Keywords), // small table, read once
Categorised = Table.AddColumn(Source, "Category", each
let hits = Table.SelectRows(Rules, (r) => Text.Contains(Text.Upper([Description]), Text.Upper(r[Keyword])))
in if Table.IsEmpty(hits) then "Other" else hits{0}[Category], type text)
in
CategorisedSettings that slow the editor
Open File › Options and settings › Query Options (in Excel: Data › Get Data › Query Options).
- Current Workbook › Data Load › Allow data preview to download in the background: when on, Power Query refreshes previews of all queries while you work. In a heavy workbook, turning it off makes the editor much more responsive.
- Current Workbook › Data Load › Detect column types and headers for unstructured sources: off means no automatic Changed Type steps; you set types yourself.
- Current Workbook › Privacy: checking privacy levels costs time when sources are combined. Ignoring them can speed things up, but read the warnings in Getting data: files, folders, tables, web pages and databases first.
- Global › Data Load › Data Cache Management Options: clear the cache if previews show stale data.
Where to load: sheet, PivotTable, Data Model or nowhere
Choose the destination with Home › Close & Load To… in the editor, or later by right-clicking a query in Data › Queries & Connections and choosing Load To….
| Destination | Limit | Best for |
|---|---|---|
| Table on a worksheet | 1,048,576 rows | Results people read, filter or use in formulas |
| PivotTable Report | Data stays in the pivot cache | A single summary on one query |
| Only Create Connection + Data Model | Millions of rows, compressed | Large fact tables, relationships, DAX measures |
| Only Create Connection | Nothing loaded | Staging, helper and parameter queries |
Loading two million rows to a sheet is impossible (and 500,000 rows is already slow to save and open). Loading them to the Data Model is routine: the model compresses columns, typically to a fraction of the CSV size, and PivotTables built on it stay fast. See Power Pivot and the Data Model: relationships and DAX measures for relationships and measures.
Refresh commands and options
| Action | How |
|---|---|
| Refresh everything | Data › Refresh All, or Ctrl+Alt+F5 |
| Refresh one query | Right-click it in Queries & Connections › Refresh, or select its table and choose Data › Refresh All › Refresh (Alt+F5) |
| Refresh the preview in the editor | Home › Refresh Preview |
| Cancel a running refresh | Click the refresh message in the status bar or in Queries & Connections |
| See what depends on what | In the editor: View › Query Dependencies |
Per-query settings live in Query Properties: right-click the query in Queries & Connections and choose Properties…. On the Usage tab you find:
- Enable background refresh: you can keep working while it runs. Turn it off when a macro must wait for the refresh to finish.
- Refresh every N minutes: for dashboards that watch a live database. Do not use it with large file sources.
- Refresh data when opening the file: opening the workbook always shows current data. Combine with Remove data from the external data range before saving the workbook to keep the file small.
- Refresh this connection on Refresh All: untick it for archival or very slow queries you want to refresh only on demand.
- Enable Fast Data Load (on the query's properties in the editor): loads faster but keeps Excel busy until the load finishes.
ThisWorkbook.RefreshAll is enough (see Your first macro: Sub, MsgBox, running it and a button). With background refresh off, the macro waits until all queries finish, so it can safely format or export the result afterwards.Refresh on Mac and on the web
Microsoft 365 for Mac refreshes queries with supported sources, using Data › Refresh All. Excel for the web can refresh queries whose sources are reachable from the cloud, such as tables in the workbook and files on SharePoint or OneDrive; queries that read local paths, on-premises databases without a gateway, or unsupported connectors cannot be refreshed there. If a workbook must refresh for everyone in a browser, keep its sources in the cloud.
A performance checklist
- Remove columns you do not need in the first steps.
- Filter rows as early as possible; on databases, keep those steps foldable.
- Group before merging when you only need totals from the second table.
- Merge on clean, typed, unique keys; remove duplicates in lookup tables first.
- Load large tables to the Data Model, not to sheets; set helpers to connection only.
- Buffer small lookup tables used by row-by-row functions.
- Turn off background previews in heavy workbooks.
- Sort only at the end, and only if the output order matters.
Check yourself
1. What is query folding?
Folded steps run on the database, so less data travels.
2. An Index column is added right after Source on a SQL table. What happens to the filters after it?
Once a step breaks folding, later steps run on your PC.
3. How can you check in Excel whether a step folds?
An available View Native Query shows the generated SQL.
4. Where should a 3-million-row fact table be loaded?
The Data Model handles millions of compressed rows; a sheet holds at most 1,048,576.
5. Which setting shows current data every time the file is opened?
It refreshes the connection on open.