Help with counting the number of events in a spreadsheet based on multiple criteria.

sasha123

New Member
Joined
Sep 13, 2022
Messages
2
Office Version
  1. 365
Platform
  1. MacOS
Hi all,

I'm not sure my title explains this question well but I am struggling to label my data in a specific way.
I have a spreadsheet with where individuals (identified by a UR) have more than one scan in their pregnancy. Each pregnancy is identified by its due date.
I am wanting to find a formula to count the number of times a person has a pregnancy in my dataset regardless of the number of scans. This can occur 5+ times for some people.
In the example below, the UR 25016151 has two pregnancies - the pregnancy ID for the pregnancy with the due date 21/8/19 should be labelled 1 in each row with the correlating scan as it is the first pregnancy in this set, and then the second due date of 2/12/20 should be labelled 2. All other URs only have 1 pregnancy in this set and hence their pregnancy ID should be 1.

Thanks very much in advance, please let me know if any further clarification is needed!

Sample Data.xlsx
ABCDE
1Exam dateDOBURDue DatePregnancy ID
27/25/192/18/95250161518/21/19
36/27/192/18/95250161518/21/19
44/12/192/18/95250161518/21/19
511/4/2012/2/78286180212/1/20
610/6/2012/2/78286180212/1/20
79/8/2012/2/78286180212/1/20
810/28/202/18/952501615112/2/20
99/10/202/18/952501615112/2/20
107/22/202/18/952501615112/2/20
1111/5/2011/8/882628184412/2/20
1210/21/2011/8/882628184412/2/20
1310/8/2011/8/882628184412/2/20
149/10/2011/8/882628184412/2/20
1511/6/2010/19/942628265612/2/20
1610/23/2010/19/942628265612/2/20
1711/2/204/17/882628662712/2/20
1810/6/204/17/882628662712/2/20
199/16/204/17/882628662712/2/20
207/15/204/17/882628662712/2/20
Sheet1
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi.

Is this correct? (Edit: No. Something's off. Will post again soon.)

With 2 helper columns (which you can hide, after you carry the formulas in them down), I have a report/summary of the number of pregnancies each individual had. (Most of these are dynamic formulas, as I see that you have Office 365. But they would work in Office 2021 also.) You do not carry down the formula that's in column i. (It automatically expands on its own.) But carry down the formulas in the other 3 columns.
Blank.xlsb
ABCDEFGHIJ
1Exam dateDOBURDue DatePregnancy IDHelper Column (Hide me)Number of Pregnancies (Hide Me)Report/Summary
27/25/20192/18/1995250161518/21/20190225016151had 2 pregnancies.
36/27/20192/18/1995250161518/21/2019022861802had 2 pregnancies.
44/12/20192/18/1995250161518/21/20191226281844had 2 pregnancies.
511/4/202012/2/1978286180212/1/20200126282656had 1 pregnancy.
610/6/202012/2/1978286180212/1/20200126286627had 1 pregnancy.
79/8/202012/2/1978286180212/1/202011 
810/28/20202/18/19952501615112/2/202002 
99/10/20202/18/19952501615112/2/202002 
107/22/20202/18/19952501615112/2/202012 
1111/5/202011/8/19882628184412/2/202001 
1210/21/202011/8/19882628184412/2/202001 
1310/8/202011/8/19882628184412/2/202001 
149/10/202011/8/19882628184412/2/202011 
1511/6/202010/19/19942628265612/2/202001 
1610/23/202010/19/19942628265612/2/202011 
1711/2/20204/17/19882628662712/2/202000 
1810/6/20204/17/19882628662712/2/202000 
199/16/20204/17/19882628662712/2/202000 
207/15/20204/17/19882628662712/2/202000 
Sheet6 (2)
Cell Formulas
RangeFormula
F2:F20F2=IF(AND(C2<>C3,C3<>""),1,0)
G2:G20G2=SUM(IF(C$2:C20=C2,F$2:F20,0))
I2:I6I2=UNIQUE(C$2:C20)
J2:J20J2=IF(I2<>"","had "&G2&IF(G2>1," pregnancies."," pregnancy."),"")
Dynamic array formulas.
 
Last edited:
Upvote 0
Okay, yet another helper column. So is this correct? (So now, carry down the formulas in all columns except for column K.)

Blank.xlsb
ABCDEFGHIJK
1Exam dateDOBURDue DatePregnancy IDHelper Column (Hide me)Number of Pregnancies (Hide me)Helper Column (Hide me)Report/Summary
27/25/20192/18/1995250161518/21/201902225016151 pregnancies. for individual: 25016151
36/27/20192/18/1995250161518/21/20190212861802 pregnancies. for individual: 2861802
44/12/20192/18/1995250161518/21/201912126281844 pregnancies. for individual: 26281844
511/4/202012/2/1978286180212/1/202001126282656 pregnancies. for individual: 26282656
610/6/202012/2/1978286180212/1/202001126286627 pregnancies. for individual: 26286627
79/8/202012/2/1978286180212/1/202011  
810/28/20202/18/19952501615112/2/202002  
99/10/20202/18/19952501615112/2/202002  
107/22/20202/18/19952501615112/2/202012  
1111/5/202011/8/19882628184412/2/202001  
1210/21/202011/8/19882628184412/2/202001  
1310/8/202011/8/19882628184412/2/202001  
149/10/202011/8/19882628184412/2/202011  
1511/6/202010/19/19942628265612/2/202001  
1610/23/202010/19/19942628265612/2/202011  
1711/2/20204/17/19882628662712/2/202001  
1810/6/20204/17/19882628662712/2/202001  
199/16/20204/17/19882628662712/2/202001  
207/15/20204/17/19882628662712/2/202011  
Sheet6 (3)
Cell Formulas
RangeFormula
F2:F20F2=IF(AND(C2<>C3),1,0)
G2:G20G2=SUM(IF(C$2:C20=C2,F$2:F20,0))
I2:I20I2=IFERROR(VLOOKUP(K2,C$2:G20,5),"")
J2:J20J2=IF(K2<>"",K2&IF(K2>1," pregnancies."," pregnancy.")&" for individual: ","")
K2:K6K2=UNIQUE(C$2:C20)
Dynamic array formulas.
 
Last edited:
Upvote 0
Note: I just rearranged it so that you can carry down the formulas easier. (Carry down the formulas for all columns except for column K.)

Edit: Made an obvious mistake. For the third time!
Blank.xlsb
ABCDEFGHIJK
1Exam dateDOBURDue DatePregnancy IDHelper Column (Hide me)Number of Pregnancies (Hide me)Helper Column (Hide me)Report/Summary
27/25/20192/18/1995250161518/21/20190222 pregnancies for individual: 25016151
36/27/20192/18/1995250161518/21/20190211 pregnancy for individual: 2861802
44/12/20192/18/1995250161518/21/20191211 pregnancy for individual: 26281844
511/4/202012/2/1978286180212/1/20200111 pregnancy for individual: 26282656
610/6/202012/2/1978286180212/1/20200111 pregnancy for individual: 26286627
79/8/202012/2/1978286180212/1/202011  
810/28/20202/18/19952501615112/2/202002  
99/10/20202/18/19952501615112/2/202002  
107/22/20202/18/19952501615112/2/202012  
1111/5/202011/8/19882628184412/2/202001  
1210/21/202011/8/19882628184412/2/202001  
1310/8/202011/8/19882628184412/2/202001  
149/10/202011/8/19882628184412/2/202011  
1511/6/202010/19/19942628265612/2/202001  
1610/23/202010/19/19942628265612/2/202011  
1711/2/20204/17/19882628662712/2/202001  
1810/6/20204/17/19882628662712/2/202001  
199/16/20204/17/19882628662712/2/202001  
207/15/20204/17/19882628662712/2/202011  
Sheet6 (3)
Cell Formulas
RangeFormula
F2:F20F2=IF(AND(C2<>C3),1,0)
G2:G20G2=SUM(IF(C$2:C20=C2,F$2:F20,0))
I2:I20I2=IFERROR(VLOOKUP(K2,C$2:G20,5),"")
J2:J20J2=IF(I2<>"",I2&IF(I2>1," pregnancies"," pregnancy")&" for individual: ","")
K2:K6K2=UNIQUE(C$2:C20)
Dynamic array formulas.
 
Last edited:
Upvote 0
Hi,

Thanks so much for your response.
Unfortunately that's not exactly what I want - it needs to be a running count of the number of times the UR occurs in the dataset - so the pregnancy ID in E2-4 should be 1 and then E8-10 should be 2.
I actually found a solution that works for me:

=MATCH($D2, UNIQUE(FILTER($D$2:$D$20, $C$2:$C$20 = $C2)), 0)

Will leave that here if anyone else has a similar problem in the future.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,909
Messages
6,122,189
Members
449,072
Latest member
DW Draft

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