Counting unique text in array

binary101

New Member
Joined
Oct 20, 2021
Messages
4
Office Version
  1. 2013
Platform
  1. Windows
Hi,

Im trying to count the number of unique drugs given to patients in an excel array, I currently use =SUMPRODUCT(1/COUNTIFS(B2:B9,B2:B9)) however I have to manually shift the cells when the Pt ID changes as the number of drugs given is different for each patient. Is there a way to display the Pt ID when the value changes as well as counting the total number of drugs given to that patient?
 

Attachments

  • example.png
    example.png
    20.3 KB · Views: 14

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Welcome to the MrExcel board!

Try this. Note that the column E formula should be confirmed with Ctrl+Shift+Enter, not just Enter

21 10 21.xlsm
ABCDE
1
21Drug 214
31Drug 431
41Drug 353
51Drug 2  
61Drug 2  
71Drug 1  
81Drug 1
91Drug 2
103Drug 3
115Drug 2
125Drug 4
135Drug 4
145Drug 3
155Drug 4
165Drug 3
Count Unique
Cell Formulas
RangeFormula
D2:D7D2=IFERROR(INDEX($A$2:$A$100,MATCH(0,INDEX(COUNTIF($D$1:D1,$A$2:$A$100)+(A$2:A$100=""),0),0)),"")
E2:E7E2=IF(D2="","",COUNT(1/(MATCH(D2&"|"&B$2:B$16,A$2:A$16&"|"&B$2:B$16,0)=ROW(A$2:A$16)-ROW(A$2)+1)))
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Solution
Thank you so much, I have tired it on a the actual data set, thought the formula didnt work but just took a while to go through the entire set, still trying to get my head around the nested index, match and countifs ?.
 
Upvote 0
thought the formula didnt work but just took a while to go through the entire set,
Do you have very large ranges, or perhaps used whole column references in the formulas?
 
Upvote 0
The data set has about 4700 rows which I specified instead of using the entire column.
 
Upvote 0
Oh well, not much more you can do then I think. At least it sounds like the correct results came in the end. :)
 
Upvote 0

Forum statistics

Threads
1,214,839
Messages
6,121,891
Members
449,058
Latest member
Guy Boot

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