Sub topic 10
Loading to a Table or Connection Only and Refreshing
This chapter covers what happens after the last Applied Step. You choose where a query's result goes, and you keep it current when the source changes. It assumes you can build a query in the Power Query Editor, read the Applied Steps pane, and work with Excel tables.
Where a query result can go
Closing the editor with Close & Load sends the result to the default destination, usually a new worksheet table. Close & Load To... opens the Import Data dialog, where you choose among these options:
| Option | What it creates | Typical use |
|---|---|---|
| Table | An Excel table on a worksheet | Data people read, filter, or reference with formulas |
| PivotTable Report | A PivotTable backed by the query | Summaries built straight from the result |
| PivotChart | A PivotChart backed by the query | Charts built straight from the result |
| Only Create Connection | No worksheet output | Staging queries and queries that feed other queries |
You can also tick Add this data to the Data Model. The Data Model is an in-memory store inside the workbook. Use it when you want relationships between tables, or when the data has more rows than a worksheet can hold (1,048,576).
Table output can go to a new worksheet or to an existing cell. If you pick an existing cell, the table starts there. Leave room below and to the right, because a refresh can change the table's size.
Connection only and staging queries
A connection-only query runs when something depends on it, but it writes nothing to a worksheet. It is the standard tool for staging, where several queries share one cleaned source.
Suppose you import SalesRaw once, fix types and trim text, and then build three outputs from it: a merge with a customer list, a Group By by region, and an appended yearly table. You make the cleaned query connection only. The three outputs reference it. The shared steps live in one place, so a fix applies to all three.
To create a reference, right-click a query in the Queries & Connections pane and choose Reference. The new query starts from the output of the original. Duplicate copies all the steps instead, so the two queries are independent afterward.
let
Source = SalesClean
in
SourceThis is what a referencing query looks like in the Advanced Editor. SalesClean is another query, and Source is its output.
A referenced query is evaluated again for each query that uses it during a refresh. Staging therefore improves maintainability more than speed. Chapter material on query folding returns to this point.
Changing the load destination later
You are not locked into your first choice. In the Queries & Connections pane, right-click a query and choose Load To.... You can switch a table to connection only, or the reverse.
When you switch a loaded query to connection only, Excel removes the worksheet table. Formulas that pointed at that table then return #REF!. Check dependent formulas before you change the destination.
Within the Power Query Editor, Home > Close & Load To... does the same thing. The same pane shows each query's row count and load status, so check it when a query seems to produce nothing.
Refreshing
Refresh re-runs the query against the current source and replaces the output. The Applied Steps are not changed. The main commands are:
- Data > Refresh All refreshes every query and connection in the workbook.
- Data > Refresh (the arrow beneath it) refreshes only the selected query.
- Right-click inside a loaded table and choose Refresh to refresh just that table.
- Refresh in the Queries & Connections pane refreshes a single query.
Refresh replaces the loaded rows. Anything you typed into the output table is overwritten. Keep manual notes in separate columns outside the table, or in another sheet that uses lookups.
Refresh options
Open Data > Queries & Connections, right-click a query, and choose Properties. These settings control refresh behavior:
- Enable background refresh lets you keep working while the refresh runs.
- Refresh every N minutes re-runs the query on a timer while the workbook is open.
- Refresh data when opening the file runs the query each time the workbook opens.
- Refresh this connection on Refresh All controls whether the query is included in Refresh All. Clear it for rarely needed queries that are slow.
Excel may ask to trust the data source, or to enable external content, when the file opens. These prompts are security settings, not errors.
What a refresh depends on
A refresh re-reads the source exactly as the steps describe it. It does not reconsider choices you made on a sample. Four situations cause most problems:
- The source file moved or was renamed. A step such as
File.Contents("C:\Data\sales.csv")fails with a message that the file cannot be found. Fix the path in the Source step, or use a parameter (covered in a later chapter). - A column was renamed or removed at the source. A later step that names the column raises
Expression.Error: The column 'Amount' of the table wasn't found.Open the failing step and update the name. - New values break an earlier type. If a number column starts to contain
N/A, rows in that column showError. The Changed Type step does not fail. The individual cells do. - The source is an Excel table that you resized. An Excel table source follows the table. A fixed range does not, so convert ranges to tables first.
Credentials are another cause. Excel stores them per data source. If they change, use Data > Get Data > Data Source Settings to edit or clear the permissions.
How a refresh flows
Excel works out the dependencies from the query references. Staging runs first, and the outputs follow. You do not set the order yourself.
PivotTables and refresh
A PivotTable built on a query's output does not update the moment the query does. Refresh All refreshes the query first, then the PivotTable cache, so use it when a PivotTable depends on a query. If you refresh only the PivotTable, you get the query's previous output.
A worked example
You have SalesRaw in the workbook and want a connection-only staging query plus one output.
- Select a cell in
SalesRawand choose Data > From Table/Range. - In the editor, set types and trim
Region, as in the first code block below. Rename the query toSalesClean. - Choose Home > Close & Load To... > Only Create Connection, then click OK.
- In the Queries & Connections pane, right-click
SalesCleanand choose Reference. Rename the new querySalesByRegion. - Group by
Regionwith a sum ofAmount. - Choose Close & Load To... > Table, on a new worksheet.
let
Source = Excel.CurrentWorkbook(){[Name="SalesRaw"]}[Content],
Typed = Table.TransformColumnTypes(Source, {{"Date", type date}, {"Amount", type number}}),
Trimmed = Table.TransformColumns(Typed, {{"Region", each Text.Trim(Text.Clean(_)), type text}})
in
TrimmedAdd a row to SalesRaw, then choose Data > Refresh All. The SalesByRegion table updates without any change to the steps.
let
Source = SalesClean,
Grouped = Table.Group(Source, {"Region"}, {{"TotalAmount", each List.Sum([Amount]), type number}})
in
GroupedCommon mistakes
- Loading every staging query to a sheet. This clutters the workbook and enlarges the file. Make intermediate queries connection only.
- Editing the output table by hand. The next refresh discards the edits.
- Refreshing only a PivotTable. It will not pick up new source data. Use Refresh All.
- Using fixed ranges as sources. New rows are missed. Use a table.
- Ignoring the load status. A query that loaded with errors shows a count of error rows in the Queries & Connections pane. Open it and fix the cause.
Exercise
You have a query named Staging that cleans a CSV, and two other queries that reference it. You want only the two outputs to appear on worksheets. Which load option do you choose for Staging, and what happens when you click Refresh All?
Show answer
Choose Only Create Connection for Staging. On Refresh All, Excel reads the CSV through Staging and updates the two output tables. Staging itself has no worksheet table.
Exercise
A colleague types a correction directly into a loaded query table. After the next refresh, the correction is gone. Why, and where should the fix be made?
Show answer
A refresh replaces the output table with the query result, so manual edits are overwritten. Make the fix at the source or as a step in the query, for example a Replace Values step.