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

Understanding Binary Data

Before diving into the M code behind Binary.ApproximateLength, it’s important to understand what binary data is and how it’s used. Binary data is a sequence of 1s and 0s that is used to represent information in a computer system. This information could be anything from text to images to executable code.

In Power Query, binary data is often used when working with files, such as PDFs, images, and videos. These files are typically stored in binary format, which means that the data is represented as a series of bytes (groups of 8 bits). Each byte can have a value between 0 and 255, which corresponds to a unique combination of 1s and 0s.

The Binary.ApproximateLength Function

The Binary.ApproximateLength function in Power Query is used to calculate the approximate length of a binary value. This function takes a binary value as its input and returns the approximate length of that value in bytes.

Here’s the M code for the Binary.ApproximateLength function:

(Binary as binary) as number =>


Length = List.Count(Binary),

ApproximateLength = Number.RoundUp(Length/1024)1024



Let’s break down this code and see how it works.

Breaking Down the M Code

The Binary.ApproximateLength function takes a single parameter, Binary, which is the binary value that you want to calculate the approximate length of. This parameter is defined as a binary data type.

The first step in the function is to calculate the length of the binary value. This is done using the List.Count function, which returns the number of elements in a list. In this case, the binary value is treated as a list of bytes, so the List.Count function returns the number of bytes in the binary value.

Length = List.Count(Binary)

The next step is to calculate the approximate length of the binary value in bytes. This is done by dividing the length of the binary value by 1024 and then rounding up to the nearest whole number. The result is then multiplied by 1024 to get the approximate length in bytes.

ApproximateLength = Number.RoundUp(Length/1024)1024

For example, if the length of the binary value is 2500, the approximate length would be calculated as follows:

ApproximateLength = Number.RoundUp(2500/1024)1024

= Number.RoundUp(2.44)1024

= 31024

= 3072

So the approximate length of the binary value in this case would be 3072 bytes.

The Binary.ApproximateLength function in Power Query is a useful tool for calculating the approximate length of a binary value. The M code behind this function is relatively simple, but it provides an efficient and accurate way to estimate the size of binary data. Understanding how this function works can help you to better manipulate and transform binary data in Power Query, and improve your overall data analysis skills.

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)