Sub topic 2
Applied Steps and the M code behind them
What you need before this chapter
This chapter assumes you can open Power BI Desktop, connect to a CSV or Excel file, and open the Power Query Editor. You should know what tables, rows, columns, and data types are.
How the editor records your work
Every transformation you make in the editor is saved as a step. The steps appear in order in the Applied Steps pane on the right. Each step is written in the Power Query formula language, called M. You don't have to type M, because the editor writes it for you as you click.
A query is a list of named steps. Each step takes the result of an earlier step and changes it. Refreshing the data replays the steps from the top against the new source data.
Reading the formula bar and Advanced Editor
Click any step and the formula bar shows its M expression. If the bar is hidden, turn it on in the View tab. To see the whole query, choose Advanced Editor on the Home tab.
The following query loads a CSV file, promotes the first row to headers, and sets column types.
let
Source = Csv.Document(File.Contents("C:\Data\sales.csv"), [Delimiter = ",", Encoding = 65001]),
PromotedHeaders = Table.PromoteHeaders(Source, [PromoteAllScalars = true]),
ChangedType = Table.TransformColumnTypes(PromotedHeaders, {{"OrderID", Int64.Type}, {"OrderDate", type date}, {"Amount", type number}}, "en-US")
in
ChangedTypeThe structure is always the same:
letstarts the list of steps.- Each line has the form
StepName = expression, and a comma ends every line except the last. innames the step whose result the query returns. This is usually the last step.
Most steps pass the previous step's name as their first argument. Table.PromoteHeaders(Source, ...) works on the output of Source. This is how the chain forms.
Working with the Applied Steps pane
The pane gives you several controls:
| Action | How | Effect |
|---|---|---|
| Inspect a step | Click it | Shows the data as it looked after that step |
| Rename a step | Right-click, then **Rename** | Changes the step name in the M code |
| Delete a step | Click the **X** beside it | Removes the step |
| Insert a step | Select an earlier step, then transform | Adds a step in the middle |
| Reorder | Drag the step | Changes the order of the steps |
A step with a gear icon has a dialog behind it. Click the gear to change its settings.
Making small edits to M
Readable step names make a long query easier to maintain. If you rename ChangedType to SetColumnTypes in the formula bar, the editor updates the later references for you. If you edit in the Advanced Editor, you must update every reference by hand.
Names with spaces must be written as #"Changed Type". The editor adds this form automatically. A name without spaces needs no quoting.
Common errors
- A missing comma at the end of a step line causes "Token Comma expected."
- A comma after the last step, just before
in, causes a syntax error. - A wrong step name in an expression causes "The name 'X' wasn't recognized."
- A hard-coded column name that no longer exists in the source causes a "column wasn't found" error on refresh.
Exercise
A query ends with ChangedType = Table.TransformColumnTypes(PromotedHeaders, {{"Amount", type number}}, "en-US"), followed by in ChangedType. The editor reports a syntax error. What is wrong, and how do you fix it?
Show answer
The last step line ends with a comma, but only the lines before the last step take one. Remove the trailing comma after the final step, so the line ends right before in.
Exercise
You delete the PromotedHeaders step from the query above, and the ChangedType step then shows an error. Why does it fail?
Show answer
ChangedType takes PromotedHeaders as its input, so removing that step breaks the chain. Also, the column names such as "Amount" only exist after the headers are promoted. Restore the step, or change ChangedType to point to Source.