Is this the best way to calculate the number of events since the last one?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,687
Office Version
  1. 365
Platform
  1. Windows
The minisheet below is my attempt to calculate the number of events (not the number of days) that have occurred since the last time that event occurred.

Col B has the events. Col C has the dates when they have occurred. Cols D & E are parts of the solution. D is the row # of this event. E is the row # of the previous occurrence of that event. Col F puts these together and adds IfError. Is there a better way?

Note: This depends on the table being sorted by date (newest to oldest).

Take Function.xlsx
BCDEFG
3EventDateRow #Row #(Next)# EventsComments
4A6/10/24144Event A last occurred 4 events ago.
5B6/08/24222Event B last occurred 2 events ago.
6C6/05/24333Event C last occurred 3 events ago.
7B6/02/24455Event B last occurred 5 events ago.
8A5/30/245#N/An/aThis is the first time event A occurred.
9C5/25/246#N/An/aThis is the first time event C occurred.
10D5/20/24711Event D last occurred 1 event ago.
11D5/19/248#N/An/aThis is the first time event D occurred.
12B5/10/249#N/An/aThis is the first time event B occurred.
Count Between
Cell Formulas
RangeFormula
D4:D12D4=ROW()-ROW(Table4[[#Headers],[Event]])
E4:E12E4=MATCH([@Event],DROP([Event],[@[Row '#]]),0)
F4:F12F4=IFERROR(MATCH([@Event],DROP([Event],ROW()-ROW(Table4[[#Headers],[Event]])),0),"n/a")
 
"only 1 Match" I mean there is only one MATCH function in my formula, a single MATCH solves the problem, do not need more functions and more complicate logic, maybe could be more easier to understand and maintain.

Aha! You mean there is only 1 function (besides IFNA), whereas I have XMatch, Row (twice), and Drop (plus IFERROR). Yes, that is simpler, but, alas, it doesn't work in the situations where I need it to work. Simple is good, but working is better. 😉👍
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Returning to the original question of your post, you asked whether there is a better solution, so I provided what I think is a better solution. i am not saying my solution is better, just offered for your reference.
 
Upvote 0
Aha! You mean there is only 1 function (besides IFNA), whereas I have XMatch, Row (twice), and Drop (plus IFERROR). Yes, that is simpler, but, alas, it doesn't work in the situations where I need it to work. Simple is good, but working is better. 😉👍
yes, i agree with you. Formulas are not necessarily better the shorter they are; universality and sufficient robustness are very important.
 
Upvote 0
Since your current formula works and is quite compact, I am struggling with what would be considered better.
The only thing I can think of is to make independant of the list being sorted but that might only confuse the user.

This is definitely not compact but does make in dependent of the sort.
Excel Formula:
=LET(currEvent,[@Event],
currDate,[@Date],
sortList,SORTBY(Table1[[Event]:[Date]],[Date],-1,[Event],1),
currRow,MATCH(1,(CHOOSECOLS(sortList,1)=currEvent)*(CHOOSECOLS(sortList,2)=currDate),0),
nextRow,MATCH(1,(CHOOSECOLS(sortList,1)=currEvent)*(CHOOSECOLS(sortList,2)<currDate),0),
IFNA(nextRow-currRow,"n/a"))
 
Upvote 0
Since your current formula works and is quite compact, I am struggling with what would be considered better.
The only thing I can think of is to make independant of the list being sorted but that might only confuse the user.

This is definitely not compact but does make in dependent of the sort.
Excel Formula:
=LET(currEvent,[@Event],
currDate,[@Date],
sortList,SORTBY(Table1[[Event]:[Date]],[Date],-1,[Event],1),
currRow,MATCH(1,(CHOOSECOLS(sortList,1)=currEvent)*(CHOOSECOLS(sortList,2)=currDate),0),
nextRow,MATCH(1,(CHOOSECOLS(sortList,1)=currEvent)*(CHOOSECOLS(sortList,2)<currDate),0),
IFNA(nextRow-currRow,"n/a"))

Wow! I would have to study that one. But since my list will always be sorted, I really don't need anything more general. And the "user" is just me, but then I am easily confused.

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,596
Members
452,657
Latest member
giadungthienduyen

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