Sub topic 1
Power Query in Excel: the editor and queries
Prerequisites
This chapter assumes you can work with Excel tables and the Data ribbon. You should know what a CSV file and an .xlsx file are. You need Excel 2016 or later, or Microsoft 365, because Power Query is built in. In Excel 2010 and 2013 it is a separate add-in, and this tutorial does not cover it.
What Power Query does
Power Query is Excel's tool for getting data from a source, reshaping it, and loading it into a worksheet or the Data Model. You do not change the source. You record a list of steps, and Excel replays them each time you refresh.
That recorded list is what makes the later chapters work. When you combine a folder of monthly files, you build the steps once. Next month, a refresh applies the same steps to the new files.
The query
A query is a named, saved set of steps that produces one table. Every query has three parts:
- A source, such as a file, a folder, or a database.
- A sequence of transformation steps, such as removing columns or changing types.
- A destination, such as a worksheet table or a connection only.
Queries are stored inside the workbook. They appear in the Queries & Connections pane, which you open from Data > Queries & Connections.
The Power Query Editor
The Power Query Editor is a separate window where you build and edit queries. Open it by choosing Data > Get Data > Launch Power Query Editor, or by double-clicking a query in the Queries & Connections pane.
The window has these main areas:
| Area | Where | What it does |
|---|---|---|
| Ribbon | Top | Commands for transforming, adding columns, and viewing |
| Queries pane | Left | Lists every query in the workbook |
| Data preview | Center | Shows the table after the selected step |
| Formula bar | Above the preview | Shows the M formula for the selected step |
| Query Settings | Right | Holds the query name and the Applied Steps list |
If the formula bar is hidden, turn it on with View > Formula Bar. If Query Settings is hidden, use View > Query Settings.
Applied Steps
The Applied Steps list is the record of what the query does. Each step is one transformation. Click a step and the preview shows the table as it looked after that step.
Steps run from top to bottom, and each one takes the result of the step above it. Because of this, deleting or reordering an early step can break the later ones. Excel warns you when a change could affect other steps.
Behind each step is a line of code in the M language. You do not need to write M yet. For now, only notice that selecting a step shows its formula.
How a query flows
The diagram shows the path from source to worksheet.
Loading and the Close & Load options
When your steps are ready, choose Home > Close & Load. This closes the editor and sends the result to the workbook. Close & Load To lets you pick a destination:
- Table: writes the result to a worksheet.
- Only Create Connection: keeps the query but loads no data. Helper queries often use this.
- Add this data to the Data Model: loads the result for use in PivotTables and Power Pivot.
To pick up changes in the source, use Data > Refresh All. Excel reruns every step against the current source.
Try it
Open Excel, select any table, and choose Data > From Table/Range. The editor opens with a query that has two steps, Source and Changed Type. Select each step and watch the formula bar. Then rename the query in Query Settings and choose Close & Load.
Exercise
You delete the third step in a query's Applied Steps list. Why might the steps below it fail, and does the source file change?
Show answer
Each step uses the output of the step before it, so later steps may refer to columns or values that the deleted step created or changed. The source file does not change, because a query only records steps and reads the source.
Exercise
Which load option would you choose for a query that only supports other queries and should not appear as its own table on a worksheet?
Show answer
Only Create Connection. It keeps the query available to other queries without writing its data to a worksheet.