Sub topic 14
Reading the M code for append and merge
This chapter assumes you can load tables into the Power Query Editor and have already built an append and a merge with the buttons. Here you read the M code those buttons write and learn to edit it safely.
Where to see the code
Open the Advanced Editor from the Home tab to see the whole query. Each applied step is one line inside a let ... in block. The formula bar shows a single step when you select it. Every step names its result, and later steps refer to earlier ones by that name.
Append is Table.Combine
Append Queries writes a call to Table.Combine. It takes one list of tables, written in curly braces.
let
Source = Table.Combine({Sales_2023, Sales_2024})
in
SourceColumns are matched by name, not by position. A column that exists in only one table is kept, and the rows from the other table get null in it. Appending three or more queries only adds names to the list.
Merge is Table.NestedJoin
Merge Queries writes Table.NestedJoin. Its arguments, in order, are:
- The first table.
- The key column names from the first table, as a list.
- The second table.
- The key column names from the second table, as a list.
- The name of the new column that holds the matched rows.
- The join kind.
let
Source = Table.NestedJoin(Orders, {"CustomerID"}, Customers, {"CustomerID"}, "Customers", JoinKind.LeftOuter),
Expanded = Table.ExpandTableColumn(Source, "Customers", {"Name", "Region"}, {"Name", "Region"})
in
ExpandedThe merge step creates a column where each cell is a whole table. The second step, Table.ExpandTableColumn, takes that column name, the columns to bring in, and the names they get in the result.
The join kind is a value such as JoinKind.LeftOuter, JoinKind.Inner or JoinKind.LeftAnti. Changing that one word changes which rows survive. Keys are lists, so a multi-column merge needs matching lists of equal length, and the order of the names must line up.
let
Source = Table.NestedJoin(Prices, {"Region", "Product"}, Targets, {"Region", "Product"}, "Targets", JoinKind.Inner)
in
SourceEditing the code
Some changes are quicker in code than in the dialogs:
- Swap the join kind by editing the
JoinKindvalue. - Replace a step's input name to rewire the pipeline, for example point
Table.Combineat a new query. - Use
Table.AggregateTableColumninstead of an expand, so each customer keeps one row.
let
Source = Table.NestedJoin(Customers, {"CustomerID"}, Orders, {"CustomerID"}, "Orders", JoinKind.LeftOuter),
Totals = Table.AggregateTableColumn(Source, "Orders", {{"Amount", List.Sum, "Total Amount"}})
in
TotalsFuzzy merges use Table.FuzzyNestedJoin, which has the same shape plus an options record at the end, such as [IgnoreCase = true, Threshold = 0.8].
Common errors
- Expression.Error: The column 'X' of the table wasn't found. A name in the code does not match the column. M is case-sensitive, so
customeridis notCustomerID. - Expression.Error: The name 'Sales_2025' wasn't recognized. The query name is misspelled or the query was renamed or deleted. Renaming through the Queries pane updates references. Typing a new name by hand does not.
- Wrong column count after an expand. The expanded names are fixed in the step. If the source table gains a column later, the new column does not appear until you edit the list.
- Unequal key lists.
{"A", "B"}against{"A"}fails. Both lists need the same number of names.
Reading a step as a pipeline
Read the code from top to bottom, one line at a time. Ask what table goes in, what changes, and what comes out. Selecting a step in the Applied Steps pane shows the table at that point, which makes it easy to find the line where rows were lost or doubled.
Exercise
You want a merge that returns only the customers who never placed an order, and you are editing the M code by hand. Which function and join kind do you use, and which table comes first?
Show answer
Use Table.NestedJoin with JoinKind.LeftAnti, and put Customers first: Table.NestedJoin(Customers, {"CustomerID"}, Orders, {"CustomerID"}, "Orders", JoinKind.LeftAnti). A left anti join keeps only the rows of the first table that have no match in the second.