Some more savvy Excel users know that you can break text onto multiple lines in a cell by pressing Alt+Enter mid entry. Today’s post explores how we can split by line breaks in order to break these types of cell contents into multiple columns.
Set up the data
To start with, let’s set up some simple data:
- In cell A2, type “Text” and press Enter
- In cell A3 type “This” –> Alt + Enter –> “is” –> Alt + Enter –> “text” –> Enter
The result should look like this:
And now we’ll go and pull it in to Power Query:
- Select the data –> create new query –> From Table
Split by Line Breaks
At this point, you’d certainly be forgiven for thinking that only the first line was pulled in. But if you select the cell, you’ll see in the preview window that all the data is there:
So let’s try and split it up.
- Right click the Text column –> Split Column –> By Delimiter
Unfortunately, there is no line break or carriage return option in the dialog, which means that you’ll need to pick “Custom”, and enter the special character for a Line Feed:
Even worse, with entering this, Power Query is overly aggressive when you click OK. It assumes that this is special text, so escapes it to text, and appends some commands that actually mess you up:
Notice how we have two columns with nothing in the second. What gives there?
To correct this code, we need to modify the formula in the formula bar to do two things:
- Undo the escaping that Power Query did on our #(lf) entry, and
- Remove the code that is telling which columns to import
So first, we need to replace:
And second, we need to remove this completely:
And the results are much better:
The Good/Bad News
The bad news is that currently it’s a bit painful to do this. The good news is that it can be done, and the better news is that Power Query is constantly being updated. I’m sure it won’t be long before they give us an easier to use/more discoverable mechanism to make this work.
Other Special Characters
Should you need them, here are three special characters that you can refer to in Power Query:
- Line feed: #(lf)
- Carriage return: #(cr)
- Tab: #(tab)