Formula to SUM distinct values based on a specific list

Milos

Board Regular
Joined
Aug 28, 2016
Messages
121
Office Version
  1. 365
Platform
  1. Windows
Hi there,

I am wanting a bit of help to refine a formula so that I can count the number distinct values in a certain range. This will assisting me ascertaining people hours for a project that I am involved with.

I am currently using this formula =SUMPRODUCT(1/COUNTIF(H3:K353,H3:K353)) which works well except I need to be able to exclude particular values in that range. Can anybody explain to me how I would refine this formula to only include specific values in this formula? The list of distinct values I want to count is: MA, MB, EMW, SR, KF, HS, KR, EW, CM, JR & HO

Cheers,
Milos
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Maybe:

=SUMPRODUCT(SIGN(COUNTIF(H3:K353,{"MA","MB","EMW","SR","KF","HS","KR","EW","CM","JR","HO"})))
 
Upvote 0
Wow that is impressive! Thank you very much!
 
Upvote 0
Glad to help! :cool:

And I just realized it doesn't have to be SUMPRODUCT, it works just fine with a regular SUM:

=SUM(SIGN(COUNTIF(H3:K353,{"MA","MB","EMW","SR","KF","HS","KR","EW","CM","JR","HO"})))
 
Upvote 0
Thanks Eric,

I have finally started to implement this. Unfortunately my idea of what I needed was wrong and I need to amend this formula.. I am not sure if I need to make a new thread for this or not? I am happy to do so.

I actually need to merge the original formula into COUNTIFS formula (I think). So that if column B:B has a specific number (e.g. 37) then the formula below will only SUM the list for those rows. I tried using an IF formula to no avail. I think I need to utilise a COUNTIFS but I have not been able to make it work.

Formula to:
COUNTIF(B:B,37) and then: SUM(SIGN(COUNTIF(H3:K353,{"MA","MB","EMW","SR","KF","HS","KR","EW","CM","JR","HO"})))

Cheers,
Milos
 
Upvote 0
I'm not quite clear on what you want. Consider this range:

Book1
BCDEFGHIJK
41XXXX
52MAMB
637HOSR
75
837SRSR
937SRKF
104
1137MAX
Sheet3


First, is this a reasonable subset of your sheet? Next, what would the final result be? How many times would I count MA, HO, SR, KF, MB, X in that total?
 
Upvote 0
Apologies for the confusion. Yes this a reasonable subset.

Based on the subset that you created, the ideal formula would get a result of 4 ( see cell C9).

1. The count would only search in rows with 37 so rows 1, 2, 4 and 7 are dismissed automatically etc.
2. Then based on the above formula it would dismiss duplicate SR results in cells I5, I6 and J5, only counting distinct values once.
3. Lastly it would dismiss 'x' in column J8 due to 'x' not being in the above formula list (e.g.{"MA","MB","EMW","SR","KF","HS","KR","EW","CM","JR","HO"}).

1575842067842.png


Hopefully this makes sense?

Cheers,
Milos
 
Upvote 0
Tricky! This is the kind of formula that's fun to figure out. It's sometimes hard to figure out which set of functions to use that don't result in a 3-D (or 4-D!) internal arrays. But it generally ends up being complicated and hard to follow and/or maintain. But try this:

Book1
BCDEFGHIJK
31XXXX
42MAMB
537HOSR
65
737SRSR
837SRKF
94
1037MAX
11Final Count Version 1:4
12Final Count Version 2:4
Sheet1
Cell Formulas
RangeFormula
C11C11=SUM(SIGN(COUNTIFS(B:B,37,H:H,{"MA";"MB";"EMW";"SR";"KF";"HS";"KR";"EW";"CM";"JR";"HO"})+ COUNTIFS(B:B,37,I:I,{"MA";"MB";"EMW";"SR";"KF";"HS";"KR";"EW";"CM";"JR";"HO"})+ COUNTIFS(B:B,37,J:J,{"MA";"MB";"EMW";"SR";"KF";"HS";"KR";"EW";"CM";"JR";"HO"})+ COUNTIFS(B:B,37,K:K,{"MA";"MB";"EMW";"SR";"KF";"HS";"KR";"EW";"CM";"JR";"HO"})))
C12C12{=SUM(SIGN(MMULT(TRANSPOSE(ROW(H3:H353)^0),(B3:B353=37)*COUNTIFS(OFFSET(H3:K3,ROW(H3:H353)-ROW(H3),0),{"MA","MB","EMW","SR","KF","HS","KR","EW","CM","JR","HO"}))))}
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.


The C11 formula is longer, but probably more efficient, and easier to understand. But it's also a pain to maintain. If you add a new set of initials, you have to add it in 4 places. If you add another column to search, say L, then you have to add a whole new COUNTIFS to the formula.


The C12 formula is my attempt to shorten it. This requires row references instead of whole column references, so you have to change the 353 in 3 places if you add a row. But adding a new set of initials is as easy as adding it to the list once, and adding another column is as easy as changing H3:K3 to H3:L3. But this requires entry by pressing Control+Shift+Enter, and the OFFSET function in it makes it volatile, so there could be some performance issues. But try them out and see how they work. Maybe I (or someone else here) can come up with yet a better version.
 
Upvote 0

Forum statistics

Threads
1,214,883
Messages
6,122,077
Members
449,064
Latest member
MattDRT

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