Skip to content
Home » GroupBy function in Power Apps

GroupBy function in Power Apps

The GroupBy function in Power Apps is used to group records from a table based on one or more columns. It returns a table where records are grouped together, and each group can be further processed. It’s useful when you need to summarize or categorize data within your app.




Syntax:

GroupBy(Table, ColumnName1, ColumnName2, ..., GroupColumnName)
  • Table: Require. The table that contains the data to group.
  • ColumnName1, ColumnName2: Required. The columns by which you want to group the data.
  • GroupColumnName: Required. The name of the new column that will hold the grouped records.

Example of GroupBy function

Assume, you have a collection called colRegionalSales with the following columns: TranId, Region, ExecutiveId and Sales.

Now, you want to group the data by Region and create a new grouped column called GroupedSalesByRegion and store that data into new collection called colGroupedData.

Let’s add a button control, go to OnSelect property of button and write below formula.

ClearCollect(
    colGroupedData,
    GroupBy(
        colRegionalSales,
        Region,
        SalesBy
    )
)
Let’s understand the working of above formula.
  • The formula groups all the records in the colRegionalSales collection based on the values in the Region column.
  • The grouped data is then stored in a new column called SalesBy.
  • After the grouping is done, the ClearCollect function clears any previous data from colGroupedData and stores the newly grouped data into the collection.

You can see, the formula groups the records based on the Region column and stores them in the new collection, colGroupedData.

Each SalesBy column contains a group of records for each respective Region. To see the records click on table icon, let’s click on North region.

Showing group records in a gallery

You can access the individual records within the nested tables created by the GroupBy function. In the formula above, GroupBy created a column named SalesBy, which holds a group of records for each corresponding region. These grouped records can be displayed in a gallery.




Let’s add a Gallery control. Set the Items property of the gallery to the grouped collection, colGroupedData, to display the group information. Within each group, you can then display the detailed records

You can see, Gallery displays the lists of Regions.

Now, to display the records for each group, Inside the gallery, add another nested gallery to display the records in the SalesBy column.

You can set the Items property of the nested gallery to the following formula.

ThisItem.SalesBy
Now, you can display the each records information, set the lables Text property for each record.
ThisItem.Executive
Similarly, you can display the Sales amount as well. You can also customize the gallery by adjusting the border, font size and color, background color and other properties to make it look more interactive.
Now, you can see that it displays the grouped data under the each region.
Also Read..

Loading

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from SQL BI Tutorials

Subscribe now to keep reading and get access to the full archive.

Continue reading