I deeply apologize for the title being unclear. It is difficult to explain what I am looking for in such a small amount of characters. To provide some background, see the table below for reference.
Column A | Column B |
---|---|
600 | apple |
-100 | apple |
-200 | apple |
500 | orange |
-100 | orange |
300 | pear |
I am looking for a single formula that will find the highest sum of values in column A that have matching values in column B. Per the table, apple has the highest individual value of 600 at A2. However, when summing apple's corresponding values in column A, 600 - 100 - 200 = 300. When performing the same calculations with orange, 500 - 100 = 400. Thus, I would like the formula to return 400 since it is greater than apple's 300.
At some point, I ended up with the following formula. After testing, I realize it is incorrect:
=SUM(FILTER(B2:B7,A2:A7=INDEX(A2:A7,MATCH(MAX(COUNTIF(A2:A7,A2:A7)),COUNTIF(A2:A7,A2:A7),0))))
It appears the above formula first finds the highest occurring fruit in column B and then sums it's corresponding values in column A afterwards. The solution I am looking for shouldn't care about the number of occurrences in column B.
Any help would be appreciated. Thanks in advance!
=index(query(A:B,"SELECT SUM(A), B WHERE NOT A IS NULL GROUP BY B ORDER BY SUM(A) DESC"),2)
Result:
Or
=index(sort(HSTACK(map(unique(B1:B6),lambda(a,sumif(B1:B6,a,A1:A6))),unique(B1:B6)),1,0),1)