The Modulo operator (%), not to be confused with the modulus (i.e. absolute value), returns the remainder of a division after one number is divided by another.
- 10 % 10 = 0
- 10 % 2 = 0
- 10 % 3 = 1
- 100 % 13 = 9
For example 1, 10 divided by 10 is 1 remainder 0. For example 2, 10 divided by 2 is 5 remainder 0. For example 3, 10 divided by 3 is 3 remainder 1. For example 4, 100 divided by 13 is 7 remainder 9. If it's not clear enough already, it's the remainder of the division which is the answer of the modulo operation.
Although perhaps niche, here are a few uses of this operator:
- Checking whether a number is odd or even
- Do something every nth time
- Convert an elapsed time into hours, minutes and seconds
- Pairing together longitude and latitude in a dataset where the two are not separated and are under a single 'Coordinates' column
- Creating a line chart in Tableau with multiple lines and different marks for different lines
- Assigning question numbers to a survey
Tableau and Alteryx
- In Tableau the modulo operator is %
- In Alteryx the modulo operator is Mod(n, d) where n is the dividend and d the divisor
This has been a short intro into the modulo operator. I hope it gives you some inspiration for its use cases and you keep it in your back pocket when solving problems.
In the next few blog posts I'll be writing about some of the specific use cases that I listed above.