# BinaryFormat.Length

## Understanding BinaryFormat.Length

BinaryFormat.Length is a function used with binary values in Power Query M. It returns the length of a binary value in bytes. This function is useful when working with binary data, such as images, audio files, and documents.

The syntax for BinaryFormat.Length is as follows:

BinaryFormat.Length(binary as binary) as number

``` The function takes a binary value as its argument and returns the length of the binary value in bytes as a number. M Code Example Here's an example that demonstrates the use of BinaryFormat.Length in Power Query M: ```

let

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

length = BinaryFormat.Length(binaryValue)

in

length

``` In this example, we first create a binary value from the text "Hello World" using the Binary.FromText function. We then pass this binary value to the BinaryFormat.Length function, which returns the length of the binary value in bytes. The result is stored in the length variable. Tips for Using BinaryFormat.Length Here are some tips for using BinaryFormat.Length in Power Query M: 1. Check for Null Values When working with binary data, it's important to check for null values before using BinaryFormat.Length. Null values can cause errors in your code, so it's important to handle them properly. ```

let

binaryValue = null,

length = if binaryValue <> null then BinaryFormat.Length(binaryValue) else null

in

length

``` In this example, we check if the binaryValue variable is null before passing it to BinaryFormat.Length. If binaryValue is null, we return null. Otherwise, we pass binaryValue to BinaryFormat.Length and return the result. 2. Use Binary Encoding When working with text data, it's important to use binary encoding to convert the text to binary before using BinaryFormat.Length. This ensures that the data is represented correctly in binary format. ```

let

textValue = “Hello World”,

binaryValue = Binary.FromText(textValue, BinaryEncoding.Base64),

length = BinaryFormat.Length(binaryValue)

in

length

``` In this example, we first convert the textValue variable to a binary value using Binary.FromText with the Base64 encoding. We then pass this binary value to BinaryFormat.Length to get the length of the binary data. 3. Use with Other Binary Functions BinaryFormat.Length can be used with other binary functions in Power Query M to manipulate and transform binary data. For example, you can use BinaryFormat.Length with Binary.SelectRange to extract a specific range of bytes from a binary value. ```

let

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

length = BinaryFormat.Length(binaryValue),

range = Binary.SelectRange(binaryValue, 0, length-6)

in

range

``` In this example, we first create a binary value from the text "Hello World" using Binary.FromText. We then pass this binary value to BinaryFormat.Length to get the length of the binary data. We subtract 6 from the length to get a range of bytes that excludes the last 6 bytes. We use Binary.SelectRange to extract this range of bytes from the binary value. BinaryFormat.Length is a useful function in Power Query M for working with binary data. It allows you to get the length of a binary value in bytes, which is helpful when manipulating and transforming binary data. By using BinaryFormat.Length with other binary functions, you can easily extract specific ranges of bytes from binary data and perform other manipulations. 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) ```
``` ```
``` ```
``` ```
``` ```
``` ```