Data - How to group data in a gallery and calculate sums

Calculating and displaying the sums of values in galleries can help users better understand the data and make better-informed decisions. The problem is that the formula to carry out this task may not be obvious. Therefore, this post describes a method to display data in a gallery and to show the sums of values.

Example Data Structure

To demonstrate, let's take the following two tables which provide a typical example of a parent-child data structure. A 'Customer' table stores customer details and a region. The 'Orders' table displays the orders that are related to each customer.



In the remainder of this post, we'll walk through how to display this data in a gallery control and how to show the sum of the order prices by customer.

Showing parent records in a gallery

To begin, we'll add a gallery control to display the customer details. To do this, we add a gallery control and set the Items property to the customer table, as shown in the screenshot below.



To display the related order records for each customer, we add a nested gallery inside the customer gallery. In this example, the name of the gallery is galOrders.

To do this, we set the Items property of the gallery to the following formula. This filters the gallery to return Orders where the CustomerID of the Customer record matches the CustomerID in the Orders table.

Filter(Orders, ThisItem.CustomerID=CustomerID)



From the nested galOrders gallery, we can add a label and display the Price field. As the screenshot beneath highlights, we can format the price value with a currency symbol and to display 2 decimal places with the following syntax:

Text(ThisItem.Price, "£0.00")

How to show a subtotal of gallery values

To display the sum of the order prices grouped by customer, we can add a label to the parent galCustomers gallery. We can then use the following syntax to calculate the sum.

Sum(galOrders.AllItems, Price)
With this formula, we call the Sum function against the datasource galOrders.AllItems. For each row in galCustomers, galOrders.AllItems returns the related order records since galOrders is filtered by CustomerID.

We can further adapt the formula to format the sum as a currency value like so:
Text(
Sum(galOrders.AllItems, Price),
"£0.00"
)
The screenshot below shows the final result.
.

Conclusion

A common requirement is to show a group of records in a gallery and to display a sub-total of values. This post walked through a technique to carry out this task.

Related posts

FormuIas - Is it possible to call a user-defined function recursively in Power Apps?
January 31, 2024
Formulas - A beginners guide on how to create and call user-defined functions (UDFs)
January 28, 2024
Formula - How to add a button that converts degrees Centigrade to Fahrenheit and vice versa
May 08, 2023
Formula - How to convert a single delimited string to rows and columns
April 05, 2023
Formula - How to calculate compound interest
November 24, 2022
Utilities - The best way to peform OCR on images of Power Apps Formulas
October 11, 2022
Example - How to use a drop down control to convert currencies
September 27, 2022
Formula - How to parse JSON in Power Apps- 4 examples
September 15, 2022
Data - How to get a row by ordinal number
April 28, 2022
Formula - What to do when the If statement doesn't work?
December 17, 2021
Formula - Boolean And / Or operators - What is the order of precedence?
December 16, 2021
Controls - How to set the data source of a Combo Box to a comma separated string
November 16, 2021
Numbers - 10 examples of how to round numbers
August 18, 2021
Formula - Difference between round, square, and curly brackets
July 20, 2021
Top 3 highlights of upcoming enhancements to the Power Apps language (Power FX)
May 26, 2021
Email - Sending email attachments with the Office 365 Outlook connector
March 30, 2021
Formula - What to try when numbers don't format correctly
March 24, 2021
Controls - How to convert HTML to Text
March 23, 2021
Formulas - how to return all days between two dates
March 15, 2021
Formula - How to create comma separated (CSV) list of items
February 25, 2021
Formula - How to use the IF and Switch functions - 3 common examples
February 12, 2021
Location - Finding the closest location and and sorting records by distance, based on the current location of the user
January 24, 2021
Formulas - How to cope with weekends and public holidays in date calculations
January 21, 2021