BinaryFormat.SignedInteger16

D

T

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

Understanding the BinaryFormat.SignedInteger16 function

Before we delve into the M code behind the BinaryFormat.SignedInteger16 function, let us first understand what this function does. The BinaryFormat.SignedInteger16 function is used to convert data types into signed 16-bit integers. This function takes two arguments: the first argument is the data type that needs to be converted, and the second argument is the endianness of the byte order.

The data type argument can be any valid Power Query data type such as text, number, date, or time. The endianness argument is an optional argument that determines the byte order of the output. If this argument is not specified, the function assumes a little-endian byte order.

The M code behind the BinaryFormat.SignedInteger16 function

Now that we understand what the BinaryFormat.SignedInteger16 function does, let us dive into the M code behind this function. The M code for the BinaryFormat.SignedInteger16 function is as follows:


let

BinaryFormat.SignedInteger16 = (value as nullable any, optional endianness as nullable any) as nullable any =>

let

binaryValue = BinaryFormat.Integer16(value, endianness),

signedValue = if binaryValue > 32767 then binaryValue – 65536 else binaryValue

in

signedValue

in

BinaryFormat.SignedInteger16


As you can see from the code, the BinaryFormat.SignedInteger16 function is simply a wrapper for the BinaryFormat.Integer16 function. The BinaryFormat.Integer16 function is used to convert the input value into a 16-bit signed integer, and the BinaryFormat.SignedInteger16 function then checks if the value is greater than 32767. If the value is greater than 32767, the function subtracts 65536 from the value to get the correct signed integer value.

Using the BinaryFormat.SignedInteger16 function

Now that we understand the M code behind the BinaryFormat.SignedInteger16 function, let us look at some examples of how this function can be used to manipulate data.

Example 1: Converting text to a signed integer

Suppose we have a column in our Power Query table that contains text values. We want to convert these text values into signed integers. We can use the BinaryFormat.SignedInteger16 function to achieve this. Here is the M code to achieve this:


let

Source = #table({“TextValues”}, {{“10”}, {“-20”}, {“30”}, {“-40”}}),

#”Changed Type” = Table.TransformColumnTypes(Source,{{“TextValues”, type text}}),

#”Added Custom” = Table.AddColumn(#”Changed Type”, “SignedIntegers”, each BinaryFormat.SignedInteger16([TextValues]))

in

#”Added Custom”


This code creates a table with a column called "TextValues" that contains four text values. We then use the Table.TransformColumnTypes function to convert this column into a text data type. Finally, we add a custom column called "SignedIntegers" that uses the BinaryFormat.SignedInteger16 function to convert the text values into signed integers.

Example 2: Converting numbers to a signed integer

Suppose we have a column in our Power Query table that contains numeric values. We want to convert these numeric values into signed integers. We can use the BinaryFormat.SignedInteger16 function to achieve this. Here is the M code to achieve this:


let

Source = #table({“NumericValues”}, {{10}, {-20}, {30}, {-40}}),

#”Added Custom” = Table.AddColumn(Source, “SignedIntegers”, each BinaryFormat.SignedInteger16([NumericValues]))

in

#”Added Custom”


This code creates a table with a column called "NumericValues" that contains four numeric values. We then add a custom column called "SignedIntegers" that uses the BinaryFormat.SignedInteger16 function to convert the numeric values into signed integers.

Example 3: Converting dates to a signed integer

Suppose we have a column in our Power Query table that contains date values. We want to convert these date values into signed integers. We can use the BinaryFormat.SignedInteger16 function to achieve this. Here is the M code to achieve this:


let

Source = #table({“DateValues”}, {{#date(2021,1,1)}, {#date(2021,2,1)}, {#date(2021,3,1)}, {#date(2021,4,1)}}),

#”Added Custom” = Table.AddColumn(Source, “SignedIntegers”, each BinaryFormat.SignedInteger16([DateValues]))

in

#”Added Custom”


This code creates a table with a column called "DateValues" that contains four date values. We then add a custom column called "SignedIntegers" that uses the BinaryFormat.SignedInteger16 function to convert the date values into signed integers.

In conclusion, the BinaryFormat.SignedInteger16 function is a powerful tool that allows users to convert data types into signed 16-bit integers. This function can be used in a variety of scenarios, making it an essential tool for data analysts and developers alike. By understanding the M code behind this function, users can better manipulate data in Power Query.

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)