Sub topic 3
Working in the Power Query Editor and Applied Steps
This chapter assumes you can work with Excel tables and understand basic data types (text, number, date). You should also have loaded at least one table or CSV into Power Query, as covered in the previous chapter. Here you will learn how the Editor is laid out, how Applied Steps record your work, and how to edit, reorder, and delete steps without breaking a query.
The layout of the Editor
Select any cell in an Excel table, then choose Data > From Table/Range. The Power Query Editor opens in its own window. It has five areas you will use constantly:
| Area | Location | Purpose |
|---|---|---|
| Ribbon | Top | Commands on the Home, Transform, Add Column, and View tabs |
| Queries pane | Left | Lists every query in the workbook. It can be collapsed. |
| Data preview | Center | Shows a sample of the current step's result |
| Formula bar | Above the preview | Shows the M code of the selected step |
| Query Settings pane | Right | Holds the query name and the Applied Steps list |
If the formula bar or Query Settings pane is missing, turn it on under View > Layout (Formula Bar) and View > Query Settings. Many problems that sound like "the steps disappeared" are just a hidden pane.
The data preview is a sample. For large sources the Editor loads only the first rows to keep the interface fast. Row counts and values you see in the preview may therefore differ from the final loaded table. The status bar at the bottom shows how many columns and rows the preview contains.
What Applied Steps are
Every action you take in the Editor is recorded as a named step in the Applied Steps list. A step is not a change made to your source data. It is an instruction that says "take the result of the previous step and do this to it." The source file or table is never modified.
When you refresh, Power Query starts from the source and replays the steps from top to bottom. This is why a query cleans next month's data with no extra work.
Typical steps from an early query look like this:
- Source: where the data comes from.
- Changed Type: data types applied automatically.
- Trimmed Text: a text cleanup you added.
- Filtered Rows: a filter you applied.
Selecting a step shows the table as it looked after that step ran. This makes Applied Steps a built-in history you can inspect. If a value looks wrong, click earlier steps until it looks right. The step just after that point is the one that caused the problem.
Steps are M code
Each step is one line of M, the language behind Power Query. Select a step and the formula bar shows its code. Open Home > Advanced Editor to see all steps together. Using the first example from this tutorial's plan:
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
TrimmedRead it from the top. Each line names a step and usually passes the previous step's name as its first argument. Typed takes Source, and Trimmed takes Typed. The in line names which step is the final result. In the Applied Steps list, the same steps appear with friendlier names such as "Changed Type" instead of Typed. You will learn to read M in more detail in a later chapter. For now, treat the Advanced Editor as a way to see exactly what the ribbon buttons wrote.
Editing, renaming, reordering, and deleting steps
Right-click any step to see the main options.
- Rename: Give a step a meaningful name, such as "Remove blank Region rows". Names with spaces appear in M as
#"Remove blank Region rows". Renaming is safe because Power Query updates later references for you. - Edit Settings: Reopens the dialog for that step. This is available for steps that have a gear icon beside them.
- Delete: Removes that step only.
- Delete Until End: Removes the step and all steps after it.
- Move Up / Move Down: Changes the order of steps.
- Insert Step After: Adds a new step that returns the previous step's table, which you can then change.
- Properties: Lets you add a description that shows as a tooltip.
Editing a step in the middle
When you select an earlier step and make a change, Power Query shows an Insert Step prompt. If you choose Insert, the new step goes in the middle and every later step runs on its result. This is useful, but later steps might refer to something you changed.
What breaks and why
Each step depends on the step before it. Three situations cause most errors:
- A column was renamed or removed earlier. A later step that names that column fails with "The column 'X' of the table wasn't found."
- Steps were reordered. Moving a "Changed Type" step above the step that creates the column it types causes the same error.
- A step was deleted that later steps relied on. For example, deleting a "Promoted Headers" step leaves columns named Column1, Column2, and so on, and every later step that mentions a real column name fails.
Power Query warns you before it deletes a step that has later steps. Read the warning. The Editor cannot predict that your change will break later steps, but it will show an error on the first step that fails.
Reading errors in the step list
When a step fails, its preview shows an error message in place of the table, and the step itself keeps the same position. Click the step just before it to confirm the data was fine up to there. Then fix the failing step with Edit Settings or the formula bar.
Two other things to know:
- A step can succeed while individual cells show
Error. That is a row-level problem, covered in the chapter on handling errors. It is different from a step-level failure. - A yellow warning triangle next to a step means the step was affected by data privacy settings or a similar message. Select the step for details.
Auto-generated steps you should review
Power Query often adds steps without being asked. Two common ones:
- Changed Type after loading a source. Power Query guesses types from the first rows. The guess can be wrong, for instance reading an ID like
00123as a number and dropping the leading zeros. Check it, or delete it and set types later on purpose. - Promoted Headers for sources whose first row looks like column names.
Because these steps run automatically, keep them in mind when you troubleshoot an unexpected result.
Good habits for step lists
- Rename steps that carry business meaning. "Filtered Rows" is unclear after five filters.
- Keep one purpose per step, so a failure points to one change.
- Set types once, near the end of cleanup, instead of after every step. Fewer type steps make the list shorter and easier to read.
- Use the Query Settings Name box to give the query a clear name, since that name becomes the table name in Excel.
- Do not delete the Source step. It defines where the data comes from. To change the source, use Edit Settings on it.
Closing the Editor
Choose Home > Close & Load to load the result to the workbook. Choose Close & Load To... to pick a different destination. Reopen the Editor any time with Data > Queries & Connections, then double-click the query. Your steps are saved with the workbook.
Exercise
A query has these Applied Steps in order: Source, Promoted Headers, Renamed Columns (Amt to Amount), Changed Type (sets Amount as number). You move Renamed Columns to the bottom of the list, below Changed Type. What happens, and how do you fix it?
Show answer
Changed Type now runs before the rename, so it looks for a column called Amount that does not exist yet (the column is still called Amt). The step fails with "The column 'Amount' of the table wasn't found." To fix it, move Renamed Columns back above Changed Type, or edit Changed Type to refer to Amt.
Exercise
You select the fourth step in Applied Steps and see the correct data. The sixth step shows an error. What is the most efficient way to find the cause?
Show answer
Click the fifth step. If it shows data, the error comes from the sixth step. If the fifth step also fails, move back one step at a time until you find the last good step. The step right after that one is the cause. Then use Edit Settings or the formula bar to correct it.