How to use Count/ Sum using GroupBy in Power Apps Canvas Apps

I am back with another blog on Power Apps and in this blog I will discuss on how to use SUM or COUNT using GroupBy function in Power Apps.

While this seem to be pretty easy in different programming languages, it can come across bit tricky while trying to do the same in Power Apps.

Here is a simple collection on which I need to do a Count aggregate using GroupBy

Collect(StudentAndCourses,
    {Course: "Maths", Student: "John"},
    {Course: "Chemistry", Student: "Jack"},
    {Course: "Maths", Student: "Jack"},
    {Course: "Physics", Student: "John"},
    {Course: "Chemistry", Student: "Julie"}
)

I want to get the count of students by Course. But how to do it? Below is precisely the output I need.

Use Aggregate functions using GroupBy in Power Apps canvas apps
Above is a simple table and I set the Items property of the table to the below formula.
AddColumns(GroupBy(StudentAndCourses, "Course", "CourseGroup"),"StudentCount", CountRows(CourseGroup))

Let’s divide the formula.

GroupBy(StudentAndCourses, “Course”, “CourseGroup”) – This formula groups the actual data source StudentsAndCourses grouped by Course field into a table – CourseGroup.

Then I use the AddColumns and add a custom field StudentCount. The value of this field is CountRows(CourseGroup). Remember CourseGroup is the table formed by using the GroupBy function.

And then you get the desired output.

Hope this helped!

You might also like the below posts.

Debajit Dutta
Business Solutions MVP