How do I find the average of values?

I have 4 fields in my DB that are part of my page data type

  • Value 1
  • Value 2
  • Value 3
  • Value 4

On each page I wan to find the average of the 4 values.

I can do “This page’s value 1 + This page’s value 2 + This page’s value 3 + This page’s value 4 / 4” but the problem is that I don’t always have 4 values entered. Sometimes the user enters 2 or 3 values only, so how do I find the average of the values entered?

Any ideas?

Create a list of all the values, then use :avg to find the mean average.

This works but I have a query.

How do I best deal with values getting deleted. Once they are in the list, and I delete the value, it still stays in the list. How do I correctly identify input value 1 from input value 2 when deleting the value.

You can still use the average operator in searches. You could structure it like this:

search for x: each item's y: average

image

The issue is that each value is saved as a different field.

So it’s like value 1, value 2, value 3, value 4, etc.

value 1 plus value 2 plus value 3 plus value 4 divided by 4

or value 1:converted to list:plus item value 2 plus item value 3 plus item value 4:average

But if you’re having to average like this, it indicates inefficient DB structure as they should be a list of numbers or a new data type entirely.

If a user needs to be able to enter a list of numbers to average them, you should be having a repeating group that’s a list of numbers, then a button that allows them to add a new number to the list and a delete icon to remove existing numbers.

Then you just do Repeating Group’s list of numbers:average.

The issue again is that its not always 4 numbers.

The second suggestion, the type is a number, so I can’t find the converted to list option.

It’s an input field, so users need to enter numbers and I’m looking to dynamically calculate the average of those numbers. It could be 4 numbers or 3 numbers, or the user could enter 4 numbers and delete 1.

Yes, which is why you should probably use my last recommendation:

There might be issues around adding identical numbers but I’ll try and make a demo app that resolves those

Is this what you’re looking for?

Taking inspiration from @eliot1 nquforumdemo | Bubble Editor

1 Like

Thank you, I’d appreciate a demo app.

The below is an excel representation of what I’m trying to achieve. Basically the average dynamically changes even when values are added/removed, and I need to have 4 input fields

image

To an extent. I have 4 input fields only, so it has to be limited to just 4 fields, and there has to be 4 input fields, instead of just the one input field you have with a button.

This topic was automatically closed after 70 days. New replies are no longer allowed.