Sub topic 15
Refresh behavior and performance notes
This chapter covers what Excel does when it refreshes queries that append and merge, and how to keep those refreshes fast. It assumes you can build an append and a merge, choose a join kind, and expand the merged column. It also assumes you know where to find the Queries & Connections pane and the Advanced Editor.
What a refresh actually does
A refresh does not update the old result in place. Power Query runs every step of the query again, from the source to the last step. It then replaces the loaded table. This has three consequences for appends and merges.
- New rows in a source appear in the result. Rows deleted from a source disappear.
- A step that worked yesterday can fail today. A new column name, a changed data type, or a missing file in an append can all cause errors.
- Cost grows with the size of the sources, not with the number of rows that changed. Power Query has no incremental refresh in Excel.
Refresh follows the dependency chain. If Combined appends Sales_2023 and Sales_2024, and Report merges Combined with Customers, refreshing Report evaluates all four queries, whether or not they are loaded.
Connection-only queries are not cached
Many people make the source queries connection only so they do not load twice. That saves worksheet space. It does not save refresh time, because a connection-only query stores no result. Every query that references it evaluates it again.
In this layout, one Refresh All can read the source files and run the append twice. With two references the cost doubles. With many references it becomes a real problem. The usual fixes are:
- Reduce the work that the shared query does, such as the number of files or the transformations on each file.
- Have only one query reference the shared query, and build the other outputs from that query's result.
- Load the shared query to a table and have the other queries read that table. This breaks the dependency chain, but you must refresh in the right order.
Query folding: the biggest factor
Query folding means Power Query translates your steps into one request in the source's own language, usually SQL. The database does the work and sends back only the result.
Both operations in this tutorial can fold.
- An append (
Table.Combine) can fold into aUNION ALL. - A merge (
Table.NestedJoinfollowed by an expand) can fold into aJOIN.
Folding works only when both inputs come from the same database connection. It also requires every step before the combine to be foldable. Excel workbook tables, CSV files, and web pages never fold. Power Query reads them fully and combines them in memory.
To check, right-click a step in Applied Steps. If View Native Query is available, the step folds. If it is greyed out, the step runs locally.
A foldable merge looks like this:
let
Source = Sql.Database("sqlserver01", "SalesDb"),
Orders = Source{[Schema = "dbo", Item = "Orders"]}[Data],
Recent = Table.SelectRows(Orders, each [OrderDate] >= #date(2024, 1, 1)),
Slim = Table.SelectColumns(Recent, {"OrderID", "CustomerID", "Amount"}),
Customers = Source{[Schema = "dbo", Item = "Customers"]}[Data],
CustSlim = Table.SelectColumns(Customers, {"CustomerID", "Name"}),
Merged = Table.NestedJoin(Slim, {"CustomerID"}, CustSlim, {"CustomerID"}, "Customers", JoinKind.LeftOuter),
Expanded = Table.ExpandTableColumn(Merged, "Customers", {"Name"})
in
ExpandedThe filter and column selection come before the merge, so the database does both and sends back a small result. If you add a step that cannot fold, such as a custom function, every step after it runs locally.
Reduce the data before you combine it
In-memory combines work best on small inputs. Apply these changes before the append or merge step.
- Filter rows early. Remove rows you will never use, such as old years or test records.
- Remove unneeded columns. Wide tables cost memory in both inputs. The merged table column carries every column of the lookup table until you expand it, so select only the columns you need.
- Fix data types on the key columns first. Matching text to text is faster than converting values during the join. It also avoids silent mismatches.
- Deduplicate the lookup side. If the lookup table has repeated keys, a merge multiplies rows. This costs time and also makes the result wrong. Chapter 11 covers checking for duplicates.
Prefer aggregating the nested column with Table.AggregateTableColumn over expanding it when you need only a total. The result then has one row per key, not one row per match.
Join cost and kind
Joins are not equally expensive.
| Situation | Effect on refresh | What to do |
|---|---|---|
| Inner or left outer join on one indexed key, same database | Folds, runs at the source | Nothing special |
| Merge of two Excel tables or CSV files | Runs in memory | Trim columns and rows first |
| Full outer join | Needs both inputs fully, no early stopping | Use it only when you need both sides' unmatched rows |
| Merge on several key columns | More comparisons per row | Keep keys to the minimum that identifies a row |
| Fuzzy merge | Compares each value with many candidates, never folds | Use only on small tables, and set a higher threshold |
| Append of a folder with many files | Reads and parses every file on each refresh | Filter files by name or date, and combine only what you need |
A fuzzy merge is the slowest option by a wide margin. Run a normal merge first for the exact matches. Then run a fuzzy merge only on the rows that did not match.
Buffering a lookup table
When a merge runs locally, Power Query may read the lookup input more than once while it evaluates. Table.Buffer loads a table into memory once and holds it there for the rest of that query's evaluation.
let
Customers = Excel.CurrentWorkbook(){[Name = "Customers"]}[Content],
CustomersBuffered = Table.Buffer(Customers),
Orders = Excel.CurrentWorkbook(){[Name = "Orders"]}[Content],
Merged = Table.NestedJoin(Orders, {"CustomerID"}, CustomersBuffered, {"CustomerID"}, "Customers", JoinKind.LeftOuter)
in
MergedPrivacy levels and the Formula.Firewall error
If you merge or append queries from different sources, Power Query may stop with the error Formula.Firewall: Query references other queries or steps, so it may not directly access a data source. The cause is privacy levels. Power Query will not send data from one source to another unless the sources' privacy levels allow it.
You can fix this by setting suitable privacy levels for each source in Data > Get Data > Query Options > Privacy. Another fix is to separate the work: one query per source that only loads data, and a later query that combines them. Setting privacy to Ignore removes the check but lets data from a private source reach other sources, so use it only on files you control.
Controlling when refresh happens
Excel offers several settings for each query connection. Open Queries & Connections, right-click the query, and choose Properties.
- Enable background refresh. The workbook stays usable while the query runs. Turn it off if another process must wait for the data. For example, a macro that reads the result right after refresh needs this off.
- Refresh every N minutes. This reruns the query on a timer. Use it only for light queries. A heavy merge refreshing every minute will slow the whole workbook.
- Refresh data when opening the file. This suits reports that must be current. It makes opening slow for heavy queries.
- Include in Refresh All. Clear this box for a rarely needed query so it does not run every time.
When you choose Refresh All, Excel decides the order itself. Do not rely on one loaded query finishing before another begins. If a query reads a loaded worksheet table that another query writes, the result can be one refresh behind. Make such queries reference the source query directly instead of the worksheet table.
Load destination and result size
Where you load the result affects the workbook as well as the refresh.
- A worksheet table is capped at about 1,048,576 rows, and large tables make the file big and slow to open.
- The Data Model holds much larger tables in compressed form. Use it when the result feeds PivotTables and does not need to be read cell by cell.
- A connection-only result has no storage cost, but, as noted above, it is recomputed by each query that uses it.
A bad merge can make this worse. If a left outer join multiplies rows because of duplicate keys, the loaded table grows without any error message. Compare row counts before and after.
A short checklist
- Check whether the combine step folds, using View Native Query.
- Filter rows and select columns before the append or merge.
- Set key column data types and clean the keys before the merge.
- Count how many queries reference each shared query.
- Reserve fuzzy matching and
Table.Bufferfor cases where you have measured a need. - Choose the load destination to match how the result will be used.
Exercise
Your workbook has a connection-only query Combined that appends 40 CSV files. Two other queries, Report and Summary, both reference Combined. A full refresh takes four minutes. Which refresh-related behavior explains why it is slower than reading the files once, and what is one change that helps?
Show answer
A connection-only query stores no result, so each query that references it evaluates it again. The 40 files are read and appended twice, once for Report and once for Summary. One fix is to load Combined to a worksheet table or the Data Model and build Report and Summary from that loaded result. Another is to make only one query read the files and derive the second output from it. Filtering to the files you need also reduces the cost of each evaluation.
Exercise
A merge between a SQL Server Orders table and a SQL Server Customers table is slow. In Applied Steps, View Native Query is available on the merge step but greyed out on the step that adds a custom column after it. What does this tell you, and what should you try?
Show answer
The merge folds and runs on the server, but the custom column cannot be translated to SQL, so that step and every step after it run locally in Power Query. Try moving the custom column to a replacement that can fold, such as a built-in conditional or arithmetic column. Alternatively, move the non-foldable step to after the filters and column selection so that fewer rows and columns reach it.