Home > Enterprise >  Conditionally Sum a googlesheet column based on entries in related tables
Conditionally Sum a googlesheet column based on entries in related tables

Time:06-01

Say I have two related sheets/tabs within a google sheet. One sheet/tab is titled "Categories", the other is "Measures".

Categories:

userid catcode
1 a
1 b
2 a
3 c

Measures:

userid catcode points
1 a 5
1 b 5
1 c 3
2 a 4
3 c 3

For each user I'd like to be able to sum the points from the Measures table where the catcode is present for the user in the categories table. Ideally using an auto-extending/filling formula (like an arrayformula or query).

I have some idea how I'd approach this with SQL statements (joining the related tables, or doing a select where exists), but I'm new to googlesheets and would appreciate some direction here. I've experimented with this a bit and assuming a third table named "Users" with userids in column A, I can add this formula:

=sum(filter(measure!C2:C4, measure!A2:A4=users!A2, not(iserror(vlookup(measure!B2:B4, unique(filter(categories!B2:B5, categories!A2:A5=users!A2)), 1, false)))))

However this approach doesn't seem to be compatible with arrayformula and won't allow me to autofill down the Users tab for newly added userids. Sum itself is apparently incompatible with arrayformula. Additionally, if I enclose the above in arrayformula and replace sum with sumproduct or some other approach to the summation, I'm unable to get the users!A2 references to extend down as I'd expect via something like users!A2:A.

Any help/direction would be appreciated. Thanks!

CodePudding user response:

try:

=ARRAYFORMULA(QUERY({A1:A, VLOOKUP(A1:A&B1:B, {D1:D&E1:E, F1:F}, 2, 0)}, 
 "select Col1,sum(Col2) where Col1 is not null group by Col1 label sum(Col2)''"))

enter image description here

  • Related