Category Archives: Power Apps

Power Apps – Using Variables in ForAll()

The ForAll command is very handy if you want to loop around a collection and perform some calculations.  One limiting feature is that you can’t use variables in the ForAll loop, using UpdateContext or Set.

There is a workaround, which is to create and use a single record collection to hold the values which can then be used as variables. The downside is that it does make the “code” not as straightforward to read.

This is the process I use:

Add a line to the Form OnVisible to reset the collection (unless you’re using the variable like a Global in which case put it in the App OnStart).

ClearCollect(colVariables, {colvarVariableOne:””});

To read the variable value use


To set the variable value use:

Patch(colVariables, First(colVariables), {colvarVariableOne:”NEW VALUE”});

One thing that can catch you out is that collection column types are set when the collection is initiated so set the default value to be of that type. For example to create a datetime column use:

ClearCollect(colVariables, {colvarHoliday:DateTimeValue(“01/01/2000 00:00”});

Photo by Tine Ivanič on Unsplash

Power Apps – Photo Camera Selection

For any mobile app these days it’s normally a requirement to be able to take photos for reference, and the new App I’m working on is no different.

Adding a photo facility to an App is very straightforward – put a Camera control onto a screen and add code in the OnSelect to take the Photo and add it a collection using Patch. Here’s my code to do this:

Patch(colPhotos, Defaults(colPhotos), {Value:Camera1_1.Photo, PhotoDateTime:Now()});

Of course most devices have multiple cameras these days – at minimum one front facing, and one rear facing.   PowerApps allows you to change the camera in use by setting the Camera property of the Camera control (!) with a number.  

These are the devices I have, and their associated Camera numbers

Samsung Galaxy S7 
0 = Rear Camera, 1 = Front Camera, 2 = Not Available

IPhone 7
0 = Rear Camera, 1 = Front Camera, 2 = Front Camera

Samsung Galaxy S9
0 = Front Camera, 1 = Front Camera (Zoomed in), 2 = Rear Camera

NOTE: All the devices I’ve tested only have two cameras, 1 front and 1 rear – though there are many newer devices which have several rear cameras.  I’m guessing that they would have Camera numbers from 2 onwards though I can’t verify this.

So it appears that the rear camera, which the user would most likely use, is not always 0.  To allow the user to change camera I use a variable and store this to local cache so the App will start on the same camera the user was last on.

Code wise – the Camera control’s Camera property is set to varCameraNumber.  There is a ‘Switch Camera’ button which loops around Cameras 0 to 2 using the code:


This is the code in the Screen OnVisible section.  It uses LoadData with the 3rd parameter IgnoreNonexistentFile set to True.  This is for when the App is first run and the file doesn’t exist.

LoadData(colCameraNumber, “colCameraNumber” ,true);
If(CountRows(colCameraNumber) = 0,

The last bit of code is at the end of the OnSelect of the ‘Switch Camera’ button.


That’s it, a fully functioning Camera control allowing switching of the viewing camera, which is saved per session too.  🙂

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