The With function is a great way to simplify formulas, reduce the query load on Data Sources and therefore improve performance
So when I say create ‘Control Variables using the With function’, what exactly do I mean?
Well, it’s not an official Power Apps description but I think a ‘Control Variable’ is good way to describe the output that can be generated by the With function. The output of the With function has a value and a name, just like a ‘real’ variable
Global variables exist across the whole App. Context (local) variables exist on individual screens. ‘Control Variables’ exist only within the control in which they have been created. Hence my use of the term ‘Control Variable’! Actually, to be more accurate, ‘Control Variables’ only exist within the With statement in which they are created
Let me explain using the simple example of combining the contents of 2 fields together to display in a single Label. This is a good way to demonstrate how a ‘Control Variable’ can be created passively within a Label. I’ll use the basic SharePoint List containing band members that I’ve used in previous posts
Concatenating Fields in a Gallery
Concatenating data is straightforward with a Gallery. All Records in a Gallery can be accessed via the ThisItem operator. This makes Galleries incredibly useful and intuitive to use. To concatenate 2 fields in a Gallery into a single field is as simple as:
Concatenate(ThisItem.First_Name," ",ThisItem.Last_Name)
Or simply
ThisItem.First_Name & " " & ThisItem.Last_Name
This also works for a Label outside the Gallery. Just prefix ThisItem with the Gallery name. Concatenating 2 fields together in a Gallery doesn’t require the With function
Concatenating Fields in a Label
Now let’s try and do the same in a Label not contained within or referencing a Gallery
As the ThisItem operator isn’t available, we have to use Lookup to identify the record and retrieve the first name. Then we have to use LookUp a second time to get the last name
LookUp(Band_Member,Band="Nirvana").First_Name & " " & LookUp(Band_Member,Band="Nirvana").Last_Name
Having to access the record twice is inefficient and an improved solution is required
Retrieving the Record and Storing in a Variable
An improvement would be to retrieve the full Record and then access the individual fields as required
We could do that by creating a Global or Local Variable. However, that can’t be done directly within the Label itself because label’s don’t have an OnSelect property. We would need to create the variable in some other way, by using the OnVisible property of the screen for example. This isn’t difficult, just inconvenient
With Function: Creating a 'Control Variable'
A better solution by far is to create a ‘Control Variable’ simply by using the Label control itself
Using the With function, we can use LookUp to retrieve the record we want and give it a name. Once this is done, we can access each of the fields in the record using the dot.notation. Using this method, only one LookUp is required. There is no need to make multiple calls to the data source
Assign the following to the Text property of a Label:
With({ctlMusician:LookUp(Band_Member,Band="Nirvana")}, ctlMusician.First_Name & " " & ctlMusician.Last_Name)
You can see I’ve called the ‘Control Variable‘ to which I’ve assigned the record ctlMusician. The ‘ctl’ prefix denotes it’s a ‘Control Variable’, just like I use the gbl and loc prefixes for the other types of variables
As I pointed out earlier, you won’t find any references to a ‘Control Variable’ in Power Apps documentation. However, it works exactly as the other variables do, so I find it appropriate to think of the With function in this way. There is even a broad similarity between the structure of the With function and that to create a Context Variable
It’s important to remember, the ‘Control Variable’ only exists within the With statement itself. It can’t be accessed from other controls or even within the same control from outside the With statement
Be that as it may, in situations where the requirement is more complex than simply combining the data in 2 fields, the With function can be a great way to streamline and simplify formulas and improve performance of the App
More Possibilities
It’s possible to assign an individual field to a ‘Control Variable’ rather than a full record. Also, the value of the ‘Control Variable’ doesn’t have to come from a data source, it can be taken from another control, a Text Input box for example. Changing the value in the Text Input box will immediately update the value of the ‘Control Variable’
Multiple ‘Control Variables’ can be assigned using the With function in a Label (or other controls) and the value can be hardcoded within the With function itself, for example:
With({Hours:24,Minutes:60,Seconds:60},"Seconds in a Day: " & Hours * Minutes * Seconds)
With functions can also be nested inside each other
Getting your head fully around the With function can take a few attempts and requires a bit of thinking time, but I’ve found it really worthwhile making the effort
- Hiredgun.tech: Power Apps Variables Checklist
- Power Apps Reference: With Function
I found using With confusing but thinking this way helps, though I don’t think I’ve fully got it yet. I liked the summary you did on the Variables Checklists. Would you also add With variables to this too?
Hi Brian. That is a really good idea, though I’ll make the ‘control variable’ post seperate to avoid any confusion with real variables. I’m pleased you are enjoying the site