Power Query Lines.FromBinary: How to Split Text
In this article
Lines.FromBinary splits a binary value into a list of text lines. You give it the binary (for example the result of File.Contents or Web.Contents) and it returns a list where each item is one line, with line breaks removed. This is the standard way to turn a downloaded CSV, log file or text file into rows you can clean and load. Nothing extra needs installing: the function is built into Power Query in Excel, Power BI Desktop and Dataflows, though the exact menu labels around it can differ slightly by version, device or region.
What You'll Need
- Power Query available in Excel (Data > Get Data), Power BI Desktop (Home > Transform data) or a Power Query dataflow.
- A binary source: File.Contents, Folder.Files content column, Web.Contents, or a hard-coded binary literal for testing.
- A destination query or table where the resulting list can be expanded.
Step-by-Step
- Get the file as binary. In Excel use Data > Get Data > From File > From Text/CSV and choose Transform Data rather than Load, or use Data > Get Data > From Other Sources > Blank Query and type
= File.Contents("C:\Data\log.txt"). In Power BI Desktop use Home > Get data > Text/CSV and click Transform Data. The preview shows the file as a binary value, not as text. - Confirm the value is binary. Select the step in Applied Steps and look at the formula bar. It should read something like
File.Contents("C:\Data\log.txt"). If the step already shows a table, you have skipped the binary stage and Lines.FromBinary has nothing to split. - Add a custom step. With the binary step selected, open Home > New Source > Blank Query (or Add Column > Custom Column if you want to keep the original). In the formula bar enter
= Lines.FromBinary(Source), replacing Source with the name of your binary step. Press Enter. The result is a list, shown with the List tool in the column header. - Choose the right options. Lines.FromBinary accepts optional arguments:
Lines.FromBinary(binary, quoteStyle, includeLineSeparators, encoding). Set includeLineSeparators to false (the default) to drop the line breaks; set it to true if you need to keep them. Set encoding to TextEncoding.Utf8, TextEncoding.Unicode or another value when the file is not UTF-8, otherwise accented characters can appear garbled. - Convert the list to a table. Click List Tools > Transform > To Table. Accept the default column name or rename it to Line. Each row is now one line of the original file.
- Clean the lines. Use Transform > Trim and Transform > Clean to remove stray spaces and control characters. If the file is delimited, use Home > Split Column > By Delimiter and choose the correct separator (comma, tab, semicolon). Rename the resulting columns.
- Set data types. Click the data type icon in each column header and choose Text, Whole Number, Date or Decimal as appropriate. Do this after splitting, because changing types before splitting can turn numbers into errors.
- Load or close and apply. In Excel choose Home > Close & Load to send the table to a worksheet or the Data Model. In Power BI Desktop choose Home > Close & Apply. The query then refreshes with the file.
Common Mistakes
- Passing text instead of binary. Lines.FromBinary expects a binary value; if you feed it a text string you get an error. Use Text.FromBinary or Lines.FromText for text sources.
- Ignoring encoding. Files saved as ANSI or UTF-16 produce odd characters when read as UTF-8. Set the encoding argument explicitly.
- Forgetting to expand the list. A list in the preview is not a table until you use To Table; loading a list directly gives a single column of lists.
- Assuming the function parses CSV. It only splits on line breaks. Commas, quotes and headers still need Split Column or a dedicated CSV parser.
- Using a hard-coded local path in a shared report. Other users will not have that file. Use a parameter or a network path that everyone can reach.
Tips
- Test the function in a blank query first:
= Lines.FromBinary(Text.ToBinary("a" & "#(cr)#(lf)" & "b"))returns a two-item list. That confirms the function works before you point it at a real file. - Keep the binary step and the Lines.FromBinary step separate so you can inspect each one. Combining them in one formula makes errors harder to trace.
- If the file is large, filter rows immediately after converting to a table rather than at the end; Power Query folds some filters back to the source when it can.
- Use includeLineSeparators only when you need to preserve exact line endings for a downstream tool; otherwise leave it off.
- Give the query and its steps descriptive names. A step called Lines is easier to reference in later formulas than Custom1.
Troubleshooting
- Expression.Error: We cannot convert a value of type Text to type Binary. The input is text, not binary. Check the previous step; use File.Contents or Web.Contents, or wrap text with Text.ToBinary.
- All lines appear as one row. The file may use a line separator the function does not recognise, or includeLineSeparators was set to true. Check the file in a text editor, then try a different encoding.
- Garbled characters. Set the encoding argument to match the file, for example TextEncoding.Unicode for UTF-16 files.
- The query works locally but fails on refresh. The path is probably local or the file has moved. Use a parameter for the path and confirm the file exists at refresh time.
- Errors after changing data types. Some lines are not valid for the chosen type. Right-click the error and choose Replace Errors, or keep the column as Text and convert later.
Frequently Asked Questions
What is the difference between Lines.FromBinary and Lines.FromText?
Lines.FromBinary takes a binary value, while Lines.FromText takes a text value. Use FromBinary when the source is a file or web response; use FromText when you already have a text string.
Does Lines.FromBinary parse CSV columns?
No. It only splits the content into lines. You still need Split Column by Delimiter or a CSV parser to separate fields.
Can I keep the line break characters?
Yes. Set the includeLineSeparators argument to true. The default is false, which removes them.
Why do I get one long line instead of many rows?
The file may use an unusual line ending or the encoding may be wrong. Check the file in a text editor and set the encoding argument to match.
Does this work in Power BI and Excel the same way?
Yes, the function is the same in both. The menus around it differ slightly, and labels can vary by version or region.
Can I use Lines.FromBinary on a web response?
Yes. Wrap the response with Web.Contents and pass the binary result to Lines.FromBinary, provided the response is text-based.