# Lines.ToBinary

## #

In this article, we will explore the M code behind the Lines.ToBinary function and learn how to use it effectively in our Power Query workflows.

## Understanding the Lines.ToBinary Function

The Lines.ToBinary function is a built-in function in Power Query that converts a text string into a binary value. The function takes one argument, which is a text string, and returns a binary value.

The syntax for the Lines.ToBinary function is as follows:

Lines.ToBinary(text as text) as binary

``` In this syntax, the text argument is the text string that we want to convert to binary. The function returns a binary value. Let's look at an example to understand how the Lines.ToBinary function works. Suppose we have a text string "Hello, world!". We can apply the Lines.ToBinary function to this text string using the following code: ```

Lines.ToBinary(“Hello, world!”)

``` This will return the binary value: ```

01101000 01100101 01101100 01101100 01101111 00101100 00100000 01110111 01101111 01110010 01101100 01100100 00100001

``` The binary value is a sequence of 0s and 1s that represent the characters in the text string. Each character in the text string is converted to its binary representation, and the binary values for all the characters are concatenated to form the final binary value. Using the Lines.ToBinary Function in Power Query Now that we understand the Lines.ToBinary function, let's see how we can use it in Power Query. Suppose we have a table with a column called "Text" that contains text strings. We want to add a new column to this table that contains the binary values of the text strings in the "Text" column. We can do this using the following steps: 1. In Power Query, select the table that contains the "Text" column. 2. Click on the "Add Column" tab in the ribbon. 3. Click on the "Custom Column" button. 4. In the "Custom Column" dialog box, enter a name for the new column (e.g. "Binary"). 5. In the "Custom Column" dialog box, enter the following formula: ```

Lines.ToBinary([Text])

``` 6. Click on the "OK" button to create the new column. This will create a new column in the table called "Binary" that contains the binary values of the text strings in the "Text" column. The M Code Behind the Lines.ToBinary Function Now let's take a look at the M code behind the Lines.ToBinary function. Understanding the M code can help us better understand how the function works and how we can modify it to suit our needs. The M code for the Lines.ToBinary function is as follows: ```

(text as text) => Binary.FromText(

Text.Combine(

List.Transform(

Text.ToList(text),

Number.ToText(

Number.FromText(

Text.Upper(

Text.FromUnicode(_)

)

),

8,

“0”

),

8,

“0”

)

),

” “

),

BinaryEncoding.Base2

)

``` Let's break down this code to understand what it does. The first line of the code defines the function argument: ```

(text as text) =>

``` This tells us that the function takes one argument, which is a text string, and returns a value. The next line of the code converts the text string to a binary value: ```

Binary.FromText(

``` The Binary.FromText function is a built-in function in Power Query that converts a text string to a binary value. The function takes two arguments: the text string to be converted, and the encoding type (in this case, BinaryEncoding.Base2). The next few lines of the code transform the text string into a list of binary values: ```

Text.Combine(

List.Transform(

Text.ToList(text),

Number.ToText(

Number.FromText(

Text.Upper(

Text.FromUnicode(_)

)

),

8,

“0”

),

8,

“0”

)

),

” “

),

``` Let's break down this code to understand what it does. The first line of this code converts the text string to a list of characters: ```

Text.ToList(text),

``` The List.Transform function is a built-in function in Power Query that transforms a list of values into a new list of values. In this case, the function takes the list of characters in the text string and transforms them into a list of binary values. The next few lines of code convert each character in the list to its binary value: ```

Number.FromText(

Text.Upper(

Text.FromUnicode(_)

)

),

8,

“0”

``` Let's break down this code to understand what it does. The first line of this code converts the character to its Unicode value: ```

Text.FromUnicode(_)

``` The second line of this code converts the Unicode value to uppercase: ```

Text.Upper(

``` The third line of this code converts the uppercase Unicode value to a number: ```

Number.FromText(

``` The final line of this code converts the number to an 8-bit binary value: ```

Number.ToText(

Number.FromText(

Text.Upper(

Text.FromUnicode(_)

)

),

8,

“0”

),

8,

“0”

)

``` The Text.PadStart function is a built-in function in Power Query that pads a text value with a specified character to a specified length. In this case, the function pads the 8-bit binary value with leading 0s to ensure that it is 8 bits long. Finally, the List.Combine function is used to concatenate all the binary values into a single binary value: ```

Text.Combine(

List.Transform(

Text.ToList(text),

Number.ToText(

Number.FromText(

Text.Upper(

Text.FromUnicode(_)

)

),

8,

“0”

),

8,

“0”

)

),

” “

),

``` The binary values are concatenated with a space character in between each value to form the final binary value. In this article, we have explored the Lines.ToBinary function in Power Query and learned how to use it to convert text strings to binary values. We have also examined the M code behind the Lines.ToBinary function to better understand how it works and how we can modify it to suit our needs. The Lines.ToBinary function is a powerful tool in Power Query that can be used to transform text data into binary data. With a better understanding of how the function works, we can use it more effectively in our Power Query workflows. Power Query and M Training Courses by G Com Solutions (0800 998 9248) Power Query and M Intensive Training Course £1,050.00 – £26,550.00 Select optionsContinue Loading Done Power Query and M Introduction £395.00 – £9,750.00 Select optionsContinue Loading Done Power Query and M Intermediate £395.00 – £9,750.00 Select optionsContinue Loading Done Power Query and M Advanced £395.00 – £9,750.00 Select optionsContinue Loading Done Upcoming Courses Contact Us Subject Your Name (required) Company/Organisation Email (required) Telephone Training Course(s) Power BI Intensive TrainingPower BI introduction Power BI IntermediatePower BI AdvancedDAXPower Query MPower BI CertificationPower BI AdministrationPower PlatformPower AutomatePower AppsOTHER Your Message Upload Example Document(s) (Zip multiple files) ```
``` ```
``` ```
``` ```
``` ```
``` ```