Binary.ToList

Binary.ToList

D

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

One of the most useful M functions in Power Query is Binary.ToList. This function converts a binary value into a list of integers representing the binary digits. In this article, we will explore the M code behind this function and how it works.

Understanding Binary Values

Before we dive into the M code behind Binary.ToList, we need to understand what binary values are and how they work. Binary values are a sequence of 0s and 1s used to represent numbers, characters, and other data types in computers.

For example, the binary value 1001 represents the decimal number 9. The first 1 represents 2^3 (or 8), and the second 1 represents 2^0 (or 1). Therefore, we add 8 and 1 to get the decimal value 9.

Binary values are often used in computer systems because they are easier to process and store than decimal values. However, they are not as easy to read and understand for humans.

The Binary.ToList Function

The Binary.ToList function in Power Query takes a binary value as input and returns a list of integers representing the binary digits. For example, the binary value 1001 would result in the list [1, 0, 0, 1].

Here is the M code behind Binary.ToList:


(BinaryValue as binary) => List.Reverse(List.SkipWhile(List.Reverse(List.Transform(Binary.ToText(BinaryValue), each Number.FromText(_))), (x) => x = 0))


Let's break down this M code step by step and see how it works.

Step 1: Converting Binary to Text

The first step in the Binary.ToList function is to convert the binary value to text using the Binary.ToText function. This function takes a binary value as input and returns a text value representing the binary digits.

For example, the binary value 1001 would result in the text value "1001".

Step 2: Converting Text to Numbers

The next step in the Binary.ToList function is to convert each character in the text value to a number using the Number.FromText function. This function takes a text value as input and returns a number value.

For example, the text value "1001" would result in the list [1, 0, 0, 1].

Step 3: Removing Trailing Zeros

The final step in the Binary.ToList function is to remove any trailing zeros from the list. This is done using the List.Reverse and List.SkipWhile functions.

The List.Reverse function reverses the order of the list, so the trailing zeros are at the beginning of the list. The List.SkipWhile function then removes all the zeros until it reaches a non-zero value.

For example, the list [1, 0, 0, 1, 0, 0, 0] would result in the list [1, 0, 0, 1].

The Binary.ToList function in Power Query is a powerful tool for converting binary values into a list of integers. Its M code is complex but efficient, and it shows the power of the M language for data transformations.

Understanding how Binary.ToList works can help you write more efficient and powerful queries in Power Query. Whether you are working with binary values or not, the M language is an essential tool for any data analyst or Power BI user.

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)

Copyright, G Com Solutions Ltd, 2024.
Tower Bridge Business Centre, 46-48 East Smithfield, London E1W 1AW
0800 998 9248   |   9:00 a.m. till 5:30 p.m.

Connect With Me: