The M Code Behind the Power Query M function BinaryFormat.ByteOrder

Understanding Byte Order

Before we dive into the M code behind BinaryFormat.ByteOrder, it’s important to understand what byte order is and why it matters. Byte order refers to the way in which a computer stores multi-byte data types (such as integers) in memory. There are two common byte orders: big-endian and little-endian.

In big-endian byte order, the most significant byte (i.e. the byte with the highest value) is stored first. In little-endian byte order, the least significant byte (i.e. the byte with the lowest value) is stored first. The byte order used by a particular computer architecture can have a significant impact on the way data is interpreted and processed.

Using BinaryFormat.ByteOrder

BinaryFormat.ByteOrder is a Power Query M function that can be used to change the byte order of binary data. The function takes two arguments: the binary data to be converted, and a value indicating the desired byte order. The byte order value can be either “BigEndian” or “LittleEndian”.

Here’s an example of how BinaryFormat.ByteOrder can be used to convert binary data from little-endian to big-endian byte order:


data = Binary.FromText(“01020304”, BinaryEncoding.Hex),

bigEndianData = BinaryFormat.ByteOrder(data, “BigEndian”)



In this example, we start with a binary value of "01020304", which represents the integer value 16909060 in little-endian byte order. We use Binary.FromText to convert this value into binary data, and then use BinaryFormat.ByteOrder to convert it to big-endian byte order. The resulting binary data is "04030201", which represents the same integer value in big-endian byte order.

The M Code Behind BinaryFormat.ByteOrder

So, what's the M code behind BinaryFormat.ByteOrder? Let's take a look:


sourceBinary = Value.ReplaceType(binaryData, Value.Type(Binary.Type)),

sourceType = Value.Type(sourceBinary),

sourceEndianness = if endian = “LittleEndian” then Endianness.LittleEndian else Endianness.BigEndian,

targetType = Type.ReplaceMetadata(sourceType, [Binary.Format = BinaryFormat.Endianness(sourceEndianness)]),

resultBinary = Value.ReplaceType(sourceBinary, targetType)



As you can see, the M code behind BinaryFormat.ByteOrder is relatively simple. The function starts by replacing the type of the input binary data to ensure that it has the correct type. Next, it determines the desired byte order based on the value of the "endian" argument (which is either "BigEndian" or "LittleEndian"). It then creates a new type that specifies the desired byte order using the BinaryFormat.Endianness function. Finally, it replaces the type of the input binary data with the new type to produce the output binary data in the desired byte order.

BinaryFormat.ByteOrder is a useful Power Query M function that can be used to change the byte order of binary data. Understanding byte order is important when working with multi-byte data types, and BinaryFormat.ByteOrder provides an easy way to convert between big-endian and little-endian byte order. By looking at the M code behind this function, we can gain a deeper understanding of how it works and how it can be used to manipulate data in Power Query.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us


    Your Name (required)


    Email (required)


    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)