You may have seen the modulo (mod) function used in the awesome Collatz Conjecture macro on the Gallery; here, it's used to determine the parity of the entered integer. Depending on if the number is even or odd, a different set of operations is applied to the value.
Given two positive integers, n (the dividend) and d (the divisor), n modulo d (or n mod d) is the remainder of dividing n by d. For example, the expression "3 mod 2" would return a value of 1 because 3 divided by 2 leaves a quotient of 1 and a remainder of 1, while "6 mod 3" returns 0 because the division of 6 by 3 has a quotient of 2 and leaves a remainder of 0. To test if a number is odd, you’d want to determine when a number (n) divided by 2 (d) is equal to 1. For even numbers, the result is equal to 0.
The mod function can be found in the Formula tool’s Math functions, nested under the sub-set of Integer functions.
The function, however, has many uses beyond determining if a value is even or odd. When coupled with a Record ID tool, the modulo function can be used to make groups within data. Keep reading for three examples of how to apply the mod function!
Example 1: let’s say you’ve received a dataset of geographic coordinates with the latitude and longitude for each different sites split into separate records.
You have this: You want this:
That the latitude and longitudes are stacked allows you to know that the first two records are the coordinates for Site 1, the third and fourth records are the coordinates for Site 2, and so on. You run through your options for grouping records: “The Tile tool? Handy, but this option won’t work I don’t know how many groups I have in my data. A conditional statement in the Formula tool to flag positive latitude and negative longitude values? Possible, but this definitely won’t work if my points are outside of the western hemisphere”. For a dynamic solution, consider the one-two punch of the Record ID and the Formula tool’s mod function!
Adding a Record ID provides a field for establishing a pattern. Since we know the data are in pairs or multiples of 2, the Record ID associated with the odd entries are the first of every pair. So, when the remainder of the Record ID divided by two is one, we know we’re at the first record of a paired group. We can apply this logic in a Multi-Row Formula to determine the groupings of our coordinates.
After some data manipulation, the coordinates for each site are in order and ready to be used for spatial analyses.
Example 2: Similarly, say you have a client file where the names and addresses are all in a single field, rather than discrete fields:
But you know that each client has four records, and the name is first, address1 is second, address2/ suite is third, address, the last line, is fourth. So the client names are records 1, 5, 9, and so on. Address1 is records 2, 6, 10, etc.
Use Mod([RowCount], 4) to number the records, and a conditional statement to create the Headers:
Finally, transpose to get your fields in order:
Example 3: Imagine that you have an elapsed time in seconds and you want to convert this to hours, minutes, and seconds:
The hours are just a fraction – elapsed time divided by 3600 (the number of seconds in an hour). The seconds are the remainder once you've divided the elapsed time by 60, "removing" the full minutes, and the minutes result is the remainder once you've "removed" the full hours.
Fun with math! Happy Alteryx-ing! See the examples above in the attached workflow.