BinaryFormat.Binary

D

T

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

What is BinaryFormat.Binary?

BinaryFormat.Binary is a Power Query M function that enables users to convert any value to a binary representation. The binary representation is a sequence of 0s and 1s that represent the value’s bits. For example, the binary representation of the decimal number 5 is 101. BinaryFormat.Binary takes two arguments: the value to be converted and the number of bits to use in the binary representation.

M Code Behind BinaryFormat.Binary

The M code behind BinaryFormat.Binary is relatively simple. The function first checks whether the value to be converted is null. If the value is null, the function returns null. If the value is not null, the function converts the value to a binary representation.

Here is the M code for BinaryFormat.Binary:


(BinaryValue as any, Optional Bits as number) as binary =>

if BinaryValue = null then

null

else if Bits = null or Bits = 0 then

Binary.FromNumber(BinaryValue, 0)

else

Binary.FromNumber(BinaryValue, Bits)


The function takes two arguments: BinaryValue and Bits. BinaryValue is the value to be converted, and Bits is the number of bits to use in the binary representation. If Bits is not specified, the function defaults to using the minimum number of bits required to represent the value.

The first line of the function checks whether BinaryValue is null. If BinaryValue is null, the function returns null. This is to ensure that the function does not try to convert a null value to a binary representation.

The second line of the function checks whether Bits is null or zero. If Bits is null or zero, the function returns the binary representation of BinaryValue using the minimum number of bits required.

The third line of the function converts BinaryValue to a binary representation using the specified number of bits.

Using BinaryFormat.Binary

BinaryFormat.Binary can be useful in a variety of data transformation scenarios. For example, it can be used to convert IP addresses to binary representations or to convert dates to binary representations.

Here is an example of using BinaryFormat.Binary to convert a decimal number to a binary representation:


let

Source = 5,

Binary = BinaryFormat.Binary(Source, 8)

in

Binary


This code converts the decimal number 5 to a binary representation with 8 bits. The resulting binary representation is 00000101.

BinaryFormat.Binary is a useful Power Query M function that enables users to convert any value to a binary representation. The function's M code is relatively simple, and it can be used in a variety of data transformation scenarios. By understanding the M code behind BinaryFormat.Binary, users can gain a better understanding of how Power Query works and how they can create custom code to meet their specific data transformation needs.

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)