Formulas and functionality

Completed

When using formulas, you have the ability to change the way controls respond and function. For example, in certain scenarios, you can hide a control on the screen until another action has taken place. This is done by writing a simple formula to update the Visible property of the control. Or maybe instead of hiding the control, you set the DisplayMode property to View which removes the ability to edit that control. There are many more scenarios where you can implement this type of formula in a canvas app. By using formulas in your canvas app to modify the DisplayMode and or Visible properties of a control, you can develop a more user-friendly app.

Let's take a look at how you can apply this formula to show and hide a button in a gallery based on the information in our data source. In this example, we create a collection called "TestScoresCollection". To follow along, you can open an already existing canvas app and create a new screen.

  1. Insert a Button control to your screen.

  2. Set the OnSelect property for the button to:

    ClearCollect(TestScoresCollection,{Name:"Student 1", TestScore:"B"},{Name:"Student 2", TestScore:"C"},{Name:"Student 3",TestScore:"A"},{Name:"Student 4", TestScore:"C"},{Name:"Student 5", TestScore:"A"})
    
  3. Hold the Alt key on your keyboard (or put the app in preview mode) and select the button to generate the TestScoresCollection.

  4. Insert a vertical gallery control into your screen.

  5. Select TestScoresCollection as the data source, and your gallery should resemble the following.

    Screenshot of gallery test scores information.

  6. Select the first row of the gallery and insert a new button into the gallery itself.

  7. Change the text property of the button to "Retake Test".

  8. With the button still selected, go to the Visible property and enter the following code, so that this button will only be visible for student grades not equal to "A" or "B".

    If(ThisItem.TestScore = "A" Or ThisItem.TestScore = "B",false,true)
    

    Screenshot of retake test button to show only with grades not equal to A or B.

Though we used this formula for the View property of a button, you could apply logic to this to affect the display mode of the button. In this case, we didn't add any code to our "Retake test" button, but you can get the idea that we're controlling app behavior adjusting how controls respond. Our formula evaluates to true or false, which is all that Power Apps is looking for in the visible property. We can even shorten this code because it's a true/false answer, but we'll cover that in the next unit.