Exercise - Dataverse choice columns

Completed

In this exercise, you'll be helping Contoso add choice columns to an existing Dataverse table. Then, you'll modify their Product Management app to use the new columns.

Objectives

The goal for this exercise is for you to learn how to:

  • Create choice columns.

  • Use filter data in the app by using choice columns.

  • Use the Patch() function to update column values.

Prerequisite

To complete this exercise, you'll need an environment with Microsoft Dataverse.

Exercise 1: Import a solution

In this exercise, you'll import a solution into your environment. This solution contains a product table and an application for product management.

Task 1: Import the starting solution

In this task, you'll import a solution into your environment.

  1. You'll need to download the zip file to complete this exercise. Select download once the link opens.

  2. Go to the Power Apps maker portal and select the environment you would like to use for this lab.

  3. Select Solutions > Import solution.

    Screenshot of the Import solution button in Power Apps.

  4. Select Browse.

  5. Find/Select the ContosoProductChoiceLab_1_0_0_0.zip solution, and select Open.

  6. Select Next.

  7. Select Import and then wait for the solution importing process to complete.

Task 2: Run the application

In this task, you'll run the Contoso Product Manager application and add product rows.

  1. Open the solution Contoso Product Choice Lab and select the Contoso Product Manager app. (Open it in play mode.)

  2. From BrowseScreen1 select the Create new item button (the plus + icon).

    Screenshot of the Create new product button.

  3. Enter Measuring Gadget for the Name and then select Save.

    Screenshot of the Save new product button in Power Apps.

  4. Add a few more product rows.

  5. You should now have at least three products.

    Screenshot of the product rows in Power Apps.

  6. Close the application browser window or tab.

Exercise 2: Create columns

In this exercise, you'll create two columns of data type choice: one for Product visibility and one for Sales channels.

Note

Pay attention to which column is of data type Choice and which column is of data type Choices.

Task: Create choice columns

In this task, you'll create two columns of data type Choice.

  1. Go to the Power Apps maker portal and select Solutions.

  2. Select the Contoso Choice Lab solution that you imported.

  3. Select the Product table.

  4. In the Schema pane, select Columns.

    Screenshot of the Product table dashboard with Columns selected from under Schema.

  5. In the Columns view select + New column.

    Screenshot of the Columns view and the Add new column button.

  6. In the New column pane on the right of your screen, enter Product visibility for the Display name, select Choice under Data type, and then select Choice from the dropdown.

    Screenshot of the Choice column data type in Power Apps.

  7. Under Sync this choice with, select + New choice.

  8. In the New choice pane on the right side of your screen, enter chProductVisibility in the Display name field. Adding the "ch" in front of the name will make it much easier to determine that this is a Choice set.

  9. Enter Private for the first item under Choices and then select + New Choice.

    Screenshot of the Add new choice in the New choice pane.

  10. Enter Invite for the second choice and then select + New choice.

  11. Enter Public and then select Save.

    Screenshot of the Choice items in Power Apps.

  12. Under Sync this choice with enter chProductVisibility in the search field to find/select the choice set you created. chProductVisibility should now be showing in this field.

  13. Select Save and the New column pane disappears.

  14. Select + New column again.

  15. Enter Sales channels for Display name, select Choice for Data type, and then select Choice from the dropdown.

  16. Select the box next to Selecting multiple choices is allowed. This is the way that our column will allow multiple choices (don't forget this step).

    Screenshot of the Choices column data type in Power Apps.

  17. Under Sync this choice with select + New choice.

  18. Enter chSalesChannels for the Display name and Wholesale for the first item and then select + New choice.

  19. Enter Retail for the second item and then select + New choice.

  20. Enter Direct and then select Save.

    Screenshot of the Save button in the new Sales channels choices column.

  21. Under Sync this choice with enter chSalesChannels in the search field to find/select the choice set you created. chSalesChannels should now be showing in this field.

  22. Select Save.

In this exercise, you'll modify the Edit form and the gallery of the Contoso Product Manager application.

Task: Edit application

In this task, you'll edit the Contoso Product Manager application.

  1. If you've navigated away, find your solutions, and select to open the Contoso Product Choice Lab solution. If you're still in the solution, in the Objects navigation pane, find/select Apps

  2. Select to open the Contoso Product Manager application.

    Screenshot of Solutions > Contoso Product Choice Lab section, showing the Contoso Product Manager application highlighted.

    The application will open in the app studio.

  3. From the Tree view tab, expand EditScreen1, and then select EditForm1.

    Screenshot of the EditForm1 control highlighted on the Tree view tab in Power Apps.

  4. Select Edit fields.

    Screenshot of the Edit fields option on the Properties tab of EditForm1.

  5. Select the More actions ellipsis (...) button of the Created On column and then select Remove.

    Screenshot of the Remove button selected from the Created On column dropdown list.

  6. Select + Add field.

    Screenshot of the Add field button in the Fields pane of Power Apps.

  7. Select the Product visibility and Sales channel columns and then select Add.

    Screenshot of the Choose a field section, with columns selected and the Add button.

  8. Close the Fields pane.

  9. Expand BrowseScreen1 and select BrowseGallery1.

    Screenshot of the BrowseGallery1 control highlighted in the Tree view.

  10. Select the Edit icon in the gallery.

    Screenshot of the Edit icon in the gallery.

  11. Change the TemplateSize to 230.

    Screenshot of the TemplateSize changed to 230.

  12. Select the Title label inside the gallery and move it to the top of the template.

    Screenshot of the label control position highlighted in Power Apps.

  13. While still editing the gallery, select Insert and expand Input, then select Drop down.

    Screenshot of selecting Insert > Input > Drop down in Power Apps.

  14. Resize and reposition the dropdown control and change the Items value of the dropdown list to the following formula.

    Choices(Products.'Product visibility')

    Screenshot of the Items formula value in Power Apps.

  15. Set the Default value of the dropdown control to the following formula.

    ThisItem.'Product visibility'

  16. While still editing the gallery, go to the Insert tab and select Text label.

  17. Rename the label Channels.

  18. Move the Channels label below the dropdown control and then resize it.

  19. Set the Text value of the Channels label that you added to the following formula. This formula will concatenate the selected options.

    Concat(ThisItem.'Sales channels',Text(Value),",")

  20. Make sure that you're still editing the gallery. Select + Insert > Rectangle.

    Screenshot of Insert selected and the Rectangle option highlighted.

  21. Set the X and Y values of the rectangle to 0.

  22. Set the Width of the rectangle to 6.

  23. Set the Height of the rectangle to 220.

  24. Set the Fill value of the rectangle to the following formula. This formula will give a different fill color for each choice of the Product visibility column value.

    Switch(ThisItem.'Product visibility', chProductVisibility.Private, Color.Red, chProductVisibility.Public, Color.Green, chProductVisibility.Invite, Color.Blue, Color.Black)

  25. Select the Gallery and then delete the OnSelect value, so that the fx formula field is blank.

    Screenshot of the OnSelect value showing as empty.

  26. Select NextArrow1 inside the gallery.

    Screenshot of the NextArrow2 control in the gallery.

  27. Set the OnSelect value of NextArrow1 to the following formula.

    Navigate(DetailScreen1, ScreenTransition.None)

  28. In the upper right corner of the screen, select Save.

    Screenshot of the Save button in the Contoso Product Manager application.

  29. Select Preview the app (or F5).

    Screenshot of the Preview the application button in Power Apps.

    The application should load.

  30. Select the Next arrow for one of the products.

  31. Select the Edit button.

  32. For Sales channels select Retail and Direct. Then for Product visibility select Private, and then select Save.

    Screenshot of the Save button selected in Products to save the entered information.

  33. Select the back button.

  34. Select to open another product.

  35. Select Edit.

  36. For Sales channels select Retail and for Product visibility select Public, and then select Save.

  37. Select the back button.

  38. Select to open another product.

  39. Select Edit.

  40. For Sales channels select Direct, and select Invite for Product visibility, and then select Save.

  41. Select the back button.

  42. The gallery should look like the following image. When you're done viewing the results, close the preview.

    Screenshot of the edited gallery, with the Close application preview button highlighted.

  43. Don't navigate away from this page.

Exercise 4: Use filters

In this exercise, you'll filter the gallery rows by product visibility.

Task: Use a filter

In this task, you'll filter the gallery rows by product visibility.

  1. On BrowseScreen1, select TextSearchBox1.

    Screenshot of the TextSearchBox1 control in Tree view in Power Apps.

  2. Change the Width of the search box to the following formula.

    Parent.Width/2

  3. From the ribbon, select Insert and expand Input, then select Drop down.

    Screenshot of the Insert dropdown control, showing Input selected with the Drop down option highlighted.

  4. Rename the dropdown item to Visibility filter.

    Screenshot of the Rename control in Power Apps.

  5. Resize and reposition the Visibility filter and then place it to the right of the search box.

    Screenshot of the control position next to search in Power Apps.

  6. Set the Items value of the Visibility filter to the following formula.

    Choices(Products.'Product visibility')

  7. Set the AllowEmptySelection value of the Visibility filter to true.

  8. Set the Default value of the Visibility filter to the following formula.

    Blank()

  9. From the ribbon, select Insert and expand Icons, and then select Cancel.

  10. Resize and reposition the icon and then place it to the right of the dropdown arrow.

    Screenshot of the control position of the icon in Power Apps.

  11. Set the OnSelect value of the icon to the following formula.

    Reset('Visibility filter')

  12. Set the Visible value of the icon to the following formula.

    If(IsBlank('Visibility filter'), false,true)

  13. Select BrowseGallery1 and then change the Items value to the following formula. This formula changes the existing formula to include a Filter() function that uses the user-selected values.

    SortByColumns(
        Search(
            Filter([@Products],IsBlank('Visibility filter'.Selected) || 'Product visibility' = 'Visibility filter'.Selected.Value),
            TextSearchBox1.Text,
            "contoso_name"
        ),
        "contoso_name",
        If(
            SortDescending1,
            SortOrder.Descending,
            SortOrder.Ascending
        )
    )
    

    Screenshot of the gallery OnSelect formula in the Items field.

  14. Select Preview the app (or press F5).

  15. Change the Visibility filter to Private. The gallery should show only the private products.

    Screenshot of the products filtered to Private in Power Apps.

  16. Select the X icon. The gallery should show all products.

    Screenshot of the gallery showing all products.

  17. Close the preview.

  18. Don't navigate away from this page.

Exercise 5: Use the Patch formula

In this exercise, you'll use the Patch formula to update product table rows.

Task: Use the Patch() formula

  1. Expand BrowseGallery1 and select the Dropdown1 control.

    Screenshot of the Dropdown1 control in the BrowseGallery1 menu.

  2. Set the OnChange value of the dropdown control to the following formula. This formula will update the row when the value of the dropdown control changes.

    Patch(Products,ThisItem,{'Product visibility':Self.Selected.Value})

  3. Select the gallery and then select the Edit icon.

    Screenshot of the Edit button in the Power Apps gallery.

  4. From the ribbon, select Insert and expand Icons, and then select Cancel.

  5. Place the icon in the lower right.

    Screenshot of the icon placement in the lower right.

  6. Set the Visible value of the icon to the following formula.

    If(IsBlank(Channels.Text),false,true)

  7. Set the OnSelect value of the icon to the following formula. This formula will remove all selected sales channels when the icon is selected.

    Patch(Products,ThisItem,{'Sales channels':Blank()})

  8. From the ribbon, select Settings.

  9. Select Upcoming features > Preview tab and then turn on the Formula-level error management toggle.

    Screenshot of the Formula-level error management option turned On.

  10. Close the Settings window.

  11. Select Preview the app.

  12. Change the product visibility of one of the products. The product should update.

    Screenshot of the updated row in Power Apps.

  13. Select the X icon of the first product. The sales channels of the product should be cleared, and the X button should become hidden.

    Screenshot of the updated row in Power Apps, showing a blank spot.

  14. Close the preview.

  15. Select Save.