Using the With function it’s possible to create something very similar to a variable that just exists within the With statement in which it is created.  I call this a ‘Control Variable‘.  It isn’t an offical Power Apps variable and you won’t see any Microsoft documentation references to it, but I find it helps my understanding of the With function to think in terms of a Control Variable.  See my earlier post With Function: Creating a ‘Control Variable’ for more info on this

Recently I wrote about what’s possible and not-possible with Global and Context variables: Power App Variable Checklist.  It was suggested that I create a similar Control Variable checklist.  That was a great idea and it reinforces just how similar ‘Control Variables’ are to the real thing!

'Control Variables'

I’ll use the same simple data sample that I used for the Global and Context variables.  I’ll assign information on students to a ‘Control Variable’ from a Collection 

The With function is different to the functions that create Global and Context variables in one very important aspect. As well as creating a ‘Control Variable’, the With function also generates output.  It’s the first attribute of the With function that creates the ‘Control Variable’ containing a value, record, or a table.  The second attribute of the With function is a formula that generates the output

It’s important to understand that the format of the output formula depends on the control being used.  For example, a With function in a Label can create a ‘Control Variable‘ that contains a record, but the output must reference a field (by using the dot.notation).  If the output refers to the full record then it cannot be displayed in the Label and it will generate an error.  Irrespective of the ‘Control Variable’ that is created, a Label has to display a single field.  It cannot display a record

The output of the With function can also be another function, as in the Patch example below.  To help clarify, I’ve added the result of the output formula below 

Control Variable Checklist

Action Example Output
Assign a value to a variable
With({ctlStudent1:"James"},ctlStudent1)
James
Assign values to multiple variables with a single command
With({ctlStudent1:"James",ctlStudent2:"Sarah"},ctlStudent1)
James
Assign a record to a variable
With({ctlStudent1:{name:"James",age:21,gender:"Male"}},ctlStudent1.name)
James
Assign records to multiple variables using a single command
With({ctlStudent1:{name:"James",age:21,gender:"Male"},ctlStudent2:{name:"Sarah",age:23,gender:"Female"}},ctlStudent1.name)
James
Use LookUp to assign a record to a variable
With({ctlStudent1:LookUp(colStudent,name="James")},ctlStudent1.age)
21
Use LookUp to assign a field to a variable
With({ctlStudent1:LookUp(colStudent,name="James").age},ctlStudent1)
21
Use Patch to change one or more fields in a record
With({ctlStudent:22}, Patch(colStudent,LookUp(colStudent,name="James"),{age:ctlStudent}))

Requires a Control with an OnSelect property such as a Button
Pached to 22
Assign multiple records to a single variable
Cannot be done. Embed a Table instead (see below)
N/A
Embed a Table into a variable
With({ctlStudents:Table({name:"James",age:21,gender:"Male"},{name:"Sarah",age:23,gender:"Female"})}, First(ctlStudents).name)
James

Because the With function contains an output, it does more than simply assigning a ‘Control Variable’.  This makes perfect sense because the ‘Control Variable‘ can only exist within the With statement itself.  If there wasn’t an output as part of the With function, the ‘Control Variable‘ would be of very limited value, in other words it would be useless!

Feel free to ask any questions you may have.  I’ll be happy to answer

Leave a Comment

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

Scroll to Top