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 builds on text cleaning, because stray spaces and mixed case break splits and merges.
Why split and merge
Source files often pack several values into one column, such as "Ada Lovelace" in a single FullName field. Reports usually need those parts separately, for example to sort by last name. The reverse also happens. You may need one key built from two columns, such as Region and Code.
Splitting turns one column into several. Merging turns several columns into one. Both are on the Transform tab and the Add Column tab.
Splitting a column
Select the column, then choose Split Column. The common options are:
| Option | Use it when |
|---|---|
| By Delimiter | Values are separated by a character such as a space, comma, or dash |
| By Number of Characters | Values have a fixed width, such as a 2-letter prefix |
| By Positions | Fields start at known character offsets |
| By Lowercase/Uppercase Digit | Boundaries fall where the case or digit-ness changes |
For a delimiter split, you also choose whether to split at the left-most, right-most, or each occurrence. You can split into columns or into rows.
let
Source = #table({"FullName", "Amount"}, {{"Ada Lovelace", 1500}, {"Alan Turing", 300}}),
SplitName = Table.SplitColumn(Source, "FullName", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"FirstName", "LastName"}),
AddedSize = Table.AddColumn(SplitName, "OrderSize", each if [Amount] >= 1000 then "Large" else "Small", type text)
in
AddedSizeThe SplitName step is the split. The list {"FirstName", "LastName"} names the new columns. The editor would otherwise call them FullName.1 and FullName.2. The last step adds a conditional column, which a later chapter covers.
A gotcha with the number of columns
When you split, the editor counts the pieces in the data it can see and writes that number into the M code. If a later refresh brings a value with three words, such as "Mary Jane Watson", the extra piece is dropped. To keep it, split at the left-most delimiter only, or edit the list of new column names. Review the generated names after every split.
Merging columns
Select two or more columns in the order you want them joined, then choose Merge Columns on the Transform tab. You pick a separator and a new column name. Merging replaces the original columns. If you want to keep them, use Add Column > Custom Column instead, or duplicate the columns first.
let
Source = #table({"Region", "Code"}, {{"West", 12}, {"East", 7}}),
TextCode = Table.TransformColumnTypes(Source, {{"Code", type text}}),
Merged = Table.CombineColumns(TextCode, {"Region", "Code"}, Combiner.CombineTextByDelimiter("-", QuoteStyle.None), "RegionCode")
in
MergedMerge Columns requires text. The editor converts non-text columns automatically when you use the menu command. In hand-written M, as above, convert them first or you get an error.
Null values behave in a way that surprises people. Merging with a null can produce a result that has an empty piece, such as West-, or an error in a custom formula that uses &. Replace nulls before you merge.
A typical order of work
Exercise
A refresh brings the value "Mary Jane Watson" into a FullName column that you previously split into FirstName and LastName with a space delimiter. What can go wrong, and how do you guard against it?
Show answer
The split step lists exactly two new column names, so the extra piece can be lost or placed wrongly. Split at the left-most delimiter only, so LastName holds "Jane Watson". You can also edit the new column list in the M code. Check the generated column names after each split.
Exercise
You merge Region and Code with a dash, but some Code values are null. Name one fix.
Show answer
Replace the nulls in Code, for example with "Unknown", before the merge. You can also filter out those rows if they are not needed.