# Sumif type calculation

Where is it best to run a sumif calculation on values in a Data Type so that you can use the results on your web page outside of a repeating group?

What’s confusing me is once you get data into a repeating group, can you then run a sumif type calculation on it to then show the result outside of the repeating group on your web page or is this better to do via running the calculation in an expression element on that page?

Also, how do you Sumif in bubble?

Hey @bryan.holmes - for a list of numbers, the “sumif” operation (as found in spreadsheets) is just a filter, followed by a sum, in Bubble.

So like, I have a list of numbers [1,2,3,4,5,6] and I want to “sumif” value is > 2 (result 18) we can do that as:

`list :filtered (advanced filter: this item > 2) :sum`

Which is similar to how we might do it in JavaScript (.filter is the filter function and the .reduce is a way to sum the resulting array):

`[1,2,3,4,5,6].filter(item => item > 2).reduce((acc, v) => { return acc + v}, 0)`

And no, Repeating Groups are not computational elements even though people might want them to be. I talk about that a bit in a recent video here, if you’re interested:

1 Like

What type of element should I use to pull in that list to the operate on it? A group won’t take a list. I know repeating group isn’t what I want. So what do I do?

Well, this expression (where list is just an expression that resolves to some list of numbers you are interested in operating on) is itself just a scalar number, so you could put it in any field that accepts a number:

`list :filtered (advanced filter: this item > 2) :sum`

Though typically what one might want to do is have (1) the original list (2) the filtered list and (3) the sum and the vanilla Bubble equivalent of a local variable is the “Custom State”. These can exist anywhere on your page and you can create a custom state on any element either in the design mode (by clicking the little (i) icon at the top of an element’s config menu) like so:

… or these can be created while you’re in workflow view and configuring a set state action.

And so you’ll have some workflow (which could be triggered by any arbitrary event, depending upon what you’re trying to do) where you populate those custom state values and it would be something like:

get the original list whatever it is (it doesn’t have to be the result of a search of course)

filter the state we set in step 1, storing it in a state called filtered-list (this state is on my page object so it has the name of the page) note: in this case you don’t need an advanced filter (you might if you were doing this as a one-liner)

sum the filtered list and store that

And now we can use those states anywhere, referencing them as we would any other expression. Here I’m just spitting them into a text element so we can see what we’re working with:

And in the runtime (preview mode):

Edit: I might have also pointed out that you could use a single step to do all of this… note the “set another state” option on the Set State action dialog.

2 Likes

If you haven’t done the bubble lessons, BTW, you should. And if you don’t want to do that, just spend some time watching any of my videos where I just “do Bubble stuff” and you’ll start grokking basic concepts.

I can tell you’re confused about what expressions are and how they are used and about things like the strongly-typed nature of Bubble expressions.

2 Likes

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