October 13, 2024

Power Apps: Calculate totals based in Gallery results

A few weeks ago, I published an article going over an experiment that we did with timesheets that failed. You can read all about it here, but today I want to focus on one specific part of the article and explain a little bit further. I want to describe how to calculate the values based on the Gallery and not the data-source.

In the article, I show you formulas and explain what they are for, but first let’s create the data-source.

The Setup

To achieve this is quite simple. I’ll create a SharePoint list that has a few items that we want to filter and calculate.

It can’t be simpler—a list with a description, hours spent, and the date when we did them.

Let’s add some simple data.

The Power App

Let’s use the template to make things faster. Our objective in this article is not to have the prettiest app, but to show you the concepts.

Start with the data in SharePoint.

Then the site:

Finally, the list:

Power Apps will generate this app for you:

I know it’s not the prettiest app, but it’s enough to explain the concept.

The formula

Let’s start simple. Let’s add the number of hours and show them in the title.

The formula looks correct, but why the error? Can you spot the issue?

I’ll help a little. Here’s the syntax of Sum function:

Sum( Table, NumericalFormula )

We’re providing a table, but not the “NumericalFormula.” It’s a common mistake, but the correct syntax is the following:

"Timesheet " & Sum(BrowseGallery1.AllItems, hours) & " hours"

It looks incredibly basic, but it’s amazingly useful. Let’s perform a filter and see what we get:

Awesome right? We don’t need to change our formula in any way, and it automatically updates the values.

You can complicate the formula to give you different types of values, like averages, count of items, etc., but the concept is always the same. Always perform the math on the Gallery and not the data source so that you can get the auto-updating values as described above.

Final Thoughts

I know it’s a simple example, but I see this issue all the time. People clone the filtering formula, regardless of what it is, and then do the sum on top of the formula. But this has several disadvantages:

  1. If you change the filtering formula, you need to remember to change  it everywhere
  2. If it makes things unnecessarily complicated. With a simple formula, you can get a clean result that always works.
  3. You can do multiple actions using the same source of data, regardless of the parsing done to it. You have a clear separation of responsibilities.
  4. You don’t block yourself in a corner where formula may become not delegable. With this strategy, your formula is always delegable since you’re fetching the information in the Gallery.
  5. You don’t need to calculate anything further. Updates are automatic and results are always updated.

 

Have a suggestion of your own or disagree with something I said? Leave a comment or interact on Twitter and be sure to check out other Microsoft Power Apps-related articles here

Photo by Dan Meyers on Unsplash

Manuel Gomes

I have 18 years of experience in automation, project management, and development. In addition to that, I have been writing for this website for over 3 years now, providing readers with valuable insights and information. I hope my expertise allows me to create compelling, informative content that resonates with the audience.

View all posts by Manuel Gomes →

Leave a Reply

Your email address will not be published. Required fields are marked *

Mastodon