Binary.Compress

D

T

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

In this article, we’ll dive into the M code behind Binary.Compress and explore how it works.

What is Binary.Compress?

Binary.Compress is a Power Query M function that compresses binary data using the GZip algorithm. It takes a binary value as its input and returns the compressed binary data as output.

Here’s an example of how to use Binary.Compress in Power Query:


let

Source = Binary.FromText("Hello, World!"),

Compressed = Binary.Compress(Source),

Decompressed = Binary.Decompress(Compressed)

in

Decompressed


In this example, we first convert the text “Hello, World!” to binary using the Binary.FromText function. We then compress the binary data using Binary.Compress and store the result in the variable Compressed. Finally, we decompress the compressed data using Binary.Decompress and store the result in the variable Decompressed.

The M Code Behind Binary.Compress

Let’s take a closer look at the M code behind Binary.Compress. Here’s the full code:


(Binary as binary, optional compressionLevel as nullable number) =>

let

compressedBinary = Binary.Buffer(List.Buffer(GZipCompress.Binary(binary, compressionLevel)))

in

compressedBinary


The code defines a function that takes two parameters: a binary value and an optional compression level. The binary value is the input that we want to compress, and the compression level determines the amount of compression to apply (higher levels result in more compression but take longer to process).

The function first uses the GZipCompress.Binary function to compress the input binary data. This function is part of the .NET Framework and is used to compress data using the GZip algorithm.

The compressed binary data is then stored in a buffer using the Binary.Buffer function. This function improves performance by caching the data in memory and making it available for reuse.

Finally, the function returns the compressed binary data.

Limitations of Binary.Compress

While Binary.Compress is a powerful feature of Power Query, it does have some limitations.

First, the function is only designed to compress binary data. If you have text or other data types, you’ll need to convert them to binary before using Binary.Compress.

Second, the function uses the GZip algorithm, which is a lossless compression algorithm. This means that the compressed data will be an exact copy of the original data, but in a more compact form. However, this also means that the amount of compression achieved will depend on the data being compressed. Some types of data may not compress well using GZip.

Finally, the function may not be suitable for large datasets or real-time data processing. The compression algorithm can be CPU-intensive, especially at higher compression levels, which can slow down processing times.

Binary.Compress is a powerful feature of Power Query that allows users to compress binary data using the GZip algorithm. By understanding the M code behind the function, users can better understand how it works and how to use it effectively.

However, it’s important to keep in mind the limitations of Binary.Compress, including its dependence on binary data, the use of the GZip algorithm, and potential performance issues with large datasets or real-time processing. With these limitations in mind, users can make informed decisions about when and how to use Binary.Compress in their Power Query workflows.

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)