Skip to main content

Accessing a Group Inside of a Collection

Sometimes you will want to group your data by a specific attribute and show that data in PowerApps. One way to do this is by creating two separate tables and have them grouped before PowerApps, but another way is by grouping a collection inside PowerApps.

In the example below, I will be grouping NFL players into their teams so I can create a gallery within a gallery to scroll and see the list of player names.

First, you want to group your data. When you create a group, you’re creating a parent for the records in your data source. I created a button to do this:

ClearCollect(PlayersbyTeam,GroupBy(Table1,”Team”,”Players”))

The syntax for using the function GroupBy is:

GroupByTableColumnName1 [, ColumnName2, … ], GroupColumnName )

You can GroupBy multiple columns, but for this example I will be just grouping by teams.

Below is how your collection will look like when you’re grouping:

[divider line_type=”No Line” custom_height=”20″][image_with_animation image_url=”377″ alignment=”center” animation=”Fade In” border_radius=”none” box_shadow=”none” max_width=”100%”]

If you click the squares that are under the “Players” column, you’ll see a whole new table with three columns. From the way I see it, “Players” is a collection inside of the “PlayersbyTeam” collection.

[divider line_type=”No Line” custom_height=”20″][image_with_animation image_url=”375″ alignment=”center” animation=”Fade In” border_radius=”none” box_shadow=”none” max_width=”100%”][divider line_type=”No Line” custom_height=”20″][image_with_animation image_url=”374″ alignment=”center” animation=”Fade In” border_radius=”none” box_shadow=”none” max_width=”100%”]

Then, you create a gallery for your collection.

[divider line_type=”No Line” custom_height=”20″][image_with_animation image_url=”373″ alignment=”center” animation=”Fade In” border_radius=”none” box_shadow=”none” max_width=”100%”]

Add a label for your groups. Here, “Team” is column name for the groups in the collection.

[divider line_type=”No Line” custom_height=”20″][image_with_animation image_url=”372″ alignment=”center” animation=”Fade In” border_radius=”none” box_shadow=”none” max_width=”100%”]

Insert another gallery into the gallery, and use a filter on that gallery to filter for the teams.

[divider line_type=”No Line” custom_height=”20″]

Filter(Players,ThisItem.Team = Team)

[divider line_type=”No Line” custom_height=”20″][image_with_animation image_url=”371″ alignment=”center” animation=”Fade In” border_radius=”none” box_shadow=”none” max_width=”100%”]

Insert labels into the gallery you just created, and change the label text to the column that is in the group.

[divider line_type=”No Line” custom_height=”20″][image_with_animation image_url=”370″ alignment=”center” animation=”Fade In” border_radius=”none” box_shadow=”none” max_width=”100%”]

You can use CountRows and Filter functions on your data:

CountRows(Filter(Players,Pos = “QB”))

The only filter you need to do is on the positions because the function will already know to pull from that specific team. As you can see, all teams have 75 players and 4 quarterbacks, but the amount of centers (C), offensive tackles (OT), and offensive guards (OG) are different.

And it will come out like this:

[divider line_type=”No Line” custom_height=”20″][image_with_animation image_url=”369″ alignment=”center” animation=”Fade In” border_radius=”none” box_shadow=”none” max_width=”100%”]