Use the Patch function to update a Gallery

Completed

This hands-on lab shows you how to use the Patch function in a gallery.

  1. Sign in to Power Apps.

  2. From the Home screen select + Create from the left-hand navigation panel, then select Blank app and then Create under Blank canvas app.

  3. Name your app "Patch Exercise" or another appropriate title of your choice and select Create.

  4. Select the Insert button and add a Button control, and set its OnSelect property to this formula:

    ClearCollect(CityPopulations,
    {City:"London", Country:"United Kingdom", Population:8615000},
    {City:"Berlin", Country:"Germany", Population:3562000},
    {City:"Madrid", Country:"Spain", Population:3165000},
    {City:"Rome", Country:"Italy", Population:2874000},
    {City:"Paris", Country:"France", Population:2273000},
    {City:"Hamburg", Country:"Germany", Population:1760000},
    {City:"Barcelona", Country:"Spain", Population:1602000},
    {City:"Munich", Country:"Germany", Population:1494000},
    {City:"Milan", Country:"Italy", Population:1344000})
    
  5. Set the button's Text property to "Collect". Then press and hold Alt Key, and select the button. (This action creates the CityPopulations collection and stores the data.)

  6. Insert a Vertical gallery control and choose CityPopulations from the data source.

  7. With the gallery selected, in the right side Properties panel, change the layout from blank to Title, subtitle, and body.

  8. Also in the Properties panel, select Fields and update the Body field in the data to display Population.

  9. Insert a Text input control, from the Properties panel, delete "text input" from the Default property, rename the text-input to tiCountry and set its Hint Text property to "Country".

  10. Again, insert a Text input control, from the Properties panel, delete "text input" from the Default property, rename the text-input to tiCity and set its Hint Text property to "City".

  11. Repeat the previous step with a third Text input, naming it tiPopulation and set the Hint Text property to "Population".

  12. Select the Insert tab add a button control, set its Text property to "Patch Country" and set its OnSelect property to this formula:

    Patch(CityPopulations,Defaults(CityPopulations),{Country:tiCountry.Text,City:tiCity.Text,Population:Value(tiPopulation.Text)})
    
  13. Align your controls like the image below:

    Screenshot of Patch Exercise controls after alignment.

  14. Now let's add more Countries to our Gallery. Put the app in Preview mode.

  15. In the Country text input, enter USA. In the City text input, enter Orlando. In the Population text input, enter 280832.

  16. Now press your Patch Country button and scroll down to the bottom of your gallery; you should see the new record you just added by using the Patch function formula from the OnSelect property of the button.

Congratulations, you used the Patch function to add new records to a Gallery!