Number.IsOdd

D

T

The M Code Behind the Power Query M function Number.IsOdd

The M code behind the Number.IsOdd function is simple yet effective. Let’s take a closer look at how it works.

How the Number.IsOdd Function Works

The Number.IsOdd function takes a single argument, which is the number you want to check. It then returns a Boolean value indicating whether the number is odd or even.

Here’s the M code behind the function:


Number.IsOdd = (number) =>

if Number.Mod(number, 2) = 1 then true else false


Let's break down what this code does.

The first line defines the function and its argument. In this case, the argument is called "number."

The second line checks whether the number is odd or even. It does this by using the Number.Mod function, which returns the remainder of dividing two numbers. If the remainder is 1, then the number is odd. If the remainder is 0, then the number is even.

The third line returns a Boolean value indicating whether the number is odd or even. If the remainder is 1, then the function returns true. If the remainder is 0, then the function returns false.

How to Use the Number.IsOdd Function

Now that we know how the Number.IsOdd function works, let's see how we can use it in Power Query.

Suppose we have a table with a column of numbers called "Values." We want to add a column that indicates whether each value is odd or even.

Here's the M code to do that:


let

Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText(“i45WMjIwMjQ2NtbVU0lSK1FIz0xRijKz8lPMzA28ElyUUouVl5KzFjKQMA”, BinaryEncoding.Base64)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Values = _t])),

#”Added Custom” = Table.AddColumn(Source, “IsOdd”, each Number.IsOdd([Values]))

in

#”Added Custom”


Let's break down what this code does.

The first line defines a variable called "Source" which represents our table of data.

The second line adds a custom column to the table called "IsOdd." The "each" keyword indicates that we want to apply the Number.IsOdd function to each row of the table. We pass the "Values" column of the current row as the argument to the function.

The third line returns the modified table with the new "IsOdd" column.

In this article, we've looked at the M code behind the Power Query M function Number.IsOdd. We've seen how the function works and how to use it in Power Query. By understanding the M code behind the functions in Power Query, you can gain a deeper understanding of how the tool works and become a more effective data analyst.

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)