# Binary.Length

## Understanding the Binary.Length Function

The Binary.Length function is a part of the Power Query M language, which is a functional programming language used to manage and transform data. As the name suggests, the Binary.Length function is used to calculate the length of binary values in a data set. It takes a binary value as an input and returns the length of the binary value.

## Syntax of Binary.Length Function

The syntax of the Binary.Length function is as follows:

``` Binary.Length(binary as binary) as number ```

The function takes a binary value as an input and returns a number that represents the length of the binary value. Let’s take a look at an example to understand the function better.

Suppose we have a data set that contains binary values in the ‘BinaryData’ column. We can use the Binary.Length function to determine the length of these binary values. The M code for this would be:

``` let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcikqzS9R0lEyNjQwVUjMKy8GzgYjIzC3LzMvJzMxNwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [BinaryData = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"BinaryData", type binary}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Binary Length", each Binary.Length([BinaryData])) in #"Added Custom" ```

The above M code will add a new column to the data set named ‘Binary Length’ that contains the length of the binary values in the ‘BinaryData’ column.

## Exploring the M Code Behind Binary.Length Function

Now that we understand the syntax and functionality of the Binary.Length function, let’s explore the M code behind it. The M code for the Binary.Length function is as follows:

``` (Binary as binary) as number => List.Count(Binary.ToList()) ```

The above code is a shorthand syntax for the Binary.Length function. The code takes a binary value as an input and returns the length of the binary value. Let’s break down the code to understand how it works.

The first part of the code, `(Binary as binary) as number`, is the parameter declaration. The function takes a binary value as an input and returns a number that represents the length of the binary value.

The second part of the code, `List.Count(Binary.ToList())`, is the body of the function. The function converts the binary value into a list and then calculates the length of that list using the List.Count function.

In conclusion, the Binary.Length function is a powerful tool in the Power Query M language that is used to determine the length of binary values in a data set. The function takes a binary value as an input and returns the length of the binary value. Understanding the M code behind the function is essential for users who wish to customize and enhance the functionality of Power Query M.

Subject