Number.Mod

D

T

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

What is Number.Mod?

Before we dive into the M code behind Number.Mod, let’s quickly review what this function does. Number.Mod takes two arguments, divisor and dividend, and returns the remainder after dividing dividend by divisor. For example, Number.Mod(10, 3) would return 1, since 3 goes into 10 three times with a remainder of 1.

The M code behind Number.Mod

In Power Query, all functions are written in the M language. The M code behind the Number.Mod function is relatively simple. Here is the code:


let

Number.Mod = (dividend, divisor) =>

let

quotient = Number.IntegerDivide(dividend, divisor),

remainder = dividend – divisor quotient

in

remainder

in

Number.Mod


Let's take a closer look at what's happening here. The Number.Mod function is defined using the "let" keyword, which allows us to define variables and functions. The function takes two arguments, dividend and divisor. Inside the function, we define two more variables - quotient and remainder.

The quotient is calculated using the Number.IntegerDivide function, which returns the result of dividing dividend by divisor and rounding down to the nearest integer. This gives us the number of times that divisor goes into dividend.

The remainder is then calculated by subtracting the product of divisor and quotient from dividend. This gives us the remaining amount after all the full divisors have been subtracted from the dividend.

Finally, the function returns the remainder value using the "in" keyword.

Using Number.Mod in Power Query

Now that we understand the M code behind Number.Mod, let's see how it can be used in Power Query. Here are some examples:


= Number.Mod(10, 3)


This would return 1, as we saw earlier.


= Table.AddColumn(Source, “Remainder”, each Number.Mod([Value], 5))


This would add a new column to a table called "Remainder", where the value for each row is calculated by taking the remainder of the "Value" column divided by 5.

In this article, we've taken a closer look at the M code behind the Number.Mod function in Power Query. We've seen how the function works and how it can be used in different scenarios. Understanding the M code behind functions like Number.Mod is essential for anyone working with Power Query, as it allows you to create custom functions and solve complex data problems.

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)