Switch

How to Use the Power Fx Switch  Function in Power Apps

What is the Power Fx Switch function?

The Power Fx Switch function is a conditional statement that allows you to check for specific conditions and perform different actions based on the result. It is similar to an “if-else” statement in traditional programming languages. The syntax for the Power Fx Switch function is as follows:


Switch( expression, value1, result1, value2, result2, …, default )


The expression is the value that you want to evaluate, and the values and results are the different conditions and actions that you want to perform based on the expression. The default value is optional and is used when none of the other conditions are met.

Using the Power Fx Switch function in Power Apps

To use the Power Fx Switch function in Power Apps, you will need to create a new formula within your application. You can do this by selecting the "Insert" tab in the Power Apps Studio and clicking on the "Formula" option. Once you have created a new formula, you can start using the Power Fx Switch function.

Example 1: Displaying a message based on a condition

Let's say that you want to display a message to the user based on a particular condition. You can use the Power Fx Switch function to achieve this. For example, if the value of a particular field is "Yes", you want to display the message "This is a valid entry", and if the value is "No", you want to display the message "Invalid entry".

Here is an example of how the formula would look:


Switch( DataCardValue1.Text, “Yes”, “This is a valid entry”, “No”, “Invalid entry” )


In this formula, "DataCardValue1.Text" is the expression that we want to evaluate. If the value of the field is "Yes", the result will be "This is a valid entry". If the value is "No", the result will be "Invalid entry".

Example 2: Changing the color of a field based on a condition

You can also use the Power Fx Switch function to change the color of a field based on a particular condition. For example, if the value of a field is "Yes", you want the field to be green, and if the value is "No", you want the field to be red.

Here is an example of how the formula would look:


Switch( DataCardValue1.Text, “Yes”, Color.Green, “No”, Color.Red )


In this formula, "DataCardValue1.Text" is the expression that we want to evaluate. If the value of the field is "Yes", the result will be "Color.Green", which will change the color of the field to green. If the value is "No", the result will be "Color.Red", which will change the color of the field to red.

The Power Fx Switch function is a powerful tool that can be used to create dynamic and responsive applications within Power Apps. By using this function, you can create conditional statements that perform different actions based on specific conditions. Whether you want to display messages, change the color of fields, or perform other actions, the Power Fx Switch function is an essential tool for any Power Apps developer.

Power Apps Training Courses by G Com Solutions (0800 998 9248)