Sub topic 1
Tour of the Power Query Editor
Prerequisites
This chapter assumes you can do the following:
- Open Power BI Desktop and connect it to a simple source such as a CSV or Excel file.
- Describe a table in terms of rows, columns, and common data types (text, whole number, decimal, date, true/false).
- Recognize that Power BI keeps a data model separate from the raw files it reads.
You do not need to know the M language yet. The next chapter covers it. Here you learn where everything lives in the editor, so later chapters can say "open the Queries pane" or "check the formula bar" without explanation.
What the editor is for
The Power Query Editor is a separate window inside Power BI Desktop. It sits between your data source and your data model. You describe how to reshape the data, and Power BI replays those instructions every time the data refreshes.
The editor never changes the source file. It stores a recipe of steps. When you refresh, Power BI reads the source again and runs the recipe from the top. This is why cleaning in Power Query scales better than editing a spreadsheet by hand.
Opening the editor
There are three common ways to open it.
- On the Home tab in Power BI Desktop, select Transform data. This opens the editor with all existing queries.
- After choosing a file in Get data, select Transform Data in the preview dialog instead of Load. Choosing Load skips cleaning and sends the raw data straight to the model.
- In the Data view or the Fields pane, right-click a table and choose Edit query.
A new window opens with its own ribbon, panes, and preview area. Power BI Desktop stays open behind it.
The main areas of the window
The editor has five regions. Learn their names, because the rest of this tutorial refers to them.
| Region | Location | What it does |
|---|---|---|
| Ribbon | Top | Tabs for Home, Transform, Add Column, View, Tools, and Help. Holds the commands. |
| Queries pane | Left | Lists every query in the file. Select one to work on it. |
| Data preview | Center | Shows a sample of the selected query's current result. |
| Formula bar | Above the preview | Shows the M expression for the selected step. |
| Query Settings pane | Right | Shows the query name, its properties, and the Applied Steps list. |
The ribbon
Each tab groups commands by purpose.
- Home has Close & Apply, Refresh Preview, Remove Rows, Keep Rows, Split Column, Group By, Use First Row as Headers, Merge Queries, Append Queries, and Manage Parameters.
- Transform works on existing columns. It includes data type, Replace Values, Unpivot Columns, Fill, and the Format menu for trimming and case.
- Add Column creates new columns and leaves the originals alone. It holds Custom Column and Conditional Column.
- View turns panes and profiling options on and off.
The Transform and Add Column tabs contain many of the same operations. The difference is that Transform changes the selected column in place, while Add Column keeps the original and adds a new one.
The Queries pane
Each query is one table-producing recipe. A file often has several: one for each source table, plus helper queries. Right-click a query to rename, duplicate, reference, or delete it. You can also group queries into folders. Chapter 14 covers organizing queries in depth.
A query name becomes the table name in the data model. Rename it now, before you build reports on it.
The data preview
The preview shows a sample, not necessarily every row. For large sources, Power Query loads only the first part of the data for display. This keeps the editor fast. Remember this when you check results. A column can look clean in the sample and still contain problems further down.
Each column header shows an icon that gives its data type. Examples are ABC for text, 123 for whole number, 1.2 for decimal, a calendar for date, and ABC123 for the "any" type. The any type means no type has been set. Chapter 5 covers types in detail.
The Query Settings pane
This pane has two parts. Properties holds the query name. Applied Steps lists every transformation in order. If the pane is hidden, open it from View > Query Settings.
The formula bar and the Advanced Editor
The formula bar shows one line of M code. It updates as you click different steps. If it is hidden, turn it on with View > Formula Bar.
For the whole query, use Home > Advanced Editor. It shows every step as M code in a single window.
Here is a small query that you can paste into the Advanced Editor to explore the layout. Create a blank query first: Home > New Source > Blank Query, then open the Advanced Editor and replace its contents.
let
Source = #table(
{"Product", "Region", "Units"},
{{"Widget", "West", 10}, {"Gadget", "East", 4}, {"Widget", "East", 7}}
),
ChangedType = Table.TransformColumnTypes(Source, {{"Units", Int64.Type}}),
FilteredRows = Table.SelectRows(ChangedType, each [Units] > 5)
in
FilteredRowsSelect Done. Look at the Applied Steps pane. It lists Source, ChangedType, and FilteredRows. Click each one and watch the formula bar and preview change.
The query returns two rows, the Widget rows, because the Gadget row has 4 units. Click Source to see all three rows again.
Working with the Applied Steps list
Applied Steps is the editor's history of what you did to the data. You can use it in several ways.
- Click a step to see the data as it looked after that step. Later steps are still stored. They are just not shown.
- Rename a step with right-click > Rename, or F2. Clear names make a long query readable.
- Delete a step with the X icon next to it, or right-click > Delete. Power Query warns you if later steps depend on it.
- Reorder steps by dragging. This can break the query if a later step needs a column that an earlier step creates.
- Insert a step by selecting an earlier step and applying a transformation. Power Query asks whether to insert it there.
A gear icon next to a step means it has a dialog. Click it to change the settings without rewriting the step.
Common mistakes in the editor
- Treating the preview as the full data. The sample can hide problems. Use column profiling over the entire data set, which a later chapter covers.
- Editing the wrong query. The Queries pane highlights the selected query. Check the name at the top of Query Settings before you start.
- Loading before cleaning. Selecting Load in the connection dialog sends raw data to the model. You can still reopen the editor later, but any reports built on the raw column names will need fixing.
- Hidden panes. If the Applied Steps list or formula bar is missing, nothing is wrong. Turn it back on under the View tab.
- Forgetting that unsaved edits are not in the model. Changes made in the editor affect the model only after Close & Apply.
Checking your understanding
Exercise
You open the editor on a query, click the second step in Applied Steps, and the preview shows fewer columns than the last step. Has Power Query deleted your later work? Where would you look to see the code for the step you clicked?
Show answer
No. Clicking an earlier step only shows the data as it was after that step. The later steps are still stored and still run when the query refreshes. The formula bar shows the M expression for the selected step. The Advanced Editor on the Home tab shows the code for every step.
Exercise
A coworker says the editor changed the original CSV file when they removed a column. Is that correct, and what actually happens when the report refreshes?
Show answer
It is not correct. The editor never writes back to the source file. It records a removal step. On refresh, Power BI reads the source again and replays all the recorded steps, so the column is removed again each time.