Number.Mod Function in Power Query M

Sumit Bansal
Written by

Sumit Bansal is the founder of TrumpExcel.com and a 13-time Microsoft Excel MVP. He started this site in 2013 to share his passion for Excel through easy tutorials, tips, and training videos, helping you master Excel, boost productivity, and maybe even enjoy spreadsheets!

Last updated

The Number.Mod function in Power Query gives you the remainder after dividing one number by another. It works like Excel’s MOD function except with negative numbers, where Number.Mod keeps the sign of the number you divide and MOD keeps the sign of the divisor.

This article shows how to use it for leftovers, odd and even numbers, every Nth row, and more, and how to get Excel’s result for negative numbers when you need it.

Follow along with the example file

Number.Mod Function Examples.xlsx

Download

Number.Mod Function Syntax

Number.Mod(number as nullable number, divisor as nullable number, optional precision as nullable number) as nullable number
  • number – the number you want to divide (the dividend).
  • divisor – the number you divide by.
  • precision (optional) – Precision.Double (the default) or Precision.Decimal. Decimal precision avoids tiny floating-point errors when you work with decimals (Example 9).

What it returns: The remainder left after dividing the number by the divisor. If the number or the divisor is null, it returns null.

The Number.Mod function is part of the Power Query M language, so it works wherever Power Query does in Excel: Excel 365, 2024, 2021, 2019, and 2016 for Windows, and Excel for Microsoft 365 on Mac.

When to Use the Number.Mod Function

Use this function when you need to:

  • Find what’s left over after packing items into boxes or batches
  • Check whether a number is odd or even
  • Keep every 2nd, 3rd, or Nth row of a table
  • Split minutes into hours and minutes
  • Assign rows to people or groups in rotation
  • Get the decimal part of a number

Example 1: Get the Remainder of Two Numbers

Let’s start with the basic idea. Suppose you have 17 players and want to split them into teams of 5, and you want to know how many players are left without a team.

In Excel, go to Data > Get Data > From Other Sources > Blank Query. In the Power Query Editor that opens, type this formula in the formula bar:

= Number.Mod(17,5)

Result: 2

Number.Mod returns 2, the remainder when 17 is divided by 5

Here, 17 divided by 5 is 3 full teams (15 players), and 2 players are left over. Number.Mod returns that leftover, 2.

In the example file, Examples 1 and 9 type their numbers right into the query, so they don’t have a sheet. You can open them from Data > Queries & Connections.

Example 2: Find the Leftover Units After Packing Cartons

Here’s a common real-world use. Below is a table named WarehouseOrders with the units in each order, and I want a Loose Units column with the units left over after packing them into cartons of 12.

WarehouseOrders table with the number of units in each order

In the code below, the Source line uses the Excel.CurrentWorkbook function to pull the WarehouseOrders table into Power Query by its name.

If you try this on your own data, format it as a table (Ctrl + T) and put your table’s name in place of “WarehouseOrders”.

Create a new blank query (as in Example 1), open the Advanced Editor (Home > Advanced Editor), and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="WarehouseOrders"]}[Content],
#"Added Loose Units" = Table.AddColumn(Source,"Loose Units",each Number.Mod([Units],12),type number)
in
#"Added Loose Units"

Result: A new Loose Units column (6, 0, 3, 2, and 5)

A new Loose Units column with the units left after packing cartons of 12

Here, the Table.AddColumn function adds a Loose Units column, and Number.Mod divides each order’s units by 12 and returns what’s left.

The Glass Jars order has 150 units. That’s 12 full cartons (144 units), with 6 units left over.

The Scented Candles order has 96 units, which fills exactly 8 cartons, so the remainder is 0.

Important: If the divisor is 0, Number.Mod doesn’t give an error. It returns NaN (not a number), which shows as #NUM! when you load it to a sheet.

Here the divisor is always 12, so that can’t happen. But if your carton size comes from a column that could hold a 0, check for it first, for example if [Carton Size]=0 then null else Number.Mod([Units],[Carton Size]).

Number.Mod also needs real numbers. If the Units column holds text, like “150”, you get the error We cannot convert the value “150” to type Number, so change the column type to a number first.

Example 3: Check If a Number Is Odd or Even

This next one comes up quite often. Below is a table named DeliveryRoute with each customer’s house number, and I want a Street Side column that says whether each house is on the odd or the even side of the street.

DeliveryRoute table with each customer's house number

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="DeliveryRoute"]}[Content],
#"Added Street Side" = Table.AddColumn(Source,"Street Side",each if Number.Mod([House Number],2)=0 then "Even side" else "Odd side",type text)
in
#"Added Street Side"

Result: A new Street Side column (Even side, Odd side, Odd side, Even side, Odd side, and Even side)

A new Street Side column that marks each house as on the odd or even side

Here, Number.Mod divides each house number by 2. An even number leaves nothing over (a remainder of 0), and a positive odd number leaves 1.

The if statement then labels a remainder of 0 as “Even side” and anything else as “Odd side”.

It’s safer to test for =0 than for =1. For a negative odd number, Number.Mod returns -1, not 1 (Example 8 shows why), so a test for =1 would miss it.

If odd or even is all you need, Power Query also has the Number.IsEven and Number.IsOdd functions. Number.Mod is more flexible, because the same test works for any divisor, like every 3rd or 5th number.

Example 4: Keep Every Third Row of a Table

Here’s a handy trick for sampling data. Below is a table named QualityChecks with the weight of each unit that came off a production line, and I want to keep only every third unit for a spot check.

QualityChecks table with the weight of nine units

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="QualityChecks"]}[Content],
#"Added Index" = Table.AddIndexColumn(Source,"Index",1,1,Int64.Type),
#"Kept Every 3rd Row" = Table.SelectRows(#"Added Index",each Number.Mod([Index],3)=0)
in
#"Kept Every 3rd Row"

Result: A table with three rows: SN-1003, SN-1006, and SN-1009

Only every third unit (SN-1003, SN-1006 and SN-1009) is kept

Here, the Table.AddIndexColumn function numbers the rows 1, 2, 3, and so on. Then the Table.SelectRows function keeps only the rows where the index divides by 3 with nothing left over, which are rows 3, 6, and 9.

To keep every other row, use 2 instead of 3. To keep the first row of each group (rows 1, 4, 7) instead of the last, change the test to Number.Mod([Index],3)=1.

Once you have the rows you want, you can remove the Index column.

Example 5: Convert Minutes to Hours and Minutes

Number.Mod is also great for splitting a total into units. Below is a table named CallLog with the length of each call in minutes, and I want a Duration column that shows it as hours and minutes, like 2h 15m.

CallLog table with the length of each call in minutes

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="CallLog"]}[Content],
#"Added Duration" = Table.AddColumn(Source,"Duration",each Text.Format("#{0}h #{1}m",{Number.IntegerDivide([Minutes],60),Number.Mod([Minutes],60)}),type text)
in
#"Added Duration"

Result: A new Duration column (2h 15m, 0h 48m, 3h 20m, 1h 30m, and 1h 1m)

A new Duration column showing each call as hours and minutes

Here, the formula builds the text in two parts. The Number.IntegerDivide function gives the number of full hours (135 / 60 is 2 full hours), and Number.Mod gives the minutes left over (135 – 120 = 15).

The Text.Format function then puts the hours in place of #{0} and the minutes in place of #{1}, so you get text like 2h 15m.

The same pattern works for other units. For example, Number.Mod([Inches],12) gives the inches left after you take out the full feet.

Follow along with the example file

Number.Mod Function Examples.xlsx

Download

Example 6: Assign Rows in Rotation (Round Robin)

Now let’s use the remainder to cycle through a list. Below is a table named SalesLeads with new leads, and I want to hand them out to three sales reps (Melanie, Raymond, and Leslie) in turn, so the leads are spread as evenly as possible.

SalesLeads table with seven new leads

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="SalesLeads"]}[Content],
#"Added Index" = Table.AddIndexColumn(Source,"Index",0,1,Int64.Type),
#"Added Sales Rep" = Table.AddColumn(#"Added Index","Sales Rep",each {"Melanie","Raymond","Leslie"}{Number.Mod([Index],3)},type text)
in
#"Added Sales Rep"

Result: A new Sales Rep column (Melanie, Raymond, Leslie, Melanie, Raymond, Leslie, and Melanie)

A new Sales Rep column that assigns the leads to three reps in turn

Here, the index starts at 0, so the remainder after dividing it by 3 goes 0, 1, 2, 0, 1, 2, 0 down the rows.

That remainder is then used to pick a name from the list {"Melanie","Raymond","Leslie"}. Positions in a Power Query list start at 0, so {...}{0} is Melanie, {...}{1} is Raymond, and {...}{2} is Leslie.

This is why the index starts at 0 here instead of 1. With a 0-based index, the first lead goes to the first rep.

To add a fourth rep, add the name to the list and change the 3 to 4.

Example 7: Get the Decimal Part of a Number

Here’s a quick one. Below is a table named HoursLogged with the hours spent on each task, and I want a Part Hour column with just the decimal part (the part of an hour after the full hours).

HoursLogged table with the hours spent on each task

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="HoursLogged"]}[Content],
#"Added Part Hour" = Table.AddColumn(Source,"Part Hour",each Number.Mod([Hours],1),type number)
in
#"Added Part Hour"

Result: A new Part Hour column (0.25, 0.5, 0.75, 0, and 0.25)

A new Part Hour column with the decimal part of each value

Here, Number.Mod divides each value by 1. Whole numbers divide by 1 exactly, so only the decimal part is left over, and 7.25 hours gives 0.25.

Multiply the result by 60 if you want it in minutes, so 0.25 becomes 15 minutes.

The results here come out clean because quarter and half hours are easy for a computer to store exactly. With other decimals, like 19.99, you can get a tiny rounding error in the result, which Example 9 shows how to fix.

Example 8: Number.Mod with Negative Numbers

This is the one place where Number.Mod and Excel’s MOD function part ways. Below is a table named SignCheck with four number and divisor pairs that cover every mix of positive and negative signs.

SignCheck table with four number and divisor pairs

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="SignCheck"]}[Content],
#"Added Remainder" = Table.AddColumn(Source,"Remainder",each Number.Mod([Number],[Divisor]),type number),
#"Added Excel-Style Remainder" = Table.AddColumn(#"Added Remainder","Excel-Style Remainder",each Number.Mod(Number.Mod([Number],[Divisor])+[Divisor],[Divisor]),type number)
in
#"Added Excel-Style Remainder"

Result: A Remainder column (1, -1, 1, and -1) and an Excel-Style Remainder column (1, 2, -2, and -1)

Number.Mod's remainder next to the Excel-style remainder for each sign mix

Here, the Remainder column shows what Number.Mod returns on its own. The remainder always takes the sign of the number you divide, so -7 divided by 3 gives -1, and 7 divided by -3 gives 1.

Excel’s MOD function does it differently. Its result takes the sign of the divisor, so =MOD(-7,3) gives 2 and =MOD(7,-3) gives -2.

Important: If your Power Query results don’t match MOD in Excel, check for negative numbers. To get the same result as Excel, use Number.Mod(Number.Mod([Number],[Divisor])+[Divisor],[Divisor]), as in the Excel-Style Remainder column.

The fix works in two steps. For -7 and 3, the inner Number.Mod gives -1, and adding the divisor turns it into 2.

The outer Number.Mod then puts the value back between 0 and the divisor, with the divisor’s sign. For 7 and 3, the inner result is 1, plus 3 is 4, and the outer Number.Mod brings it back to 1.

This matters most when you use the remainder to wrap around, like the round robin in Example 6. A remainder of -1 can’t pick an item from a list, but a remainder of 2 can.

When the number and divisor are both positive (or both negative), the two columns match, so you only need the fix when the signs can differ.

Example 9: Avoid Tiny Decimal Errors with Precision.Decimal

The last one is about decimals. Suppose a product costs 19.99 and you want to check whether its cents part is 0.99, using Number.Mod with a divisor of 1 like in Example 7.

Create a new blank query and type this formula in the formula bar:

= {Number.Mod(19.99,1)=0.99,Number.Mod(19.99,1,Precision.Decimal)=0.99}

Result: A list with FALSE and TRUE

Comparing the remainder of 19.99 with 0.99 gives FALSE with default precision and TRUE with Precision.Decimal

Here, both items get the remainder of 19.99 divided by 1 and check whether it equals 0.99. The first one uses the default precision and returns FALSE.

That’s because computers store most decimals, like 19.99, as a very close binary approximation. The remainder comes out as 0.989999999999998, so it isn’t exactly 0.99.

You won’t see this in the preview, because Power Query shows Number.Mod(19.99,1) as 0.99. It only shows up when you compare the result or use it in another calculation.

The second item adds Precision.Decimal as the third argument, which does the math in decimal instead. The remainder is exactly 0.99, so the test returns TRUE.

Use Precision.Decimal whenever your numbers have decimals and you need an exact remainder.

Other Excel Articles You May Also Like:

All Power Query Functions

Sumit Bansal

Sumit Bansal

13x Microsoft Excel MVP

Hey! I'm Sumit Bansal, founder of trumpexcel.com and a Microsoft Excel MVP. I started this site in 2013 because I genuinely love Microsoft Excel (yes, really!) and wanted to share that passion through easy Excel tutorials, tips, and Excel training videos. My goal is straightforward: help you master Excel skills so you can work smarter, boost productivity, and maybe even enjoy spreadsheets along the way!

Get the FREE 51 Excel Tips Ebook

Enter your details and the free PDF is on its way to your inbox.

Hmm, that didn't go through. Please check your email and try again.

No spam. You'll also get my weekly Excel newsletter. Unsubscribe anytime.

Check your inbox!

The ebook is on its way to your email. It usually lands within a couple of minutes.