BinaryFormat.Group

D

T

The M Code Behind the Power Query M function BinaryFormat.Group

What is BinaryFormat.Group?

BinaryFormat.Group is a function in Power Query M that allows you to group data based on a specific byte size. It works by formatting the data into a binary representation, and then grouping the data by the specified byte size. The function takes two arguments: the data you want to group, and the byte size you want to group it by.

Here is an example of how BinaryFormat.Group works:


let

data = {1, 2, 3, 4, 5, 6, 7, 8},

groupSize = 2,

result = BinaryFormat.Group(data, groupSize)

in

result


In this example, we are grouping the data {1, 2, 3, 4, 5, 6, 7, 8} into groups of 2. The resulting binary representation would be:


00000001 00000010

00000011 00000100

00000101 00000110

00000111 00001000


The resulting data would be:


{258, 772, 1286, 1800}


How does BinaryFormat.Group work?

BinaryFormat.Group works by first converting the input data into a binary representation. This binary representation is then grouped based on the specified byte size. Finally, the resulting groups are converted back into decimal format.

Here is the M code behind BinaryFormat.Group:


let

BinaryFormat.Group = (data as list, groupSize as number) =>

let

binaryData = BinaryFormat.IntegerToBinary(data, 8),

groupedData = List.Group(binaryData, each List.PositionOf(binaryData, _, groupSize)),

binaryGroups = List.Transform(groupedData, (group) => BinaryFormat.BinaryToInteger(group)),

result = binaryGroups

in

result

in

BinaryFormat.Group


The M code first converts the input data into a binary representation using the BinaryFormat.IntegerToBinary function. This function takes two arguments: the data you want to convert, and the number of bits you want to represent each value in.

In the example above, we represented each value in 8 bits, which is the standard size for a byte. The resulting binary data would be:


00000001

00000010

00000011

00000100

00000101

00000110

00000111

00001000


The binary data is then grouped based on the specified byte size using the List.Group function. This function takes two arguments: the list you want to group, and a function that returns the index of the group each value belongs to.

In this case, we used the List.PositionOf function to group the binary data based on the specified byte size. This function takes two arguments: the list you want to search, and the value you want to find the position of.

The resulting groups would be:


{{00000001, 00000010}, {00000011, 00000100}, {00000101, 00000110}, {00000111, 00001000}}


Finally, the binary groups are converted back into decimal format using the BinaryFormat.BinaryToInteger function. This function takes a binary representation of a number and returns its decimal equivalent.

BinaryFormat.Group is a powerful M function in Power Query that allows you to group data based on a specific byte size. By understanding the M code behind this function, you can use it to manipulate data in powerful ways. We hope this article has helped you understand how BinaryFormat.Group works and how you can use it in your own data processing projects.

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)