Sub topic 2
Power Query Editor and applied steps
This chapter assumes you can open Power BI Desktop, use Get Data, and read a table of rows and columns. You do not need to know the M language yet. The chapter explains how Power Query records your work, so you can read and fix a query later.
What the Power Query Editor does
The Power Query Editor is a separate window in Power BI Desktop. You use it to shape data before it reaches your report. Open it from Home > Transform data, or choose Transform Data in the preview window after you pick a source in Get Data.
The window has four main areas:
- Queries pane on the left. It lists one query per source table.
- Data preview in the center. It shows a sample of the rows for the selected step.
- Ribbon on top. It holds the transform commands.
- Query Settings on the right. It shows the query name and the Applied Steps list.
If Query Settings is hidden, turn it on with View > Query Settings.
Applied steps are a recorded recipe
Every command you run adds a step to the Applied Steps list. A step is one named transformation, such as Source, Promoted Headers, or Changed Type. Power Query stores the steps in order. Each step takes the table produced by the step above it.
Power Query does not change the source file. It saves the recipe and replays it on each refresh. If next month's file has new rows, the same steps run on them.
Click any step to see the data as it looked after that step. This is the fastest way to find where a column went wrong. Selecting an earlier step and then adding a new command inserts a step in the middle, and Power Query asks you to confirm.
Steps are M code
Each step is a line of M, the language of Power Query. Open View > Advanced Editor to see them all. This query reads a CSV file:
let
Source = Csv.Document(File.Contents("C:\Data\orders.csv"), [Delimiter = ",", Encoding = 65001, QuoteStyle = QuoteStyle.Csv]),
Promoted = Table.PromoteHeaders(Source, [PromoteAllScalars = true]),
Typed = Table.TransformColumnTypes(Promoted, {{"OrderID", Int64.Type}, {"OrderDate", type date}})
in
TypedRead it as follows:
let ... inwraps a list of named steps.- Each line is
StepName = expression. - A step refers to the step before it by name.
PromotedusesSource, andTypedusesPromoted. - The name after
inis the step whose result becomes the query's output.
The names in the Applied Steps list are the same names used in the code. Rename a step by right-clicking it and choosing Rename. Power Query updates the references for you.
Editing and managing steps
Use the small icons and right-click menu on each step:
- Gear icon. Reopens the dialog for that step, so you can change a delimiter or an encoding without starting over.
- Delete. Removes the step. Later steps that depend on it may fail.
- Move up or down. Reorders steps, which can break a query if a later step needs a column that no longer exists at that point.
- Delete Until End. Removes the selected step and everything after it.
The formula bar above the preview shows the M for the selected step. Turn it on in View if you do not see it.
Common errors
- Hard-coded column names. Steps such as Changed Type name columns explicitly. If the source renames a column, the refresh fails with an error like
The column 'Amount' of the table wasn't found. - Reordering a step that others depend on. Moving Changed Type above Promoted Headers makes it refer to columns that do not exist yet.
- Expecting the source to change. Edits in Power Query never alter the Excel or CSV file.
- Forgetting to apply. Changes reach the report only after Home > Close & Apply.
Exercise
A query has the steps Source, Promoted Headers, and Changed Type. After a refresh, Changed Type fails because it cannot find the column "Amount". Where do you look first, and what is the likely cause?
Show answer
Click the Promoted Headers step and check the column names in the preview. The source file probably renamed or removed the "Amount" column, so the Changed Type step, which names that column explicitly, can no longer find it. Fix the source or edit the step.