Binary data

D

T

The M Code Behind the Power Query M function Binary data

What is Binary Data?

Binary data refers to data that is stored in a binary format, rather than a text format. In other words, binary data is represented as a series of 1’s and 0’s, rather than as characters. Some common examples of binary data include images, videos, and audio files. Binary data is often used in computing because it can be processed much more quickly than text data.

The M Code for Binary Data

The M function for binary data is called Binary.FromText. This function takes a text value as input, and returns a binary value. The syntax for Binary.FromText is as follows:


Binary.FromText(text as text, optional encoding as nullable number) as binary


The text parameter is the text value that you want to convert to binary data. The encoding parameter is an optional parameter that specifies the character encoding that should be used when converting the text to binary data. If you don’t specify an encoding, Power Query will use the default encoding for your system.

Here’s an example of how you might use Binary.FromText to convert a text value to binary data:


let

textValue = "Hello, world!",

binaryValue = Binary.FromText(textValue)

in

binaryValue


In this example, we’re converting the text value “Hello, world!” to binary data using the Binary.FromText function. The result is a binary value that represents the text value.

Use Cases for Binary Data

Binary data can be used in a variety of ways in Power Query. Here are a few examples:

Converting Images to Binary Data

If you’re working with images in Power Query, you might need to convert them to binary data so that you can store them in a database or transfer them over the internet. Here’s an example of how you might use Binary.FromText to convert an image to binary data:


let

imagePath = "C:Imagesmyimage.jpg",

binaryData = Binary.FromText(File.Contents(imagePath), BinaryEncoding.Base64)

in

binaryData


In this example, we’re using the File.Contents function to read the contents of an image file, and then using Binary.FromText to convert the contents to binary data. We’re also specifying the BinaryEncoding.Base64 encoding, which is a common encoding used for transmitting binary data over the internet.

Converting Audio Files to Binary Data

Similar to images, you might also need to convert audio files to binary data for storage or transmission. Here’s an example of how you might use Binary.FromText to convert an audio file to binary data:


let

audioPath = "C:Audiomysong.mp3",

binaryData = Binary.FromText(File.Contents(audioPath), BinaryEncoding.Base64)

in

binaryData


In this example, we’re using the File.Contents function to read the contents of an audio file, and then using Binary.FromText to convert the contents to binary data.

Parsing Binary Data

Sometimes you might need to parse binary data in Power Query, in order to extract specific information. For example, if you’re working with a binary file format, you might need to read specific bytes from the file in order to extract certain data. Here’s an example of how you might use Binary.FromText to parse binary data:


let

binaryData = Binary.FromText("0101010101010101"),

firstByte = Binary.Buffer(Binary.Range(binaryData, 0, 1)),

secondByte = Binary.Buffer(Binary.Range(binaryData, 1, 1))

in

{firstByte, secondByte}


In this example, we’re creating a binary value from a string of 1’s and 0’s, and then using the Binary.Range function to extract the first and second bytes from the binary value. We’re also using the Binary.Buffer function to cache the binary data, so that it can be used multiple times without being recalculated.

The M code behind the Power Query M function binary data allows users to work with binary data in a variety of ways. Whether you’re converting images and audio files to binary data, or parsing binary data to extract specific information, the Binary.FromText function is a powerful tool that can save you time and effort in your data analysis tasks. By understanding the M code behind binary data, you’ll be able to take full advantage of this powerful feature in Power Query.

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)