MOD
The remainder of a division, with the divisor's sign.
Group: Math. Compatible with DAX.
Syntax
MOD ( <number>, <divisor> )
| Parameter | Type | Description |
|---|---|---|
number | Number | The number to divide. |
divisor | Number | The number to divide by; it may not be zero. |
Returns
Number. The remainder of the division. It has the sign of the divisor.
Remarks
- The remainder follows the divisor's sign:
MOD ( -7, 3 )is 2 andMOD ( 7, -3 )is -2. Some programming languages would say -1 and 1 here. - Put another way, the result is
number - divisor * INT ( number / divisor ). - A divisor of zero, or a blank one, is an error. A blank number gives BLANK.
- Decimals work too:
MOD ( 7.5, 2 )is 1.5. As with any floating-point number, a result such asMOD ( 2.3, 1 )may not be exactly 0.3; round it before comparing it with a value. - Common uses: every second row (
MOD ( [Index], 2 ) = 0), or the position in a cycle of weeks or shifts.
Examples
The sign comes from the divisor
EVALUATE
ROW (
"7 by 3", MOD ( 7, 3 ),
"-7 by 3", MOD ( -7, 3 ),
"7 by -3", MOD ( 7, -3 ),
"-7 by -3", MOD ( -7, -3 ),
"7.5 by 2", MOD ( 7.5, 2 )
)
| 7 by 3 | -7 by 3 | 7 by -3 | -7 by -3 | 7.5 by 2 |
|---|---|---|---|---|
| 1 | 2 | -2 | -1 | 1.5 |
Crates over after full pallets of 8
EVALUATE
ADDCOLUMNS ( DATATABLE ( "Crates", INTEGER, { { 7 }, { 12 }, { 30 } } ), "Left over", MOD ( [Crates], 8 ) )
ORDER BY [Crates]
| Crates | Left over |
|---|---|
| 7 | 7 |
| 12 | 4 |
| 30 | 6 |