PowerApps – Check digit calculator

I’m quite new to the world of designing and developing Power Apps and came across an interesting puzzle during the creation of my first App. I can’t go into specifics about what the App is used for, suffice to say that there are parent records created on the server and the App is used to create multiple child records linked to the parent record. The App also has a requirement to be able to function when the device is offline (i.e. no mobile data available).

In the scenario where the users device is offline, the user will call the office and be told the ID of the parent record they are working with. The ID field is just an incremental counter on the backend and the user would enter this onto their device. To ensure that they are working with the correct parent record and also to bypass user typing errors, I thought it would be a good idea to add a check digit to this number.

I initially posted onto a PowerApps forum to see if someone had designed code for this method previously. There were no replies so I went about writing the code myself.

Firstly, there was the matter of choosing an algorithm that would generate the check digit. The Luhn Algorithm seemed to fit the requirements – it wasn’t overly complex and is used on credit card numbers so must be accurate. I used a variant of the algorithm using modulus 10.

These are the steps for the calculation (taken from this website).

Summary: Given an identifier, let’s say “139”, you travel right to left. Every other digit is doubled and the other digits are taken unchanged. All resulting digits are summed and the check digit is the amount necessary to take this sum up to a number divisible by ten.

1. Work right-to-left, using “139” and doubling every other digit.
9 x 2 = 18
3 = 3
1 x 2 = 2
2. Now sum all of the digits (note ’18’ is two digits, ‘1’ and ‘8’). So the answer is ‘1 + 8 + 3 + 2 = 14′ and the check digit is the amount needed to reach a number divisible by ten. For a sum of ’14’, the check digit is ‘6’ since ’20’ is the next number divisible by ten.

Below is a recording of the App being run using the test value of 139

The code took a fair bit of time to develop as PowerApps is not really designed to work with loops and arrays, hence the need for several collections. Below is the full code:

ClearCollect(colStringChars, Split(txtInputValue.Text, ""));


ClearCollect(colStringSeq, {RecNum:0});
                 ValueToSum:If(Mod(CountRows(colStringSeq) + 1 + varOddEvenOffset,2)=1,Result,Result * 2)

UpdateContext({varStage1Total: Concat(colStringSeq, ValueToSum)});

Collect(colStage2Numbers, Split(varStage1Total, "")); 

UpdateContext({varStage2Sum: Sum(colStage2Numbers, Result)});

UpdateContext({varFinalCheckDigit: If(Mod(varStage2Sum,10) = 0,0, 10 - Mod(varStage2Sum,10))});

UpdateContext({varFinalOutput: txtInputValue.Text & varFinalCheckDigit});

I’m sure the code could be optimised to have less lines but I wanted to keep it readable (for when I come back to it in 6 months times to see how it works 😄).

That’s it – I’d appreciate any comments about the method I used, or the actual code developed.

I’ll be blogging every few weeks in the future about any more trinkets of PowerApps information I come across. Thanks!

Header Photo by Markus Spiske on Unsplash

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s