Sub topic 1
Power Query in Excel: the editor and queries
Prerequisites
This chapter assumes you can do the following:
- Work with Excel tables and the Data ribbon.
- Recognize CSV and .xlsx files.
- Use Excel 2016 or later, or Microsoft 365, where Power Query is built in. In Excel 2016 and later it appears on the Data tab in the Get & Transform Data group.
You do not need to know the M language yet. Later chapters read and write M. This chapter builds the working model that the rest of the tutorial relies on: what a query is, where it lives, and how the editor records your work.
What Power Query does
Power Query is the data-import and cleanup engine in Excel. You point it at a source, such as a file, a folder, a web page, or a database. You then describe how the data should be reshaped. The result loads into a worksheet or into the Data Model.
The key idea is that Power Query stores a recipe, not a copy of the result. The recipe is called a query. When the source changes, you refresh the query and Excel replays the recipe against the new data. This is why Power Query suits folder work. Next month's file arrives, you refresh, and the new rows appear without any repeated manual steps.
Queries and steps
A query is a named, ordered list of steps. Each step takes the table produced by the previous step and changes it. Examples include removing a column, filtering rows, or changing a data type.
Three facts about queries matter throughout this tutorial:
- Steps run in order from top to bottom. A later step can only use columns that earlier steps produced.
- Each action in the editor adds a step. Power Query writes the matching M code for you.
- A query ends at one final table. That table is what gets loaded.
Here is a very small query as M code. You can see it later through the Advanced Editor, which this chapter describes below.
let
Source = Folder.Files("C:\Data\Sales"),
CsvOnly = Table.SelectRows(Source, each [Extension] = ".csv" and not Text.StartsWith([Name], "~$")),
Kept = Table.SelectColumns(CsvOnly, {"Name", "Content"})
in
KeptEach line between let and in is a step. The name on the left, such as Source or CsvOnly, is the step name. The line after in names the step whose result is the query's output. Here that step is Kept.
Notice that each step refers to the step before it. CsvOnly takes Source as its input. This chain is what the Applied Steps list shows in the editor.
Opening the Power Query Editor
The Power Query Editor is a separate window from the Excel grid. There are two usual ways to reach it:
- Start from a source. On the Data tab, choose Get Data, then pick a source. For a folder, that is From File, then From Folder. After you choose the source, a preview dialog offers Load and Transform Data. Transform Data opens the editor.
- Open an existing query. On the Data tab, choose Queries & Connections. This opens a pane on the right of the grid. Double-click a query there to open it in the editor.
Closing the editor with Close & Load sends the result to the workbook. Closing without loading keeps your steps but does not update the sheet.
Parts of the editor
The editor has five areas you will use constantly.
| Area | Where | What it does |
|---|---|---|
| Ribbon | Top | Holds commands such as Remove Columns, Filter Rows, and Combine Files. Tabs include Home, Transform, Add Column, and View. |
| Queries pane | Left | Lists every query in the workbook. Click one to work on it. |
| Data preview | Center | Shows the table at the currently selected step. It is a preview and may show only part of the data. |
| Formula bar | Above the preview | Shows the M expression for the selected step. |
| Query Settings pane | Right | Shows the query name and the Applied Steps list. |
If you do not see the formula bar or the Query Settings pane, turn them on from the View tab. You will want both. The formula bar shows what a step does, and Applied Steps lets you move between steps.
Applied Steps
Applied Steps is the visible record of the query. Clicking a step shows the table as it looked right after that step. This lets you check where a problem started.
You can do four things with a step:
- Rename it, so a name like
Filtered Rowsbecomes something meaningful. - Delete it with the X beside the step name.
- Reorder it by dragging. Do this carefully, because a step can break if it moves above the step that creates a column it uses.
- Insert a step in the middle by selecting an earlier step and making a change. Excel asks whether to insert a step. If you accept, later steps may fail if they depended on the old shape.
A gear icon next to some steps reopens the dialog that created them. This is the quickest way to change a filter value or a column choice.
Preview versus full data
The data preview is not the whole table. Power Query loads a sample of rows so the editor stays responsive. Column profiling, which shows quality and distribution above each column, may be based on only the first 1,000 rows by default. The status bar at the bottom lets you switch profiling to the entire data set.
This matters for folder work. A problem that appears only in row 50,000 of the third file will not show in the preview. It will show up when you load the result. Treat the preview as a guide, and verify the loaded table.
Query types you will meet
When you combine a folder, Excel creates more than one query. It helps to know the common kinds now.
- A table query returns a table. The final combined result is one.
- A function query takes inputs and returns a result. The combine step uses one to apply the same transformation to each file.
- A parameter query holds a single value, such as a file path, that other queries can read.
- A sample query holds one example file that you use to design the transformation.
The Queries pane groups these under folders that Excel creates, so a combined query's helper queries stay tidy. The next chapters explain each helper in detail. For now, remember that a single Combine action produces several linked queries, and editing one can change the result of another.
Load destinations and connections
A query can be used in different ways. These choices appear in the Import Data dialog or in Load To:
| Choice | Result |
|---|---|
| Table | Loads the result into a worksheet as an Excel table. |
| PivotTable Report | Loads the result straight into a PivotTable. |
| Only Create Connection | Saves the query without placing data in the workbook. |
| Add this data to the Data Model | Loads the result into the Power Pivot model, where it can handle more rows than a worksheet. |
Helper queries are usually set to Connection Only. They exist so that other queries can use them, not to fill a sheet. Only the final combined table normally needs to load.
Refreshing
Refresh reruns the recipe. You can refresh one query from the Queries & Connections pane or from the table's context menu. You can also use Data, then Refresh All, to refresh everything.
During a refresh, Power Query reads the source again, applies each step in order, and replaces the loaded table. Any manual edits you made in the loaded worksheet table are overwritten. Make changes in the query, not in the output cells.
How a query flows
The diagram shows how data moves from a source through the steps to a loaded table.
Common mistakes at this stage
- Editing the output sheet by hand. Refresh will overwrite your changes.
- Leaving default step names. Names like
Changed Type1make long queries hard to follow. - Trusting the preview alone. Check the loaded row count against what you expect.
- Deleting a helper query. Other queries depend on it, and deleting it causes errors. Excel warns you before it removes a query that others use, so read the dialog.
Exercise
You open a query, click the third step in Applied Steps, and change a filter. Later steps now show errors. What probably happened, and how do you find the problem?
Show answer
A later step depends on something the changed step used to produce, such as a column that is now gone or a value that no longer matches. Click each later step in order and read the error message. The first step that shows an error is the one that broke. Fix it with its gear icon or by editing its formula, or undo the change to the third step.
Exercise
You refresh a query and the manual notes you typed beside the loaded table disappear. Why does this happen, and what is a safer place for the notes?
Show answer
Refresh replaces the loaded table with the query's output, so anything typed inside the table is lost. Put notes outside the table's range, on another sheet, or in a separate table that does not refresh.