Power Query Lines.ToText: Step-by-Step Guide
In this article
Lines.ToText converts a list of text values into a single text string, joining each item with a line break by default. It is the go-to function when you have split text into lines (for example, after using Text.Split) and need to put it back together as one multi-line cell. You do not need any add-ins or special setup—just Power Query in Excel or Power BI Desktop. The function is available in all current versions, though the exact menu labels for launching the editor can differ slightly by version and platform.
What You'll Need
- Excel (Microsoft 365, Excel 2021, or Excel 2019) or Power BI Desktop.
- A data source already loaded into Power Query, or a blank query to experiment with.
- Basic familiarity with the Power Query Editor and the formula bar.
Step-by-Step
- Open Power Query Editor. In Excel, go to the Data tab and select Get Data > Launch Power Query Editor (or From Table/Range if your data is already in a worksheet). In Power BI Desktop, select Transform Data on the Home ribbon. The editor opens in a separate window.
- Create or load a query with a list column. If you are starting from a table, make sure the column you want to join contains text values. If you are experimenting, use Enter Data to create a small table with a few rows of text.
- Split the text into a list (if needed). Select the column, then go to Home > Split Column > By Delimiter. Choose the delimiter that separates your lines (for example, a comma or a line feed). After splitting, the column will contain a list of text values in each cell. Alternatively, you can use the formula bar to write
Text.Split([Column], "#(lf)")to split on line feeds. - Add a custom column for the joined text. Go to Add Column > Custom Column. In the dialog, enter a name such as "JoinedText" and use the formula
Lines.ToText([Column])where [Column] is the name of your list column. Click OK. A new column appears with each list converted to a single text value separated by line breaks. - Adjust the line separator if desired. Lines.ToText accepts an optional second argument: the separator string. For example,
Lines.ToText([Column], ", ")joins items with a comma and space instead of a line break. Use"#(lf)"for a line feed,"#(cr)"for carriage return, or"#(cr)#(lf)"for Windows-style line endings. - Remove the original list column (optional). Right-click the column header of the list column and select Remove. This keeps the query clean.
- Check the result. Click the cell in the new column to preview the joined text. It should show the original items separated by your chosen delimiter. If you see errors, proceed to Troubleshooting.
- Load the query. Go to Home > Close & Load (Excel) or Home > Close & Apply (Power BI) to bring the result back to your worksheet or data model.
Common Mistakes
- Passing a single text value instead of a list. Lines.ToText expects a list. If you feed it a plain text column, you will get an error. Use Text.Split first to create a list.
- Forgetting that null values cause errors. If the list contains nulls, Lines.ToText returns an error. Clean the list with List.RemoveNulls or replace nulls with empty text before joining.
- Using the wrong separator code. In Power Query, line breaks are written as
#(lf)(line feed),#(cr)(carriage return), or#(cr)#(lf)(both). Typing a literal newline in the formula bar will not work. - Expecting the function to handle non-text items. Lines.ToText works on text lists. If your list contains numbers, convert them to text first using List.Transform with Text.From.
Tips
- To join a list of values with a custom separator, use the second argument:
Lines.ToText(myList, "; "). - You can combine Lines.ToText with Text.Combine for more control, but Lines.ToText is simpler for line breaks.
- If you need to preserve line breaks in Excel cells, enable Wrap Text on the output column after loading.
- Use the Advanced Editor to see the full M code and adjust the function call directly.
- When splitting text, choose a delimiter that does not appear in your data to avoid accidental splits.
Troubleshooting
If Lines.ToText returns an error, check the following in order:
- Verify the input is a list. Click the cell in the step before the custom column; it should show a list icon (a small table with a list symbol). If it shows a text value, you need to split the column first.
- Remove nulls. Add a step to filter out nulls or use
List.RemoveNulls([Column])inside the custom column formula. - Check the separator syntax. Ensure you are using
#(lf)or#(cr)inside quotes, not an actual line break. - Convert numbers to text. If your list contains numbers, wrap the list with
List.Transform([Column], Text.From)before passing to Lines.ToText. - Refresh the preview. Sometimes the preview does not update immediately; click Refresh Preview on the Home tab.
If the function still fails, try simplifying: create a blank query and manually enter Lines.ToText({"a","b"}) in the formula bar to confirm the function works in your environment.
Frequently Asked Questions
What does Lines.ToText do in Power Query?
It takes a list of text values and returns a single text string, joining the items with a line break (or a custom separator you specify).
Can I use a different separator instead of a line break?
Yes. Pass the separator as the second argument, for example Lines.ToText(myList, ", ") joins items with a comma and space.
Why does Lines.ToText return an error when my list contains nulls?
The function expects all items to be text. Nulls are not text, so it errors. Remove nulls with List.RemoveNulls or replace them with empty text before calling Lines.ToText.
How do I split text into a list to use with Lines.ToText?
Use Text.Split on the column, specifying the delimiter. For example, Text.Split([Column], "#(lf)") splits on line feeds. Then apply Lines.ToText to the resulting list.
Does Lines.ToText work in both Excel and Power BI?
Yes, it is part of the standard M library available in Power Query for Excel and Power BI Desktop. The editor interface may look slightly different, but the function works the same.
Can I join numbers with Lines.ToText?
No, it only accepts text lists. Convert numbers to text first using List.Transform with Text.From, then pass the result to Lines.ToText.