Binary.ViewFunction

D

T

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

Overview of Binary.ViewFunction

Before diving into the M code behind Binary.ViewFunction, it’s important to understand its purpose and how it works. The Binary.ViewFunction function takes in a binary value as its input and returns the contents of that value as text. This is useful when working with binary files, such as PDFs or images, that cannot be easily interpreted by humans.

The function takes two arguments: value and optional encoding. The value argument is the binary value that you want to view, while the encoding argument specifies the character encoding to use when converting the binary value to text. If the encoding argument is not provided, then the function uses the default encoding of UTF-8.

Here’s an example of how to use the Binary.ViewFunction function:


let

binaryValue = Binary.FromText(“Hello, world!”, TextEncoding.Ascii),

viewValue = Binary.ViewFunction(binaryValue, TextEncoding.Ascii)

in

viewValue


In this example, we first convert the text "Hello, world!" into a binary value using the Binary.FromText function and the ASCII encoding. We then pass this binary value into the Binary.ViewFunction function along with the ASCII encoding to get the text representation of the binary value.

The M Code Behind Binary.ViewFunction

Now that we understand what the Binary.ViewFunction function does, let's take a look at the M code behind it. The M code for the function is relatively simple and straightforward:


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

let

encoding = if Encoding <> null then Encoding else 65001,

text = Text.FromBinary(Binary, encoding)

in

text


The function takes in the binary value as the first argument and the encoding as an optional second argument. The encoding argument is nullable, which means that it can be omitted if desired.

The first line of the function defines the function signature, which specifies that the function returns text. The next line declares the encoding variable and sets it to the value of the encoding argument if it is not null. If the encoding argument is null, then the default encoding of UTF-8 is used (which is represented by the value 65001).

The final line of the function converts the binary value to text using the Text.FromBinary function and the specified encoding. The resulting text value is then returned by the function.

Using Binary.ViewFunction in Practice

Now that we've explored the M code behind Binary.ViewFunction, let's take a look at some practical examples of how to use the function in Power Query.

Example 1: Viewing PDF Contents

Suppose you have a PDF file that you want to view the contents of in Power Query. You can use the Binary.FromFile function to read the contents of the PDF file into Power Query as a binary value, and then use the Binary.ViewFunction function to view the contents of the PDF as text. Here's an example:


let

pdfBinary = Binary.FromFile(“C:My Documentsexample.pdf”),

pdfText = Binary.ViewFunction(pdfBinary, 1252)

in

pdfText


In this example, we read the contents of the file "example.pdf" into Power Query as a binary value using the Binary.FromFile function. We then pass this binary value into the Binary.ViewFunction function along with the Windows-1252 encoding to get the text representation of the PDF contents.

Example 2: Viewing Image Metadata

Suppose you have a JPEG image file that you want to extract metadata from, such as the image's width and height. You can use the Binary.FromFile function to read the contents of the JPEG file into Power Query as a binary value, and then use the Binary.ViewFunction function to view the contents of the image metadata as text. Here's an example:


let

jpegBinary = Binary.FromFile(“C:My Picturesexample.jpg”),

jpegMetadata = Binary.ViewFunction(jpegBinary, 65001),

widthStart = Text.PositionOf(jpegMetadata, “Width”),

widthEnd = Text.PositionOf(jpegMetadata, “Height”),

heightStart = Text.PositionOf(jpegMetadata, “Height”),

heightEnd = Text.PositionOf(jpegMetadata, “x”) – 1,

width = Number.FromText(Text.BetweenDelimiters(Text.Range(jpegMetadata, widthStart, widthEnd), ” “, ” “)),

height = Number.FromText(Text.BetweenDelimiters(Text.Range(jpegMetadata, heightStart, heightEnd), ” “, ” “))

in

[Width = width, Height = height]


In this example, we read the contents of the file "example.jpg" into Power Query as a binary value using the Binary.FromFile function. We then pass this binary value into the Binary.ViewFunction function along with the default UTF-8 encoding to get the text representation of the image metadata.

We then use the Text.PositionOf function to find the positions of the "Width" and "Height" values in the metadata text. We use these positions to extract the numerical values for the width and height of the image using the Text.BetweenDelimiters and Number.FromText functions.

In this article, we've explored the M code behind the Binary.ViewFunction function in Power Query and how it can be used to view the contents of binary values in text format. We've also looked at some practical examples of how to use the function to enhance your data analysis and visualization. By understanding the M code behind this function, you can take your Power Query skills to the next level and unlock new possibilities for data analysis.

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)