Sub topic 10
Splitting and merging columns
This chapter assumes you can open the Power Query Editor, read the Applied Steps pane, and set data types. It also assumes you can read a simple M step such as Table.TransformColumnTypes. Splitting and merging change the shape of a table, so you will see how the editor writes them in M and where they fail without warning.
When to split and when to merge
Source systems often pack several facts into one text column. Examples are WID-2024-EU (product, year, market) and Lovelace, Ada (last name, first name). Splitting gives each fact its own column, so you can filter, group, and type it correctly.
Merging does the opposite. You combine columns to build a display name, a composite key for a relationship, or an address line.
Both operations work on text. Splitting a number or date column makes the editor convert it to text first. Set data types again after a split.
Splitting a column
Select the column, then choose Home > Split Column or Transform > Split Column. The options are:
| Option | Use it when | Splitter function |
|---|---|---|
| By Delimiter | A separator such as `-`, `,`, or a space divides the parts | `Splitter.SplitTextByDelimiter` |
| By Number of Characters | Parts have a fixed width | `Splitter.SplitTextByRepeatedLengths` |
| By Positions | You know the exact character offsets | `Splitter.SplitTextByPositions` |
| By Lowercase to Uppercase | Values like `NorthWest` need separating | `Splitter.SplitTextByCharacterTransition` |
| By Digit to Non-Digit | Values like `A12B` mix letters and numbers | `Splitter.SplitTextByCharacterTransition` |
The delimiter dialog has three choices that matter:
- Split at: the left-most delimiter, the right-most delimiter, or each occurrence. The first two make exactly two columns. Use right-most to peel a file extension off a name that contains other dots.
- Quote character: whether a delimiter inside quotes counts. Pick None when quotes in your data are ordinary characters.
- Split into: Columns or Rows.
A worked example
The query below splits a product code on - and a name on , . It then sets the type of the year column and builds a full name.
let
Source = #table(
type table [Code = text, Name = text],
{{"WID-2024-EU", "Lovelace, Ada"}, {"GAD-2023-US", "Turing, Alan"}}
),
SplitCode = Table.SplitColumn(
Source,
"Code",
Splitter.SplitTextByDelimiter("-", QuoteStyle.None),
{"Product", "Year", "Market"}
),
SplitName = Table.SplitColumn(
SplitCode,
"Name",
Splitter.SplitTextByDelimiter(", ", QuoteStyle.None),
{"LastName", "FirstName"}
),
Typed = Table.TransformColumnTypes(SplitName, {{"Year", Int64.Type}}, "en-US"),
AddedFullName = Table.AddColumn(
Typed,
"FullName",
each Text.Combine({[FirstName], [LastName]}, " "),
type text
)
in
AddedFullNameTable.SplitColumn takes four arguments: the table, the column to split, a splitter function, and the list of new column names. The editor names new columns Code.1, Code.2, and so on. Rename them in the dialog or in the formula bar, because the default names are not meaningful in a model.
The fixed column count
The last argument of Table.SplitColumn is a hard-coded list of names. When you split in the UI, the editor scans the data and decides how many columns the split needs. It then writes that count into the step.
Suppose a later refresh brings the value GAD-2025-US-PROMO. The query has three names, so the fourth piece is dropped. Nothing errors, and the extra data disappears.
To protect against this:
- Profile the column before you split. Check the longest value, not only the first rows.
- Split to rows if the number of parts varies, as shown in the next section.
- After the split, add a check column or filter that looks for unexpected values in the last new column.
Splitting into rows
Use this option when a cell holds a list, such as red;green;blue in a Tags column. The editor splits the text, then expands the list so each item becomes its own row. Other columns in the row are repeated for each item.
let
Source = #table(
type table [Order = text, Tags = text],
{{"A1", "red;green"}, {"A2", "blue"}}
),
SplitToList = Table.TransformColumns(
Source,
{{"Tags", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), type {text}}}
),
ExpandedRows = Table.ExpandListColumn(SplitToList, "Tags")
in
ExpandedRowsThe result has three rows: A1/red, A1/green, and A2/blue. Row counts change, so a total taken from another column, such as an order amount, is now counted once per tag. Check your totals after a split into rows.
Merging columns
You have two ways to merge, and they differ in what happens to the source columns.
- Transform > Merge Columns replaces the selected columns with one new column. It writes
Table.CombineColumns. - Add Column > Merge Columns keeps the originals and adds the merged column. It writes
Table.AddColumn.
Select the columns in the order you want them joined. The dialog lets you pick a separator (space, comma, a custom string, or none) and a name for the new column.
let
Source = #table(
type table [City = text, State = text],
{{"Austin", "TX"}, {"Boise", "ID"}}
),
Merged = Table.CombineColumns(
Source,
{"City", "State"},
Combiner.CombineTextByDelimiter(", ", QuoteStyle.None),
"Location"
)
in
MergedThis returns one column, Location, with values Austin, TX and Boise, ID.
Merging and nulls
The & operator returns null if either side is null, so a custom column like each [First] & " " & [Last] loses the whole name when one part is missing. Text.Combine ignores null items in its list, which is why the first example uses it. Handle nulls with the techniques from the previous chapter before you merge, or test the result on rows where a part is missing.
Merging non-text columns
Text functions need text. If you merge Year (a number) with a text column using &, the step fails with a type error. Wrap the number in Text.From([Year]) or change the column to text first. A common case is a composite key built from an ID and a date. Format the date explicitly, for example Date.ToText([OrderDate], "yyyy-MM-dd"), so the key looks the same on every machine.
Common errors
- Extra data silently dropped. The split has fewer columns than the data needs. Profile first, as described above.
- New columns come out as text. Splitting always produces text. Set types after the split, using the same locale you used for the rest of the table.
- Leading zeros lost. If you convert a split piece such as
007to a whole number, you get7. Keep identifiers as text. - Extra spaces in pieces. Splitting on
,leaves a leading space on the second piece. Either include the space in the delimiter (", ") or trim the column afterwards. - A merge breaks after a rename. If you rename a column before the merge step, update later steps that reference the old name. Renames made in the UI update the steps for you. Edits made by hand do not.
Choosing the approach
Use the UI for the first pass, then read the generated M. If you see a hard-coded list of column names, ask whether new data could need more. If you merge on a column that may contain nulls, prefer Text.Combine in a custom column over the merge dialog or the & operator.
Exercise
A Path column holds values like North/Retail/Store12, and a future refresh might add a fourth level such as North/Retail/Store12/Kiosk. You split the column by / into three columns. What happens to the fourth level, and what would you do to avoid losing it?
Show answer
The editor writes a step with three hard-coded column names, so the fourth level is dropped on refresh with no error. To avoid this, profile the longest value before splitting and add a fourth column name, or split into rows when the depth varies. Another option is to split only at the left-most delimiter into two columns, so the remainder stays together in one column.
Exercise
A customer table has FirstName, MiddleName, and LastName, and many rows have a null MiddleName. Which expression builds a full name without losing rows to null, [FirstName] & " " & [MiddleName] & " " & [LastName] or Text.Combine({[FirstName], [MiddleName], [LastName]}, " ")?
Show answer
The Text.Combine version. The & operator returns null when any operand is null, so every row with no middle name would get a null full name. Text.Combine skips null items in the list and joins the rest with a single space.