In this short post on Power Apps Variables, I’ll summarise how values and single records are assigned and updated.  I’ll also cover what can’t be done with variables, so time isn’t wasted trying – something that has caught me out a couple of times in the past! 

The can’s and can’ts of Context and Global variables are fairly similar but there is a little more flexibility with Context Variables.  I’ll cover each in turn with code examples

Power Apps Variables

power apps variables

I’ll use the simple example of assigning information on students to variables

Context Variables

Action Example
Assign a value to a variable
UpdateContext({locStudent1:”James”})
Assign values to multiple variables with a single command
UpdateContext({locStudent1:”James”,locStudent2:”Sarah”})
Assign a record to a variable
UpdateContext({locStudent1:{name:”James”,age:21,gender:”Male”}})
Assign records to multiple variables using a single command
UpdateContext({locStudent1:{name:”James”,age:21,gender:”Male”},locStudent2:{name:”Sarah”,age:23,gender:”Female”}})
Use LookUp to assign a record to a variable
UpdateContext({locStudent1:LookUp(colRecords,name=”James”)})
Use LookUp to assign a field to a variable
UpdateContext({locStudent1:LookUp(colRecords,name=”James”).age})
Use Patch to change one or more fields in a record
UpdateContext({locStudent1:Patch(locStudent1,{age:22})})
Assign multiple records to a single variable
Cannot be done. Use a Collection or embed a Table (see below)
Embed a Table into a variable
UpdateContext({locStudents:Table({name:”James”,age:21,gender:”Male”},{name:”Sarah”,age:23,gender:”Female”})})

Global Variables

Action Example
Assign a value to a variable
Set(gblStudent,”James”)
Assign values to multiple variables with a single command
Cannot be done. Variable values must be assigned individually
Assign a record to a variable
Set(gblStudent,{name:”James”,age:21,gender:”Male”})
Assign records to multiple variables using a single command
Cannot be done. Records must be assigned individually
Use LookUp to assign a record to a variable
Set(gblStudent,LookUp(colRecords,name=”James”))
Use LookUp to assign a field to a variable
Set(gblStudent,LookUp(colRecords,name=”James”).age)
Use Patch to change one or more fields in a record
Set(gblStudent,Patch(gblStudent,{age:22}))
Assign multiple records to a single variable
Cannot be done. Use a Collection or embed a Table (see below)
Embed a Table into a variable
Set(gblStudent2,Table({name:”James”,age:21,gender:”Male”},{name:”Sarah”,age:23,gender:”Female”}))

I have no doubt I’ll be using this post in future to refresh my memory!  I hope you find it useful too

Leave a Comment

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

Scroll to Top