This site uses different types of cookies, including analytics and functional cookies (its own and from other sites). To change your cookie settings or find out more, click here. If you continue browsing our website, you accept these cookies.
09-07-2016 11:56 AM - edited 07-15-2021 09:16 AM
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.
Excellent examples - thanks Criston and Christine!
Is there an opposite to the MOD function? I'm looking for some function or way to get the quotient integer ( so not including the remainder) like the Quotient function in excel. Is there a Alteryx formula for this or a work around that would yield this result?
@DataUser I don't believe there is a single function but it can easily be achieved by using a simple formula:
C = Floor( [A] / [B] )
where C will contain the integer quotient portion of the division of A and B.
very nice method applied to good case!
I need some more time to get used to for the multi-row formulas.
I would use a combination of IF and Mod function using 4. Something like the Excel's equivalent of IF(MOD(E3,4)=1,1,IF(MOD(E3,4)=2,1,2))
@DataUser, (A - Mod(A,B))/B should work as well. A little way around.
Great Article! However, I discovered that you can pivot the table in Example 1 using a combination of Formula and Cross-Join Tools. After using the Multi-Row Tool, insert another Formula tool with below expression to classify each row as either Latitude or Longitude. You could then use the Cross Tab tool with the following options: Group Data by 'Pairs', 'Change Column Headers' by Label, Values for New Columns by 'Coordinates' and Aggregate values by Last to produce the same results. It would shorten the number of tools required to achieve the end result. Hope this helps!
Label = IF Mod([RecordID], 2) = 1 then 'Latitude' else 'Longitude' endif