Count of unique occurrences with multiple criteria/exceptions

sdk1013

New Member
Joined
Dec 3, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I need help finding the count of unique encounters for data similar to the example below. A unique encounter = unique ID at a unique location receiving a unique service on a unique date by a unique provider. I need to be able to filter and run unique encounter counts based on those variables, i.e. unique encounters at location VA vs CA, for service a vs b vs c, on [date], and by provider x vs y.

Thanks so much for any guidance!

IDLOCATIONSERVICESERVICE DATEPROVIDER
1​
VAa
12/1/2021​
x
1​
VAa
12/1/2021​
x
1​
VAa
12/1/2021​
y
1​
VAb
12/1/2021​
x
1​
VAa
12/2/2021​
x
1​
VAb
12/2/2021​
y
1​
VAc
12/1/2021​
x
1​
VAc
12/3/2021​
x
1​
VAc
12/2/2021​
y
2​
CAa
12/1/2021​
x
2​
CAa
12/1/2001​
y
2​
CAc
12/1/2021​
x
2​
CAc
12/1/2021​
x
2​
CAb
12/2/2021​
x
2​
CAb
12/2/2021​
y
2​
CAa
12/2/2021​
x
2​
CAb
12/2/2021​
x
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
this subroutine will give the count of the uniques in all 5 columns , if you just want the first 4 just change the j loop to 4, ditto 3, and 2. If you want to miss one out in the middle just put and if statement in to skip that loop
VBA Code:
Sub Countoccurence()
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
ary = Worksheets("Sheet1").Range(Cells(1, 1), Cells(lastrow, 5))
   Dim Dic As Object
   Set Dic = CreateObject("Scripting.dictionary")
      
   For i = 1 To UBound(ary, 1)
        ' concatenate 5 columns
         tt = ""
         For j = 1 To 5
         tt = tt & ary(i, j)
         Next j
         Dic(tt) = i
   Next i
 MsgBox ("Number of unique values is " & Dic.Count)
 
End Sub
 
Upvote 0
Welcome to the MrExcel forum!

Try:

Book2
ABCDEFGHIJKLM
1IDLOCATIONSERVICESERVICE DATEPROVIDERIDLocationServiceService DateProviderUnique count
21VAa12/1/2021x*VA*12/2/2021*3
31VAa12/1/2021x
41VAa12/1/2021y
51VAb12/1/2021x
61VAa12/2/2021x
71VAb12/2/2021y
81VAc12/1/2021x
91VAc12/3/2021x
101VAc12/2/2021y
112CAa12/1/2021x
122CAa12/1/2021y
132CAc12/1/2021x
142CAc12/1/2021x
152CAb12/2/2021x
162CAb12/2/2021y
172CAa12/2/2021x
182CAb12/2/2021x
Sheet4
Cell Formulas
RangeFormula
M2M2=ROWS(UNIQUE(FILTER(A2:E18,((A2:A18=G2)+(G2="*"))*((B2:B18=H2)+(H2="*"))*((C2:C18=I2)+(I2="*"))*((D2:D18=J2)+(J2="*"))*((E2:E18=K2)+(K2="*")))))


In the G2:K2 cells, put an asterisk ("*") in the cell if you don't want to restrict that column. Otherwise, put the value you want to see.
 
Upvote 0
Solution
Welcome to the MrExcel forum!

Try:

Book2
ABCDEFGHIJKLM
1IDLOCATIONSERVICESERVICE DATEPROVIDERIDLocationServiceService DateProviderUnique count
21VAa12/1/2021x*VA*12/2/2021*3
31VAa12/1/2021x
41VAa12/1/2021y
51VAb12/1/2021x
61VAa12/2/2021x
71VAb12/2/2021y
81VAc12/1/2021x
91VAc12/3/2021x
101VAc12/2/2021y
112CAa12/1/2021x
122CAa12/1/2021y
132CAc12/1/2021x
142CAc12/1/2021x
152CAb12/2/2021x
162CAb12/2/2021y
172CAa12/2/2021x
182CAb12/2/2021x
Sheet4
Cell Formulas
RangeFormula
M2M2=ROWS(UNIQUE(FILTER(A2:E18,((A2:A18=G2)+(G2="*"))*((B2:B18=H2)+(H2="*"))*((C2:C18=I2)+(I2="*"))*((D2:D18=J2)+(J2="*"))*((E2:E18=K2)+(K2="*")))))


In the G2:K2 cells, put an asterisk ("*") in the cell if you don't want to restrict that column. Otherwise, put the value you want to see.
This is great, thanks so much!
As a follow-up question to take this to the next level, how would you go about making that output table include all the options for location, service, date, and provider? Is that possible? I'm imagining a filter drop down for those columns so that I can select whatever exclusions readily. For example, a filter drop down for location having *, VA, and CA, and one for service having *, a, b, and c.
 
Upvote 0
Try this:

Book1
ABCDEFGHIJKLM
1IDLOCATIONSERVICESERVICE DATEPROVIDERIDLocationServiceService DateProviderUnique count
21VAa12/1/2021x*VA*12/2/2021*3
31VAa12/1/2021x
41VAa12/1/2021y
51VAb12/1/2021x
61VAa12/2/2021x
71VAb12/2/2021y
81VAc12/1/2021xIDLocationServiceService DateProvider
91VAc12/3/2021x1VAa44531x
101VAc12/2/2021y2CAb12/2/2021y
112CAa12/1/2021x**c12/3/2021*
122CAa12/1/2021y**
132CAc12/1/2021x
142CAc12/1/2021x
152CAb12/2/2021x
162CAb12/2/2021y
172CAa12/2/2021x
182CAb12/2/2021x
19
Sheet7
Cell Formulas
RangeFormula
M2M2=ROWS(UNIQUE(FILTER(A2:E18,((A2:A18=G2)+(G2="*"))*((B2:B18=H2)+(H2="*"))*((C2:C18=I2)+(I2="*"))*((D2:D18=J2)+(J2="*"))*((E2:E18=K2)+(K2="*")))))
K9:K11,I9:J12,G9:H11G9=IFERROR(INDEX(UNIQUE(A2:A18),SEQUENCE(ROWS(UNIQUE(A2:A18))+1)),"*")
Dynamic array formulas.
Cells with Data Validation
CellAllowCriteria
G2:K2List=G9#


The formula to get a unique list of values is just =UNIQUE(A2:A18) but I had to make it a bit more complicated to get the * on the list. Then once you have the unique lists (and you can put them anywhere, even on another sheet), you can use them in a Data Validation drop-down list.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,862
Members
449,052
Latest member
Fuddy_Duddy

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