BinaryFormat.Byte

D

T

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

What is BinaryFormat.Byte?

BinaryFormat.Byte is a Power Query M function that converts a number into a binary representation of a specified length. The function takes two arguments: the number to be converted and the length of the binary representation.

Understanding the M Code Behind BinaryFormat.Byte

The M code behind BinaryFormat.Byte is relatively straightforward. It involves the use of the Number.ToText and Number.Mod functions to perform calculations and obtain the binary representation of the number.


(BinLength, Number) =>

let

Binary = List.Transform(

List.Generate(

() => [Value = Number, Bit = 0],

each [Bit] < BinLength,

each [Value = Number 2, Bit = Number.Mod(Number, 2)]),

each Text.From([Bit])),

BinaryString = Text.Combine(List.Reverse(Binary), “”)

in

Number.FromText(BinaryString, 2)


The function takes two parameters, BinLength and Number, and begins by first defining a list of 0's and 1's to represent the binary digits. This is done using the List.Generate function, which generates a list of values based on the initial value and a condition that must be met for each value generated.

The List.Generate function takes three parameters: the initial value, the condition, and the function used to generate the next value. In this case, the initial value is set to [Value = Number, Bit = 0], where Value is the number to be converted and Bit is initially set to 0.

The condition for generating the next value is set to [Bit] < BinLength, which ensures that the function generates values until the specified length of the binary representation is reached.

The function used to generate the next value is defined by [Value = Number 2, Bit = Number.Mod(Number, 2)]. This function takes the current value of Value and Bit and performs calculations to obtain the next value of Value and Bit. The value of Value is divided by 2 using the operator to shift the binary digits to the right, while the value of Bit is obtained by taking the remainder of the division using the Number.Mod function.

The resulting list of 0's and 1's is then transformed into a binary string using the List.Transform and Text.From functions. The binary string is then concatenated using the Text.Combine function and converted back into a decimal number using the Number.FromText function with a base of 2.

Use Cases for BinaryFormat.Byte

BinaryFormat.Byte can be used in a variety of scenarios where a conversion to binary is required. For example, it can be used to:

- Convert IP addresses to binary for easier comparison and manipulation

- Convert decimal numbers to binary for use in bitwise operations

- Convert ASCII characters to binary for use in encoding and decoding

BinaryFormat.Byte is a powerful M function in Power Query that allows for easy conversion to binary. Its simple yet effective M code makes it a valuable tool in any data analyst's toolkit.

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)