Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Text.ToBinary: Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Text.ToBinary converts a text value into a binary value using a chosen character encoding, and it is the function you reach for when Power Query needs to hand raw bytes to a web service, a hash function, or a file-writing step. The shortest working version is Text.ToBinary("Hello"), which returns the UTF-8 bytes for that string. If the receiving system expects a different character set, add the encoding argument, for example Text.ToBinary("Hello", 1252) for Windows-1252 or Text.ToBinary("Hello", TextEncoding.Unicode) for UTF-16. No add-in is required; the function is part of the standard Power Query M library in Excel, Power BI Desktop, and Dataflows.

What You'll Need

  • A Power Query editor session, opened from Excel (Data > Get Data) or Power BI Desktop (Home > Transform data).
  • A text column or literal string to convert. The function accepts a single text value, not a whole column at once.
  • Knowledge of the encoding the target system expects. If you do not know it, UTF-8 is the safe default for most modern APIs.
  • Optional: a custom function or the Table.TransformColumns pattern if you need to convert an entire column rather than one value.

Step-by-Step

  1. Open the Power Query Editor. In Excel, go to Data > Get Data > Launch Power Query Editor. In Power BI Desktop, go to Home > Transform data. Menu labels can differ slightly by version, device, or region.
  2. Load or create the text you want to convert. You can use an existing query, or start a blank query and enter a literal. To work with a literal, go to Home > New Source > Blank Query, then open the formula bar (View > Formula Bar if it is hidden).
  3. Write the basic call. In the formula bar, type = Text.ToBinary("Your text here") and press Enter. The result appears as a binary value, usually shown as Binary in the data preview. This uses UTF-8 by default.
  4. Specify an encoding when needed. Add a second argument: = Text.ToBinary("Your text", 1252) for a numeric code page, or = Text.ToBinary("Your text", TextEncoding.Unicode) for a named encoding. Common named encodings include TextEncoding.Utf8, TextEncoding.Unicode, TextEncoding.Utf16, and TextEncoding.Ascii. The exact list available depends on your Power Query version.
  5. Apply the function to a column. Text.ToBinary works on one text value, so to convert a whole column use Table.TransformColumns . For example: . Replace Source with your step name and MyTextColumn with the real column name.
  6. Check the output before loading. Binary values do not display as readable text in the preview. To confirm the bytes are correct, temporarily convert back with Text.FromBinary using the same encoding, or use Binary.ToText if your version provides it. If the round-trip returns the original string, the encoding is correct.
  7. Use the binary where it is needed. Typical destinations are Web.Contents request bodies, Binary.Combine for assembling payloads, and hash functions such as Binary.Hash (availability varies by version). Pass the binary value directly; do not wrap it in quotes.
  8. Rename the step and close. Give the step a clear name (for example ConvertedToBinary), then go to Home > Close & Load (Excel) or Home > Close & Apply (Power BI).

Common Mistakes

  • Passing a column instead of a value. Text.ToBinary([MyColumn]) fails because the function expects text, not a column reference. Use Table.TransformColumns with each Text.ToBinary(_) instead.
  • Assuming UTF-8 when the target expects something else. APIs and legacy systems sometimes require UTF-16, Windows-1252, or ASCII. A mismatch produces garbled characters or rejected requests.
  • Forgetting that binary is not text. You cannot concatenate a binary with & and expect readable output. Convert back with Text.FromBinary before doing string operations.
  • Using the wrong code page number. Numeric code pages are easy to mistype. If you are unsure, prefer the named TextEncoding values.
  • Expecting the preview to show readable characters. Binary values display as Binary; that is normal, not an error.

Tips

  • Default to UTF-8 unless a system's documentation explicitly says otherwise; it handles the widest range of characters.
  • Test with a short string that includes an accented character or a symbol, then round-trip it with Text.FromBinary to confirm the encoding.
  • If you convert many columns, create a reusable custom function rather than repeating the each Text.ToBinary(_, ...) pattern.
  • Keep the encoding argument in a variable or parameter so you can change it in one place if the target system changes.
  • When sending data to a web API, check whether the API expects the body as binary or as text; some accept text and handle encoding themselves.

Troubleshooting

  • The query errors with "We cannot convert a value of type Text to type Binary" or similar. Check that you are passing a text value, not a number, date, or column reference. Wrap literals in quotes.
  • The result is binary but the receiving system rejects it. Verify the encoding. Try UTF-8 first, then UTF-16, then the code page named in the system's documentation. Also confirm the system does not expect a different format such as Base64; if so, convert the binary with Binary.ToText if available in your version.
  • Accented characters turn into question marks or symbols. This is almost always an encoding mismatch. Re-run the conversion with the correct encoding and round-trip to check.
  • The column conversion step fails on nulls. Text.ToBinary does not accept null. Add a null check: each if _ = null then null else Text.ToBinary(_, TextEncoding.Utf8).
  • The function name is not recognized. Update Power Query or the host application; very old versions may lack some TextEncoding constants. Use the numeric code page as a fallback.

Frequently Asked Questions

What does Text.ToBinary actually return?

A binary value containing the bytes of the input text encoded with the specified character set. It is not a readable string.

Can I convert a whole column at once?

Not with a single call. Use Table.TransformColumns with each Text.ToBinary(_, encoding) to apply it row by row.

What is the default encoding if I omit the second argument?

UTF-8. That is the safest choice for most modern systems, but confirm with the target system's documentation.

How do I turn the binary back into text?

Use Text.FromBinary with the same encoding, for example Text.FromBinary(binaryValue, TextEncoding.Utf8).

Why does the preview show "Binary" instead of my text?

Power Query displays binary values with a generic label. The underlying bytes are correct; use a round-trip conversion to inspect them.

Does Text.ToBinary work in Power BI and Excel the same way?

Yes, the M function is the same across hosts, though the surrounding menu labels differ. The available named encodings can vary slightly by version.