Byte.From

D

T

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

What is Byte.From?

Byte.From is a Power Query M function that converts a binary value to a text value. It takes a binary value as input and returns a text value. The syntax of the function is as follows:


Byte.From(binary as binary) as text


The binary parameter is the binary value that you want to convert to text. It can be a single value or a list of values.

Understanding the M Code

The M code behind the Byte.From function is relatively simple. It first checks if the input value is null. If it is, it returns null. If it is not null, it converts the binary value to text using the Text.From function. The Text.From function converts a value of any type to text. Here is the M code for the Byte.From function:


let

Byte.From = (binary as binary) as text =>

if binary = null then null else Text.From(binary)

in

Byte.From


Using Byte.From

Byte.From can be used in a variety of ways to manipulate and transform data. Here are some examples:

Example 1: Converting Binary to Text

Suppose you have a binary value that you want to convert to text. You can use the Byte.From function to do this. Here is an example:


let

binaryValue = Binary.FromText(“Hello World”, BinaryEncoding.Base64),

textValue = Byte.From(binaryValue)

in

textValue


In this example, we first convert the string "Hello World" to binary using the Binary.FromText function. We then pass this binary value to the Byte.From function to convert it to text. The resulting text value is "Hello World".

Example 2: Converting Multiple Binary Values to Text

Suppose you have a list of binary values that you want to convert to text. You can use the List.Transform function with the Byte.From function to do this. Here is an example:


let

binaryValues = {

Binary.FromText(“Hello”, BinaryEncoding.Base64),

Binary.FromText(“World”, BinaryEncoding.Base64)

},

textValues = List.Transform(binaryValues, each Byte.From(_))

in

textValues


In this example, we first create a list of two binary values using the Binary.FromText function. We then use the List.Transform function to apply the Byte.From function to each binary value in the list. The resulting list contains the text values "Hello" and "World".

Example 3: Converting Binary to Unicode

Suppose you have a binary value that represents a Unicode string. You can use the Byte.From function with the Text.Decoding.FromBinary function to convert the binary value to a Unicode string. Here is an example:


let

binaryValue = Binary.FromText(“Hello World”, BinaryEncoding.Unicode),

textValue = Text.Decoding.FromBinary(Byte.From(binaryValue), TextEncoding.Unicode)

in

textValue


In this example, we first convert the string "Hello World" to binary using the Binary.FromText function with a BinaryEncoding of Unicode. We then pass this binary value to the Byte.From function to convert it to text. Finally, we use the Text.Decoding.FromBinary function with a TextEncoding of Unicode to convert the text value to a Unicode string. The resulting Unicode string is "Hello World".

The Byte.From function is a powerful tool for manipulating and transforming binary data in Power Query. Its simple M code and versatile application make it a valuable asset for data analytics and business intelligence. With the M code behind the Byte.From function, you can easily convert binary to text, convert multiple binary values to text, and convert binary to Unicode.

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)