Hi folks, in this short post I will try to explain how the “If” function work through examples.
The If function in Power Apps is used to perform conditional operations depending on given criteria.
The If function allows you to evaluate a condition and do different actions depending on whether the condition is true or false. The syntax for the If function is as follows:
If(Condition, Value_if_true, Value_if_false)
Condition | This is the expression that you want to evaluate |
Value_if_true | This is the value that is returned if the condition is true |
Value_if_false | This is the value that is returned if the condition is false |
Practice Example 1.
The first example checks the “TextInput” control value and if the value is <=0 then it shows the message “The value cannot be 0” and the fill color is red. In other case, the message is “The value is correct” and the color is Aqua.
Action | Function | Screenshot |
Text | If(Value(TextInput5.Text) <=0, “The value cannot be 0”, “The value is correct “) | |
Fill | If(Value(TextInput5.Text) <=0,Color.Red, Color.Aqua ) |
Practice Example 2.
On the 2nd example I will show you how to activate a button and popup a message using the ‘If’ function.
If the value of the “dd_status” drop-down control is equal to the string “Disable”, then the button is not active, if the value is “Enable”, then the button is active.
If(dd_status.SelectedText.Value = "Disable",DisplayMode.Disabled)
In this example, I’m storing a Boolean value in the “VarDialogBox” variable. A dialog box will be displayed after pressing the button “Button” with the help of this variable.
UpdateContext({VarDialogBox: true})
The expression “UpdateContext({VarDialogBox: true})” would then update the value of the variable “VarDialogBox” to “true” using the “UpdateContext” function.
Practice Example 3.
The third and last example is more complicated than the two others where it can be used in a power app with offline capabilities. When the Internet connection is active, the data is sent to an online destination, otherwise it stores it in a local collection.
You can use the If function in Power FX to check for example the Azure SQL Database connection is active. If the connection is available, you can update the table through the Patch function. If the connection is not available, you can use the Collect function to add data to a local collection.
If(Connection.Connected, // Check if a connection to the data source is available Patch( 'Application.People', // Patch the 'People' entity in the 'Application' data source First( // Select the first (and only) record that matches the following condition Filter( 'Application.People', // Filter the 'People' entity in the 'Application' data source PersonID = Gallery1.Selected.PersonID // Select the record that matches the PersonID of the currently selected item in Gallery1 ) ), { FullName: TextInputeFullNameValue.Text, LogonName: TextInputLogonNameValue.Text, PhoneNumber: TextInputPhoneNumberValue.Text } ), // If a connection is not available, add the new record to a collection using Collect() Collect( colAccounts, { FullName: TextInputeFullNameValue.Text, LogonName: TextInputLogonNameValue.Text, PhoneNumber: TextInputPhoneNumberValue.Text } ));