The Power of Power Platform: A Beginner's Guide to Using Collections & Variables

The Power of Power Platform: A Beginner's Guide to Using Collections & Variables

Over my next three blogs, I’ll be focusing on one of the essential aspects of Microsoft PowerApps: Variables and Collections. Whether you’re new to PowerApps or looking to solidify your understanding, this series will guide you through the core concepts and practical applications of these powerful tools.

Part 1 will give you an overview of variables and collections, with a focus on understanding and using variables in PowerApps.

Part 2 will explain how to use collections in PowerApps, covering the creation, manipulation, and common operations.?

Part 3 will provide a practical, step-by-step example that demonstrates how to effectively use variables and collections together in PowerApps along with some useful tips for working with variables.

Let's get started with Part 1!


Overview of Collections and Variables in PowerApps (Focus on Variables)


What Are Collections and Variables?

Variables are like small storage boxes where you can keep a single piece of information. Think of it as writing a note on a sticky pad to remember a specific value.

Collections are like larger storage containers where you can keep multiple pieces of information, kind of like a spreadsheet with rows and columns.

?

Purpose and Benefits of Using Variables and Collections

Variables and collections serve different but complementary purposes in PowerApps:

  • Variables allow you to store and manipulate single values, which can be helpful for tasks like storing user input, toggling visibility of controls, or holding a temporary calculation result.?
  • Collections enable you to manage lists of data, such as a list of products, user details, or any other set of records. Collections are particularly useful when you need to display, filter, or manipulate large sets of data in your app.?

Using variables and collections effectively can simplify your app's logic, improve performance, and make your application more dynamic and responsive.

?

Using Variables in PowerApps

Variables in PowerApps come in two flavors: global and context/local.

  1. Global Variables: These can be accessed from anywhere in your app.
  2. Local Variables: These are limited to a specific screen.

?

Creating Global Variables

To create a global variable, use the Set function. E.g.

Set(variableName, Value)        

Here's how you can do it:

  1. Insert a Button into your app. (You need a behavior formula to create variables, as they don't have an output.)

Screenshot showing the steps to insert a button in PowerApps
Screenshot showing the steps to insert a button in PowerApps


2. In the button's OnSelect property, use the Set function like this:

Screenshot showing the steps to set a global variable in PowerApps
Screenshot showing the steps to set a global variable in PowerApps

This creates a global variable named "gblQuestion" with the value "Who let the dogs out?".


3. Insert a label, and set its Text property to gblQuestion.

When you click the button, the label will display "Who let the dogs out?".

Screenshot showing the steps to display a global variable in PowerApps using a label control
Screenshot showing the steps to display a global variable in PowerApps using a label control


Changing the Value of a Global Variable

You can change the value of a global variable at any time using the same Set functions.

Example:

  1. In the OnStart Property of the app, use the Set function to initialize a global variable:

Set(gblCount, 0)        

This sets the variable gblCount to 0 when the app starts.?

Screenshot showing the steps to set a global variable in PowerApps
Screenshot showing the steps to set a global variable in PowerApps


2. Update the value of gblCount:

  • Insert a Button and modify its OnSelect property like this:

Set(gblCount, gblCount + 1)        

This line of code tells PowerApps to increase the value of gblCount by 1 every time the button is clicked.

  • Insert a Text Label and set its Text property to display the Value.

Now, each time you click the button, the number displayed in the label will increase by 1.?

a gif demonstrating the value of a Global Variable being updated
gif demonstrating the Value of a Global Variable being changed


You can store various data types in global variables:

  • Text:? Set(gblText, "Hello World")
  • Numbers:? Set(gblNumber, 49)
  • Boolean:? Set(gblBoolean, true)
  • Records: ?Set(gblUser, User())
  • Tables:? Set(gblProducts, Table({ProductName: "Laptop", Price: 100}, {ProductName: "Mouse", Price: 200}))

Remember, variable names are case-sensitive, so make sure you use the same capitalization every time you reference the variable.


Use Cases for Global Variables

Global variables have many practical applications in PowerApps. Here are some common use cases:

  1. Storing User Information: You can store the current user's details for easy access throughout your app:

Set(gblUser, User())        

Now you can use gblUser.FullName or gblUser.Email anywhere in your app.


2. Navigation: Store screen references for easy navigation:

Set(gblMyScreen, Screen2)         

Later, you can use: Navigate(gblMyScreen)



Creating Local Variables: Screen-Specific Storage

Local variables are scoped to the screen where they are created and cannot be accessed by other screens.

To create a local variable, use the UpdateContext function. E.g.

UpdateContext({ variableName: Value })        

Steps to Create a Local Variable:

  1. Create a Button Control
  2. Set the OnSelect Property: In the OnSelect property of the button, use the UpdateContext function to create a local variable.

Screenshot showing the steps to set a local variable in PowerApps
Screenshot showing the steps to set a local variable in PowerApps


3. Display the Variable in a Text Label:

Screenshot showing the steps to display a local variable in PowerApps using a label control
Screenshot showing the steps to display a local variable in PowerApps using a label control

This creates a context variable named "locTest1" with the value "Hello, PowerApps" which can only be used by that particular screen.


Creating multiple local variables at once:

  1. In the OnSelect property of the button, use the UpdateContext function to create the local variables.?

Screenshot showing the steps to set multiple local variables in PowerApps
Screenshot showing the steps to set multiple local variables in PowerApps


2. Insert text labels and set each label's Text property to the corresponding variable.

Screenshot showing the steps to display local variables in PowerApps
Screenshot showing the steps to display local variables in PowerApps

This creates or updates two context variables in one go.

?

Changing the Value of a Local Variable

  1. Insert a new Button control.
  2. Set the OnSelect Property:

Using the same variable name (locTest1), set the new buttons OnSelect property to:

UpdateContext({locTest1: "Hello, This is the new me"})        

This code will change the value of the local variable "locTest1" when the button is clicked.


3. Verify the Change:

  • Ensure the Text property of your existing label is still set to "locTest1". Click the new button you just added:

The text label will update to display: "Hello, This is the new me."

  • Now, click the original button and the text label updates its display to the original value.

gif demonstrating the Value of a Local Variable being changed
gif demonstrating the Value of a Local Variable being changed


Advantages of Using Local Variables Over Global Variables

  1. Better Control and Organization

Local variables are only accessible within the screen or section of your app where they’re created. This makes it easier to keep track of what each variable does because you know exactly where it’s being used. You don’t have to worry about accidentally changing or misusing the variable in another part of your app, which can happen with global variables.

Example: Imagine you’re working on a to-do list app with multiple screens. If you use a local variable to track whether a task is completed, you only need to think about that variable on the screen where you’re checking off tasks. The rest of the app remains unaffected.

?

2. Easier to Debug and Maintain

When something goes wrong in your app, it’s easier to find and fix the problem if you’re using local variables. Since local variables are confined to specific parts of your app, you don’t have to search through the entire app to figure out where something went wrong.?

Example: If a value isn’t updating correctly on a screen, you only need to check the local variables on that screen rather than sifting through every global variable in your app.

?

3. Reduced Risk of Unintentional Changes

Global variables can be changed from anywhere in your app, which means it’s easier to accidentally overwrite or change their values in ways you didn’t intend. This can lead to bugs that are hard to track down. With local variables, the scope is limited, so the chances of accidental changes are much lower.

Example: If you have a global variable tracking the user’s name and it gets changed on a different screen by mistake, it could cause confusion. A local variable used only on the necessary screen avoids this risk.

?

4. Improved Performance

Using local variables can improve your app’s performance. Global variables take up memory throughout the entire app, while local variables only use memory in the specific parts where they are needed. This means your app can run faster and more efficiently by not holding on to unnecessary data.

Example: If you have a large app with many screens and use global variables for everything, your app might slow down. By using local variables where appropriate, you free up resources, helping your app run more smoothly.

?

Conclusion

In this first part of our series, we've covered the fundamentals of variables in PowerApps, including how to create and use both global and local variables. Understanding the difference between these two types of variables and knowing when to use each is essential for building efficient and organized PowerApps applications.

In the next part of the series, we'll dive into collections, another powerful feature in PowerApps that allows you to manage more complex data. Stay tuned, and happy PowerApp-ing!

?

Interested to learn more? Need help? Need to talk this through more? We understand and are here to help. Visit us at?www.consultntctech.com?or reach out to us at hello@consultntctech.com and we will happily answer your questions. Simply reach out for a FREE consultation!

Click here to read Part 2.

要查看或添加评论,请登录

Segu Essandoh的更多文章

社区洞察

其他会员也浏览了