Sub topic 11
Loading the result and refreshing as files change
This chapter assumes you can build a combined folder query with Combine & Transform Data. It also assumes you know what the Power Query editor's Close & Load button does. Here you decide where the result goes and how it stays current as files arrive, change, or disappear. You also learn what a refresh does and what can break it.
Choosing where the result goes
When you select Close & Load, Excel loads the query as a table on a new worksheet. Close & Load To... opens the Import Data dialog, which has more options:
| Option | What it creates | Use it when |
|---|---|---|
| Table | An Excel table on a worksheet | You want to see, filter, or reference the rows with formulas |
| PivotTable Report | A PivotTable built on the query | You only need summaries |
| PivotChart | A PivotChart built on the query | You only need a chart |
| Only Create Connection | Nothing visible, only the saved query | The query feeds another query, or you load it later |
| Add this data to the Data Model | A copy in the Data Model | The data is large, or you plan to use relationships and measures |
Only Create Connection suits the helper queries Excel generates. The sample file, parameter, and function queries are already set to connection only. The combined query is the one you normally load.
A worksheet holds at most 1,048,576 rows. A folder of many monthly exports can pass that limit. If you expect that, load to the Data Model or a PivotTable instead of a table.
What a refresh does
A loaded query stores the rows from its last run. It does not read the folder live. A refresh reruns every step of the query from the top:
Folder.Filesreads the folder again, so it sees the current file list.- Your filter steps decide which of those files stay.
- The generated function runs once per remaining file.
- The combined rows replace the old rows in the worksheet table.
Because the whole query reruns, a refresh handles each change to the folder the same way:
- A new file with the right layout: its rows appear. Nothing else needs editing.
- A file replaced by a newer version with the same name: the old rows disappear and the new rows appear.
- A file removed or moved out: its rows disappear.
- A file renamed: the old rows disappear and the same rows appear again under the new name. The Source.Name value changes.
- A file added to a subfolder: it is included, because
Folder.Filesreads subfolders too. Filter on Folder Path if that is not what you want.
Manual edits in the loaded table do not survive a refresh. Corrections belong in the query steps. Formulas in columns next to the table are the exception, because Excel extends them when the table grows or shrinks.
Refreshing manually
You have several ways to start a refresh:
- Select a cell in the table, then use Query > Refresh or Table Design > Refresh.
- Right-click in the table and choose Refresh.
- Use Data > Refresh All to refresh every query and connection in the workbook.
- Open the Queries & Connections pane, right-click the query, and choose Refresh.
Refresh All also refreshes the helper queries and any PivotTables. A PivotTable built on a loaded table does not update when the table refreshes by itself. Use Refresh All, or refresh the PivotTable too.
The Queries & Connections pane shows a row count for each query after loading. It also shows a warning icon when a query has errors. Check the row count against what you expect after each refresh.
Automatic refresh settings
Open the settings with Data > Queries & Connections, right-click the query, choose Properties, and look at the Usage tab.
| Setting | Effect |
|---|---|
| Enable background refresh | Excel stays usable while the query runs. Turn it off when later steps or macros depend on the finished data. |
| Refresh every N minutes | Reruns the query on a timer while the workbook is open. |
| Refresh data when opening the file | Reruns the query each time someone opens the workbook. |
| Refresh this connection on Refresh All | Includes or excludes the query from **Refresh All**. |
| Fast Data Load | Speeds up the refresh but makes Excel unresponsive until it finishes. |
Refresh on open suits a report that people read but do not edit. It slows opening, so avoid it when the folder holds thousands of files.
Excel can only refresh while the workbook is open, so these settings do not run unattended. A workbook that nobody opens does not update. Scheduled refresh needs other tooling, such as Power Automate or a script on a server.
A short VBA macro can refresh on demand. Turn off background refresh first, or the macro continues before the data has finished loading:
Sub RefreshSales()
ThisWorkbook.Connections("Query - Sales").OLEDBConnection.BackgroundQuery = False
ThisWorkbook.Connections("Query - Sales").Refresh
End SubThe connection name is Query - followed by the query name. Check it in Data > Queries & Connections > Properties before you use it.
Limiting the files that load
A folder that grows each month makes every refresh slower. You can limit the work with a filter on Date modified in the file list. This query keeps only CSV files changed in the last 12 months:
let
Source = Folder.Files("C:\Data\Sales"),
CsvOnly = Table.SelectRows(Source, each [Extension] = ".csv"),
Recent = Table.SelectRows(CsvOnly, each [Date modified] >= Date.AddMonths(DateTime.LocalNow(), -12))
in
RecentThe filter changes which files are read. Rows from older files disappear from the result, so only use it when that is what you want. A file you edit or copy gets a new Date modified, so it can enter the window again. If you need history, filter on a date inside the data, not on the file's timestamp.
When a refresh fails or looks wrong
Refresh problems fall into a few groups. Read the message in the Queries & Connections pane, or open the query and look at the step that shows the error.
- A file is open in another program. A workbook someone has open in Excel can be locked, and its
~$owner file appears in the folder. Filter out names that start with~$, and ask people to close files before a refresh. - The folder path does not exist. A drive letter or network share differs on another computer. The message says the path could not be found. Parameterizing the path, covered in a later chapter, makes this easier to fix.
- Column not found. A new file uses a different header, so a step that refers to the old name fails. Fix the file or adjust the sample-file query.
- Type conversion errors. A cell holds text such as
N/Ain a column typed as number. The cell showsError. Clean the value in the sample file query before the type step, or replace errors. - Credentials or privacy levels. Excel may ask for permission to access the folder. If it warns that data sources may be combined, set the privacy level in Data > Get Data > Query Options > Privacy.
- Unexpected extra rows. A file you did not expect is in a subfolder or has the right extension but the wrong layout. Check the Folder Path and Name columns in the file list.
Checking a refresh with the source name
If you kept the source file name in the combined data, you can verify which files contributed. Create a PivotTable with the name column as rows and a count of rows as values. You can see at a glance if a new file is present, or if one that should be present is missing. This is a cheap check that works on every refresh.
Common mistakes
- Expecting the table to change when files change, without refreshing.
- Typing corrections directly into the loaded table.
- Leaving a type step before a cleaning step, which produces errors that cleaning could have prevented.
- Loading a very large result to a worksheet when the Data Model would handle it.
- Turning on refresh every N minutes for a query that reads a large network folder.
Exercise
You added a new CSV file with the correct layout to the folder, but your loaded table still shows the old rows. A PivotTable built on the table also looks stale. What do you do, and why?
Show answer
Refresh the query, for example with Data > Refresh All. The loaded table holds the results of the last run and does not read the folder live. Refresh All also refreshes the PivotTable, which does not update just because its source table refreshed.
Exercise
A refresh now fails because one workbook in the folder is open and its temporary file ~$Budget.xlsx appears in the list. Which step in the query prevents this, and what does the condition look like in M?
Show answer
A filter step on the file list that removes temporary files. In M, add a condition such as not Text.StartsWith([Name], "~$") to the Table.SelectRows call, together with the extension test.
Exercise
A colleague wants the report to update automatically every night, even if nobody opens the workbook. Which refresh settings can do this?
Show answer
None. The refresh settings only run while the workbook is open in Excel. Unattended refresh needs other tooling, such as a scheduled script or Power Automate on a machine that keeps the workbook open.