Sub topic 12
Reading the M code
This chapter assumes you can connect to a folder, filter the file list, and run Combine & Transform Data. Here you open the Advanced Editor and read the code those steps produce. M is the language behind every Power Query step.
Where to find the code
Select a query in the Queries pane, then choose Home > Advanced Editor. The Applied Steps list on the right shows the same code as a list of step names.
Every query has the same shape:
letstarts the list of steps.- Each step is
Name = expression, and steps are separated by commas. innames the one step that becomes the query's result. This is usually the last step.
A step can use any earlier step by name. That is how data flows from one step to the next.
The file list step
The first step reads the folder. A second step filters it.
let
Source = Folder.Files("C:\Data\Sales"),
CsvOnly = Table.SelectRows(Source, each [Extension] = ".csv" and not Text.StartsWith([Name], "~$")),
Kept = Table.SelectColumns(CsvOnly, {"Name", "Content"})
in
KeptFolder.Files lists every file in the folder and its subfolders. Table.SelectRows keeps the rows where the condition is true. The word each starts a small function that runs once per row, and [Extension] reads that column from the current row. The test on ~$ removes the temporary lock files that Excel creates for open workbooks.
M is case-sensitive. [Extension] = ".csv" will not match .CSV, so use Text.Lower([Extension]) if your files vary.
What the combine step does
Combine & Transform Data builds a function and calls it for each file. You can write the same idea by hand.
let
Source = Folder.Files("C:\Data\Sales"),
CsvOnly = Table.SelectRows(Source, each [Extension] = ".csv"),
Parsed = Table.AddColumn(CsvOnly, "Data", each Table.PromoteHeaders(Csv.Document([Content], [Delimiter = ",", Encoding = 65001]))),
Kept = Table.SelectColumns(Parsed, {"Name", "Data"}),
Expanded = Table.ExpandTableColumn(Kept, "Data", {"Date", "Region", "Amount"}),
Typed = Table.TransformColumnTypes(Expanded, {{"Date", type date}, {"Region", type text}, {"Amount", type number}})
in
TypedFollow the steps in order:
Parsedadds a column named Data. For each row,Csv.Documentreads the binary[Content]as CSV, andTable.PromoteHeadersturns the first row into column names. Each cell in Data now holds a whole table.Keptkeeps only the file name and the nested tables.Expandedunpacks each nested table into rows, repeating the fileNameon each one.Typedsets the column types.
How the generated queries map to this
In the generated version, the parsing in Parsed lives in a separate function query, usually named Transform File. The sample file query feeds that function. The combined query then calls the function for each Content value. Editing the sample file query changes the function, so the change applies to every file.
Reading tips
- Select a step in Applied Steps to see its output. Compare it with the code to learn what each function does.
- Step names that contain spaces appear as
#"Changed Type". This is normal. - If a step fails, the error appears on that step. Fix the earliest failing step first.
Exercise
In the hand-written query, which step produces a column where each cell contains a table, and which step turns those tables into rows?
Show answer
Parsed (Table.AddColumn with Csv.Document) creates the Data column of nested tables. Expanded (Table.ExpandTableColumn) turns them into rows and columns.
Exercise
Why does the filter in the first example include not Text.StartsWith([Name], "~$")?
Show answer
Excel creates temporary files whose names start with ~$ while a workbook is open. They are not real data files and would cause errors if combined, so the filter removes them.