Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 38 ยท Controls and Formulas

Variables and Collections in Power Apps

Understand global variables, context variables, collections and named formulas in Power Apps, when to use each, and how Set, UpdateContext, Collect and ClearCollect work.

Because Power Fx is declarative, most values are computed from controls and data. Sometimes you need to remember something: the signed-in user's role, the selected item, a shopping basket. Power Apps offers four ways to hold state. Choosing the right one keeps your app simple.

The four kinds of state

Kind Scope Created with Typical use
Global variable whole app Set(varName, value) Selected record, flags, user role
Context variable one screen UpdateContext({varName: value}) A pop-up open/closed on one screen
Collection whole app Collect, ClearCollect A local table: basket, cache, edit buffer
Named formula whole app App.Formulas Derived values that always stay current

Variables are typed by first use: the first value you assign decides the type, and later assignments must match.

Global variables

TEXT
Set(varIsAdmin, true);
Set(varSelectedTask, galTasks.Selected);
Set(varCount, varCount + 1)

Read them by name anywhere: Visible: varIsAdmin. Variables are created at first Set, so a variable used before it is set is blank. Initialize important ones in App.OnStart or, better, use a named formula when the value is derived.

Context variables

TEXT
UpdateContext({locShowPopup: true, locMessage: "Saved!"});

Context variables live on the screen where they were created. Use a prefix such as loc to tell them apart from globals. Pass context along with Navigate(scr, transition, {locMode: "edit"}) as shown in Screens and navigation.

Collections

A collection is a table stored in memory. Functions:

TEXT
ClearCollect(colBasket, {Item: "Pen", Qty: 2});   // reset and fill
Collect(colBasket, {Item: "Ink", Qty: 1});        // append
Remove(colBasket, First(colBasket));              // delete a record
Clear(colBasket);                                  // delete all
UpdateIf(colBasket, Item = "Pen", {Qty: Qty + 1}); // edit rows matching

Collections are handy for a basket that is saved later in one batch, for caching a lookup table, and for an offline buffer. They live only while the app is open (or until you use SaveData/LoadData, which have their own limits).

The stand-in below models a basket as an array and runs the same operations.

JS Stand-in: a shopping basket collection

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Scope and lifetime

  • Globals live until the app closes. Contexts live while you stay on the screen (they are reset when the app restarts).
  • Variables cannot be seen by other users or devices. They are not saved to the data source.
  • Changing a variable triggers recalculation of every formula that reads it.

The next stand-in demonstrates the type-by-first-use rule and the idea that Set is a behavior (it can only occur in behavior properties such as OnSelect).

JS Stand-in: variables typed by first assignment

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Which should I use?

  1. Can the value be computed from other things? Use a named formula or an inline expression.
  2. Is it needed on one screen only? Use a context variable.
  3. Is it needed on several screens, or is it user state (selected record)? Use a global variable.
  4. Is it a list of records you edit locally? Use a collection.

Avoid keeping a copy of data source rows in a collection unless you have a reason (caching for speed, offline). Duplicated data gets stale, and large collections slow the app.

With and local scope

With(record, formula) creates a local name for the formula's duration:

TEXT
With({subtotal: Sum(colBasket, Qty * Price)}, subtotal * 1.2)

This is not a variable, just a convenient shorthand.

Common mistakes

  • Setting variables in Items or Text properties. Set only works in behavior properties.
  • Overusing OnStart to load everything; users wait before seeing anything.
  • Using a collection to mirror a table and then forgetting to refresh it.
  • Name clashes: a variable named like a control or column confuses readers. Use prefixes var, loc, col.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which function creates a global variable?

    Set(name, value) creates or updates a global variable.
  2. 2. A value is needed only on one screen. Which is the best fit?

    UpdateContext variables are scoped to the screen.
  3. 3. What does ClearCollect do?

    ClearCollect resets the collection and loads the given rows.
  4. 4. Where can Set be used?

    Set has side effects, so it only works in behavior formulas.

Summary

  • Four kinds of state: global variables, context variables, collections and named formulas.
  • Prefer named formulas for derived values; keep state small.
  • Collections are in-memory tables managed with Collect, ClearCollect, Remove and UpdateIf.
  • Variables are typed by first use and live only in the running app.

Next: Common controls