SUMIF with multiple criteria and first instance only

vgresia

New Member
Joined
May 31, 2018
Messages
21
Hi there - I'm trying to sum all of the data in column D that have either a yes in the primary or secondary category, but only include the first instance of that value. In the example below, the sum would be 3 (1 for A, 2 for B). Please help!! :)
[TABLE="width: 500"]
<tbody>[TR]
[TD]Area[/TD]
[TD]Primary Site[/TD]
[TD]Secondary Site[/TD]
[TD]Value[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]yes[/TD]
[TD]no[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]yes[/TD]
[TD]no[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]no[/TD]
[TD]yes[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]C[/TD]
[TD]no[/TD]
[TD]no[/TD]
[TD]3[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Welcome to Mr Excel forum

What is the expected result for a scenario like this (observe that only the second A meet the condition)?

[Table="class: grid"][tr][td="bgcolor: #DCE6F1"][/td][td="bgcolor: #DCE6F1"]
A
[/td][td="bgcolor: #DCE6F1"]
B
[/td][td="bgcolor: #DCE6F1"]
C
[/td][td="bgcolor: #DCE6F1"]
D
[/td][/tr]
[tr][td="bgcolor: #DCE6F1"]
1
[/td][td]
Area​
[/td][td]
Primary Site​
[/td][td]
Secondary Site​
[/td][td]
Value​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
2
[/td][td]
A​
[/td][td]
no​
[/td][td]
no​
[/td][td]
1​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
3
[/td][td]
B​
[/td][td]
yes​
[/td][td]
no​
[/td][td]
2​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
4
[/td][td]
A​
[/td][td]
no​
[/td][td]
yes​
[/td][td]
5​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
5
[/td][td]
C​
[/td][td]
no​
[/td][td]
no​
[/td][td]
3​
[/td][/tr]
[/table]


It would be 7 (2 for B and 5 for A)?

M.
 
Upvote 0
The expected result would be 7, because row 3 meets the condition (has a yes), row 4 meets the condition (is the first A to have a yes).
 
Upvote 0
The easiest way would be to add a helper column, like this:

ABCDEFG

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]
[TD="bgcolor: #FAFAFA"]Area[/TD]
[TD="bgcolor: #FAFAFA"]Primary Site[/TD]
[TD="bgcolor: #FAFAFA"]Secondary Site[/TD]
[TD="bgcolor: #FAFAFA"]Value[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]2[/TD]
[TD="bgcolor: #FAFAFA"]A[/TD]
[TD="bgcolor: #FAFAFA"]yes[/TD]
[TD="bgcolor: #FAFAFA"]no[/TD]
[TD="bgcolor: #FAFAFA, align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]3[/TD]

[TD="align: center"]3[/TD]
[TD="bgcolor: #FAFAFA"]B[/TD]
[TD="bgcolor: #FAFAFA"]yes[/TD]
[TD="bgcolor: #FAFAFA"]no[/TD]
[TD="bgcolor: #FAFAFA, align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]4[/TD]
[TD="bgcolor: #FAFAFA"]A[/TD]
[TD="bgcolor: #FAFAFA"]no[/TD]
[TD="bgcolor: #FAFAFA"]yes[/TD]
[TD="bgcolor: #FAFAFA, align: right"]7[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]5[/TD]
[TD="bgcolor: #FAFAFA"]C[/TD]
[TD="bgcolor: #FAFAFA"]no[/TD]
[TD="bgcolor: #FAFAFA"]no[/TD]
[TD="bgcolor: #FAFAFA, align: right"]3[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

</tbody>
Sheet4

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="width: 100%"]
<thead>[TR="bgcolor: #DAE7F5"]
[TH="width: 10px"]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
</thead><tbody>[TR]
[TH="width: 10px, bgcolor: #DAE7F5"]E2[/TH]
[TD="align: left"]=(COUNTIF(B2:C2,"yes")>0)*(COUNTIFS(A$1:A1,A2,B$1:B1,"yes")+COUNTIFS(A$1:A1,A2,C$1:C1,"yes")=0)[/TD]
[/TR]
[TR]
[TH="width: 10px, bgcolor: #DAE7F5"]F2[/TH]
[TD="align: left"]=SUMPRODUCT(D2:D5,E2:E5)[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Array Formulas[TABLE="width: 100%"]
<thead>[TR="bgcolor: #DAE7F5"]
[TH="width: 10px"]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
</thead><tbody>[TR]
[TH="width: 10px, bgcolor: #DAE7F5"]G2[/TH]
[TD="align: left"]{=SUM(IF(ROW(A2:A5)-ROW(A2)+1=IF(IFERROR(MATCH(A2:A5&"yes",A2:A5&B2:B5,0),2^20)< IFERROR(MATCH(A2:A5&"yes",A2:A5&C2:C5,0),2^20),IFERROR(MATCH(A2:A5&"yes",A2:A5&B2:B5,0),2^20),IFERROR(MATCH(A2:A5&"yes",A2:A5&C2:C5,0),2^20)),D2:D5))}[/TD]
[/TR]
</tbody>[/TABLE]
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself[/TD]
[/TR]
</tbody>[/TABLE]




The formula in E2 figures out if the area on that row is the first instance of the area with a "yes". Then the very simple formula in F2 gives you the sum.

It is possible to create a single-cell formula to get that sum, for example, look at the G2 array formula. It's pretty complicated, but maybe someone else can improve on it.
 
Last edited:
Upvote 0
The code that ended up working for this was:
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">=SUMPRODUCT(((($B$2:$B$5="yes")+($C$2:$C$5="yes"))*$D$2:$D$5)/((COUNTIFS($A$2:$A$5,$A$2:$A$5,$B$2:$B$5,"yes")+COUNTIFS($A$2:$A$5,$A$2:$A$5,$C$2:$C$5,"yes"))+(($B$2:$B$5="no")*($C$2:$C$5="no"))))</code>
 
Upvote 0
Another possible formula


[Table="class: grid"][tr][td="bgcolor: #DCE6F1"][/td][td="bgcolor: #DCE6F1"]
A
[/td][td="bgcolor: #DCE6F1"]
B
[/td][td="bgcolor: #DCE6F1"]
C
[/td][td="bgcolor: #DCE6F1"]
D
[/td][td="bgcolor: #DCE6F1"]
E
[/td][td="bgcolor: #DCE6F1"]
F
[/td][/tr]
[tr][td="bgcolor: #DCE6F1"]
1
[/td][td]
Area​
[/td][td]
Primary Site​
[/td][td]
Secondary Site​
[/td][td]
Value​
[/td][td][/td][td]
Result​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
2
[/td][td]
A​
[/td][td]
no​
[/td][td]
no​
[/td][td]
1​
[/td][td][/td][td]
7​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
3
[/td][td]
B​
[/td][td]
yes​
[/td][td]
no​
[/td][td]
2​
[/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
4
[/td][td]
A​
[/td][td]
no​
[/td][td]
yes​
[/td][td]
5​
[/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
5
[/td][td]
C​
[/td][td]
no​
[/td][td]
no​
[/td][td]
3​
[/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
6
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]
[/table]


Array formula in F2
=SUM(IF(FREQUENCY(IFERROR(MATCH(A2:A5,IF((B2:B5="yes")+(C2:C5="yes"),A2:A5),0),FALSE),ROW(A2:A5)-ROW(A2)+1),D2:D5))
confirmed with Ctrl+Shift+Enter, not just Enter

M.
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,157
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top