4
1
I have such table of payments:
account | amount
--------+-------
101 | 3
101 | 5
102 | 7
103 | 9
I named the range of this table "payments". Its first column I called "accounts". Its second column I called "amounts".
I also have another table which assigns accounts to groups:
account | group
--------+-------
101 | 1
102 | 1
103 | 2
I named the range of this table "groups".
Now I want to sum all payments of accounts of group 1 using only one formula, without using any additional columns. I'm trying to do that using such array formula:
sum(if(vlookup(accounts,groups,2,false)=1,amounts,0))
I also tried such array formula:
sumif(if(vlookup(accounts,groups,2,false)=1,amounts,0))
These formulas do not work. I think I know why - it seems that vlookup
function does not return array when used in one cell array formula.
So, how can I calculate it using one formula?
Using LOOKUP is a valid approach, Reinier, but there are some possible downsides - firstly for
LOOKUP
to work correctly the "lookup range" [C1:C3] must be sorted ascending (as per example but I don't know if that can be guaranteed) and secondly if any of the accounts1 codes don't appear in the accounts2 list then you would either get an error or a "false match" - my approach doesn't have those potential drawbacks – barry houdini – 2013-01-30T09:50:10.2671@barryhoudini For the problem statement,
LOOKUP
is the right function. And on the upside, don't you think the resulting formula is more elegant and better maintainable than, for example, yours ;-)? But from an educational standpoint, you are correct, I should have mentioned that. I extended the answer. – Reinier Torenbeek – 2013-01-30T17:36:32.713@user983447 Glad to help. Check out the link I added, or barry houdini's comment, for conditions under which this approach using
LOOKUP
works. – Reinier Torenbeek – 2013-01-30T17:42:24.557more elegant and better maintainable? Yes, definitely, good answer! Perhaps I was trying to cope with non-existent issues – barry houdini – 2013-01-30T17:43:14.260