Power Query Lines.ToBinary: Step-by-Step Guide
In this article
Power Query's Lines.ToBinary function converts a list of text lines into a binary value, typically used to create text files with specific line endings. It is not a native Power Query function; you must define it in a custom function or use it within a query. The function takes a list of text and an optional line separator (default is CRLF) and returns a binary representing the concatenated lines with separators. This is useful when generating CSV or log files programmatically. To use it, you need Power Query Editor (in Excel, Power BI, or similar) and basic familiarity with M code.
What You'll Need
- Access to Power Query Editor (Excel 2016+, Power BI Desktop, or Excel for Microsoft 365).
- A query or blank query where you can write M code.
- Basic understanding of Power Query's M language, especially lists and binary values.
Step-by-Step
- Open Power Query Editor. In Excel, go to Data > Get Data > Launch Power Query Editor. In Power BI Desktop, click Transform Data. The exact menu labels may vary slightly by version and region.
- Create a blank query. In the Power Query Editor, go to Home > New Source > Blank Query (or right-click in the Queries pane and select New Query > Blank Query). This opens the Power Query formula bar and Advanced Editor.
- Define the Lines.ToBinary function. Open the Advanced Editor (Home > Advanced Editor). Replace the existing code with the following function definition:
Click Done. This creates a custom function named(lines as list, optional lineSeparator as nullable text) as binary => let sep = if lineSeparator = null then "#(cr)#(lf)" else lineSeparator, combined = Text.Combine(lines, sep), binary = Text.ToBinary(combined) in binaryLines.ToBinaryin your query. Note: The function name is case-sensitive; use exactly as shown. - Test the function with a simple list. Create a new blank query and enter the following in the formula bar:
Press Enter. The result should be a binary value. To view it, click the "Binary" link in the results pane; it will open a text file with "Hello" and "World" on separate lines (using CRLF).Lines.ToBinary({"Hello", "World"}) - Use a custom line separator. To use a different separator, such as a line feed only (LF), call the function with the second argument:
This is useful when generating files for Unix-based systems.Lines.ToBinary({"Line1", "Line2"}, "#(lf)") - Integrate into a larger query. You can use Lines.ToBinary within other queries. For example, to convert a column of text values into a single binary, first convert the column to a list using Table.Column , then pass it to the function: let lines = Table.Column(Source, "Text"), binary = Lines.ToBinary(lines) in binary This returns a binary containing "A", "B", and "C" each on a new line.
- Output the binary to a file (optional). In Power Query, you cannot directly write files to disk; you need to load the binary into Excel or Power BI and then export. For example, load the binary into a table and use Excel's export features. Alternatively, use Power Automate or a script to save the binary.
Common Mistakes
- Forgetting to define the function first.
Lines.ToBinaryis not built-in; you must create it as a custom function before calling it. - Using the wrong line separator. The default is CRLF (
#(cr)#(lf)), but some systems expect LF only. Always specify the separator if needed. - Passing a non-list argument. The first parameter must be a list of text. If you pass a table or a single text value, you'll get an error. Use
Table.ColumnorText.Splitto create a list. - Case sensitivity. Function names in M are case-sensitive. Ensure you call
Lines.ToBinarywith correct capitalization. - Null values in the list. If the list contains nulls,
Text.Combinewill treat them as empty strings. To avoid unexpected results, filter out nulls beforehand.
Tips
- Use
Text.ToBinarywith an encoding parameter if you need a specific character encoding (e.g., UTF-8). The default is UTF-8 without BOM. - To create a binary from a table directly, consider using
Csv.FromValueor similar functions if the output is CSV. - Test with small lists first to verify line endings and encoding.
- Remember that binary values in Power Query are not human-readable; use the "Binary" link to preview.
Troubleshooting
- Error: "The name 'Lines.ToBinary' wasn't recognized." Ensure you have defined the function in the same query or in a separate query that is referenced. If defined in another query, use
QueryName.Lines.ToBinaryor merge queries. - Error: "We cannot convert a value of type Text to type List." The first argument must be a list. Check that you are passing a list, not a single text value. Use curly braces
{}to create a list. - Unexpected line breaks. Verify the line separator argument. If you see extra characters, ensure the separator is correct (e.g.,
#(cr)#(lf)for Windows). - Binary preview shows gibberish. This may indicate an encoding mismatch. Try specifying a different encoding in
Text.ToBinary, such asTextEncoding.Utf8. - Function not available in Power BI service. Custom functions work in the Power BI service if the query is part of a dataset, but some limitations apply. Test in Desktop first.
Frequently Asked Questions
Is Lines.ToBinary a built-in Power Query function?
No, it is not built-in. You must define it as a custom function using M code.
Can I use Lines.ToBinary to create a CSV file?
Yes, you can combine it with Csv.FromValue or manually format lines with commas, but for CSV specifically, consider using Csv.FromValue which handles escaping.
What is the default line separator?
The default is CRLF (#(cr)#(lf)), which is standard on Windows.
How do I specify UTF-8 encoding?
Use Text.ToBinary(combined, TextEncoding.Utf8) inside the function, or modify the function to accept an encoding parameter.
Can I write the binary to a file directly from Power Query?
No, Power Query does not write files to disk. You need to load the binary into Excel or Power BI and export from there, or use Power Automate.
Why does my binary show extra characters?
Check the line separator and encoding. Extra characters often come from incorrect separator or BOM settings.