Binary.ViewError

D

T

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

Understanding Binary.ViewError

Binary.ViewError is a function in the M language that allows users to view error messages that occur during the execution of M code. The function takes two arguments, the first being the binary data that contains the error message and the second being the optional encoding of the binary data. The function returns a text value that contains the error message.

The primary use case of Binary.ViewError is to debug M code when an error occurs during its execution. When an error occurs, Power Query returns a binary value that contains the error message. The binary data is not human-readable and needs to be converted into a text format to understand the error message.

Binary.ViewError converts the binary data into a human-readable text format, making it easier for users to understand the error message and debug their M code.

The M Code Behind Binary.ViewError

The M code behind Binary.ViewError is relatively simple and involves converting the binary data into a text format. Here is the M code behind Binary.ViewError:


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

let

EncodingNumber = if Encoding = null then 1252 else Encoding,

EncodedText = Text.FromBinary(BinaryData, EncodingNumber),

DecodedText = try EncodedText otherwise “”,

ErrorText = if DecodedText = “” then “Binary decoding error” else DecodedText

in

ErrorText


The function takes two arguments, BinaryData, which is the binary data that contains the error message, and Encoding, which is the optional encoding of the binary data.

The first step in the function is to check if the Encoding argument is null. If it is null, the function sets the EncodingNumber variable to 1252, which is the default encoding used by Power Query. If the Encoding argument is not null, the function sets EncodingNumber to the value of Encoding.

The next step is to convert the binary data into a text format using the Text.FromBinary function. The function takes two arguments, the binary data, and the encoding number. The EncodingNumber variable is used as the encoding number argument in this case.

The EncodedText variable now contains the error message in text format. However, there might be scenarios where the binary data cannot be converted into a text format, leading to an error. To handle such scenarios, the function uses the try function to catch any errors that might occur during the conversion.

If the conversion is successful, the DecodedText variable contains the error message in text format. If the conversion fails, the DecodedText variable is set to an empty string.

The last step in the function is to check if the DecodedText variable is empty. If it is empty, the function sets the ErrorText variable to "Binary decoding error". If the DecodedText variable is not empty, the function sets ErrorText to the value of DecodedText.

Binary.ViewError is a useful function in the M language that allows users to view error messages that occur during the execution of M code. The function converts the binary data into a human-readable text format, making it easier for users to understand the error message and debug their M code.

The M code behind Binary.ViewError is relatively simple and involves converting the binary data into a text format. Power Query uses a default encoding of 1252 if the encoding argument is null. If the conversion to text format fails, the function sets the error message to "Binary decoding error".

By understanding the M code behind Binary.ViewError, users can gain a better understanding of how M functions work and how they can use them to create more efficient and effective Power Query solutions.

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

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)