DEV Community

Cover image for The best way to average out different data fields on Bubble
Atomic Fusion
Atomic Fusion

Posted on

The best way to average out different data fields on Bubble

Have you ever tried to average out different entries from a list in your Bubble app development? This is one of the recently discussed threads on the Forum. Let's take a closer look at the problem statement.

Assume you're making a scorecard with different categories - Math, English, Science, Economics. The goal is to find the average of the scores of these different categories.

Let's see some tried solutions:

One approach could be to add a new field (list of numbers), add each number of the categories to this list, and then average the list.

But, if there are two of the same numbers it would only add that number once, as duplicates can’t be added through Bubble’s “:plus item” operator and this would break the average.

In addition, adding all the different fields one by one to the list can take a long time.

There has to be a better, easier, and quicker solution to do this.

Using the :Group by operator for complex averaging

What if have a list of things with number fields as well as a category field and you want to calculate averages for each category. What do you do then?

Well, it’s actually quite straightforward with the ‘:Group by’ operator. It essentially breaks your list of things into smaller lists based on the Grouping (grouped by ‘myCategory’ here). ‘:Group by’ has an inbuilt option to categorize the list of things being grouped and also run a simple operation like an average or sum on each group that gets generated.

Image description

So what it eventually gives you in this case is a list of averages of each category as numbers. It could be quite a bit to grasp the first time, but it’s super handy once you get comfortable with it!

More things that can be tried out

You can try adding a unique identifier with a delimiter and then split out the list by the delimiter when you want to use it (e.g. right before you do your average). This should save you from adding all the different fields to the list one at a time.

Or in case your calculation is getting too complex, you could try the Complex Math Expression Calculator plugin 3

Here's the link to the Thread - https://forum.bubble.io/t/average-different-fields/225320/5

Hope it helped. 🔓

Top comments (0)