BinaryFormat.Length
BinaryReturns a binary format that limits the amount of data that can be read.
Syntax
BinaryFormat.Length(binaryFormat as function, length as any) as functionParameters
| Name | Type | Required | Description |
|---|---|---|---|
binaryFormat | function | Yes | The binary format to limit. |
length | any | Yes | The number of bytes to read, or a binary format value that reads the length from the stream before the data. |
Return Value
function — A binary format function that reads data up to the specified byte limit.
Remarks
BinaryFormat.Length returns a binary format that restricts how many bytes a given binary format can consume. This is useful when a binary format like BinaryFormat.List or BinaryFormat.Binary would otherwise read to the end of the stream.
The length parameter can be:
- A number -- Limits the read to exactly that many bytes.
- A binary format -- Reads the length value from the stream first (using the specified format), then limits the subsequent read to that many bytes.
This is particularly useful for parsing binary structures where a length field precedes a variable-length payload, or when you need to read only a fixed portion of a stream with a format that would normally consume all remaining data.
Examples
Example 1: Limit the number of bytes read
let
binaryData = #binary({1, 2, 3}),
listFormat = BinaryFormat.Length(
BinaryFormat.List(BinaryFormat.Byte),
2
)
in
listFormat(binaryData)Example 2: Use a length prefix from the stream
let
binaryData = #binary({1, 2, 3}),
listFormat = BinaryFormat.Length(
BinaryFormat.List(BinaryFormat.Byte),
BinaryFormat.Byte
)
in
listFormat(binaryData)Compatibility
✓ Power BI Desktop✓ Power BI Service✓ Excel Desktop✓ Excel Online✓ Dataflows✓ Fabric Notebooks