mod() function

What is Modulo or the mod() function

I learnt about the mod() function (or modulo (%)) whilst studying Python during my Computers Degree and fortunately still had it hidden in the back of my mind.

In mathematics, the term modulo (“with respect to a modulus of”, the Latin ablative of modulus which itself means “a small measure”) is often used to assert that two distinct mathematical objects can be regarded as equivalent—if their difference is accounted for by an additional factor

https://en.wikipedia.org/wiki/Modulo_(mathematics)

Why I needed the mod() function in Power Automate

I recently had a customer who had a bespoke approvals process for reviewing policy documentation. The process is part of a larger Power Platform Solution utilising a Canvas Power App and several Power Automate Flows for other purposes.

The customer needed a review reminder email sent out 8 weeks prior to the review due date and also every two weeks after the due date if it had passed.

What Type of Schedule for my flow?

I considered using the Scheduled Trigger which is an obvious start, but then had to deliberate on its frequency. Whilst I can set the flow to run every 14 days, this would not maintain accurate review reminders especially if someone hit their 14 day review period the following day. I could also run a daily schedule but once someone had passed the 14 day point, they would be reminded every day. I did however, want to run a flow on a daily basis to at least identify each day, who should receive a 14 day reminder. Lets not forget we also need to include the reminder 8 weeks prior to the due date.

Implementing the Flow

The Trigger

As explained above, I wanted to run the flow daily to ensure someone was reminded promptly to take the action they needed.

The Date Difference

The First part of the requirement was an easy step. This was simply to use the dateDiff() expression to cross refer the review due date with today’s date.

The mod() function

The mod() function or modulo operator works by dividing a given number and providing the remainder amount. if a date difference is divisible by 14, this would give me an output of 0 thus it would be the correct day to produce a reminder email. Unfortunately the issue comes about when we are calculating days. the formulas above work, but the output is not a straight forward integer (which is what the mod() function requires).

Screenshot of the Output for a Date Difference Compose in Power Automate

The mod() function won’t interpret the output above for 2 reasons.

  1. The format of the numbers are separated by characters i.e. there are several values in the output.
  2. The overall output is recognized as a string and not an integer / float etc.

The clever part

We now need to take two further actions to remove the days part of the output and also to ensure we conduct the mod() function with integers.

You will notice that the start of the output holds our number of days difference (14) as well as other differentials (namely the milliseconds it took to run through the flow). We can split() this out from the rest of the output and use the decimal place as the split identifier. Fortunately, the value I want is the first in the sequence so I can also use the first() expression.

I can then initiate the mod() function making sure I wrap the output of the split() days with an int() function for it to function correctly.

Completing the process

Now that we have taken all the steps to tidy the date differences and instigate the mod() function, all wee need to do is run a condition to see if a review reminder should be sent.

Summary

Hurray! We’ve successfully created a flow which:

  • Gathers dates,
  • Compares them,
  • Identifies the difference value for comparison,
  • Converts them to Integers, and
  • Parses them through the mod() function to get our result!

Leave a Reply

Your email address will not be published. Required fields are marked *