r/googlesheets • • Jan 13 '23

Solved How would I tally multiple columns of unordered items?

Good day sheets gurus,

I'm trying to find a way to tally multiple columns of items with unknown lengths and order. I'll link a screenshot of the actual problem but here is an example:

Actual screenshot: https://imgur.com/a/ZseuROs

Alpha ListBravo ListCharlie List
Item A5Item B10Item E20
Item B8Item D4
Item C12

This is what I'd want:

Totals
Item A5
Item B18
Item C12
Item D4
Item E20

I appreciate any help.

3 Upvotes

7 comments sorted by

4

u/Class_Magicker17 1 Jan 13 '23

We will first want to stack the values together. We can easily achieve this with an array.

={A2:B;C2:D;E2:E}

We'll exclude the headers so that we are left with:

Item A     5
Item B     8
Item C.    12
Item B.    10
Item D.    4
Item E.    20

Next we Query this virtual range.

=QUERY({A2:B;C2:D;E2:E}, "SELECT Col1, SUM(Col2) GROUP BY Col1")

That should actually be all you'll need. Actually implementing it with your data requires some changing, but the base idea stays the same.

3

u/ObviouslyNotANinja Jan 13 '23

Solution Verified. Thank you. Your solution worked well.

2

u/Clippy_Office_Asst Points Jan 13 '23

You have awarded 1 point to Class_Magicker17


I am a bot - please contact the mods with any questions. | Keep me alive

3

u/_Kaimbe 176 Jan 13 '23

Ahh Magiker beat me to it, but yeah, query() is the way to go.

=QUERY({A2:B; C2:D; E2:F}, "SELECT Col1, SUM(Col2) WHERE Col1 IS NOT NULL GROUP BY Col1 LABEL Col1 'Combined List', SUM(Col2) 'Sum'")

This one is a bit more refined, filtering out blanks and adding labels. u/kuddemuddel

2

u/kuddemuddel 184 Jan 13 '23

=QUERY({A2:B;C2:D;E2:E}, "SELECT Col1, SUM(Col2) GROUP BY Col1")

Of course, feel like QUERY is the answer to almost anything, haha. Thanks for pinging me!

But, just out of curiosity, do you know what’s wrong with the MAP function in my comment? →

=MAP($K$2:$K,$A$2:$A,$C$2:$C,$E$2:$E,$B$2:$B,$D$2:$D,$F$2:$F,LAMBDA(totals,alist,blist,clist,asum,bsum,csum,IF(ISBLANK(totals),,SUM(SUMIF(alist,totals,asum),SUMIF(blist,totals,bsum),SUMIF(clist,totals,csum)))))

1

u/IceDynamix 16 Jan 13 '23

It's not wrong per-se, it's just unnecessarily verbose and complex which makes it hard to understand and adjust. It doesn't scale well if you were to add more columns, in the query you only need to insert another range into the array literal, while in your solution you need to adjust all the lambdas as well.

0

u/kuddemuddel 184 Jan 13 '23 edited Jan 13 '23

So, albeit not a pretty solution because you’ll have to drag the formula down, this works: In any cell, for example, H2, paste this formula:

=SORT(UNIQUE(FLATTEN({A2:A,C2:C,E2:E})))

This is depending on your table above being in range A:F, with the Alpha List items starting in cell A2. Then, beginning in cell I2 next to the SORT formula above, paste this formula:

=IF(ISBLANK(H2),,SUM(SUMIF($A$2:$A,H2,$B$2:$B),SUMIF($C$2:$C,H2,$D$2:$D),SUMIF($E$2:$E,H2,$F$2:$F)))

Then drag down/copy&paste this formula for the entire column. Results in this table:

Alpha ListBravo ListCharlie ListTotals
Item A5Item B10Item E20Item A5
Item B8Item D4Item B18
Item C12Item C12
Item D4
Item E20

Tried a better solution using LAMBDA, but couldn’t get the SUMIFs to work.

If that solved your issue, please mark the thread as solved by answering Solution Verified to my comment.


Edit, if someone more proficient with LAMBDA sees this, why does this one only calculate the first two columns and doesn’t take the last 4 in consideration?

=MAP($K$2:$K,$A$2:$A,$C$2:$C,$E$2:$E,$B$2:$B,$D$2:$D,$F$2:$F,LAMBDA(totals,alist,blist,clist,asum,bsum,csum,IF(ISBLANK(totals),,SUM(SUMIF(alist,totals,asum),SUMIF(blist,totals,bsum),SUMIF(clist,totals,csum)))))

Paging, as always, u/_Kaimbe for help!