The M Code Behind the Power Query M function Binary.ToText

Understanding the Binary.ToText Function

The Binary.ToText function is used to convert binary data into text format. The function takes an input parameter, which is the binary data to be converted, and an optional parameter, which specifies the encoding to be used for the conversion. The default encoding used is UTF8.

Here is an example of using the Binary.ToText function:


binaryData = Binary.FromText(“Hello, world!”),

textData = Binary.ToText(binaryData)



In the above example, we first convert the text "Hello, world!" into binary data using the Binary.FromText function. We then pass this binary data to the Binary.ToText function, which converts it back into text format. The resulting output will be "Hello, world!".

The M Code Behind the Binary.ToText Function

The M code behind the Binary.ToText function is relatively simple. Here is the code:

(Binary as binary, optional Encoding as nullable number) as text =>


encoding = if Encoding = null then 65001 else Encoding,

text = Text.FromBinary(binary, encoding)



The above code defines a function with two parameters - binary and Encoding. The binary parameter is the binary data to be converted, while the Encoding parameter is an optional parameter that specifies the encoding to be used for the conversion.

The first line of the function initializes the Encoding parameter to the default value of 65001 (which corresponds to UTF8 encoding) if the optional Encoding parameter is not provided. If the Encoding parameter is provided, its value is used.

The second line of the function calls the Text.FromBinary function, passing in the binary data and the encoding to be used. This function converts the binary data into text format, using the specified encoding.

The resulting text data is returned as the output of the function.

In conclusion, the Binary.ToText function is a useful tool for converting binary data into text format in Power Query. The function uses the Text.FromBinary function internally to perform the conversion, and provides an optional parameter for specifying the encoding to be used. Understanding the M code behind the Binary.ToText function can help you better utilize this function in your data transformations.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us


    Your Name (required)


    Email (required)


    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)