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,676
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")
 
please refer to my post #6, the formula is:
H4=IFNA(MATCH(B4,B5:B$13,),"n/a")
copy and paste above formula in H4.
That is exactly what is in my H4. The rest of that column has a mix of that formula and one with a zero after the comma. Neither of them work.

But why have you not addressed the other errors? And why have you not responded to any of my other questions, such as whether your data is in a table and, if so, why you are not using table syntax.
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
That is exactly what is in my H4. The rest of that column has a mix of that formula and one with a zero after the comma. Neither of them work.

But why have you not addressed the other errors? And why have you not responded to any of my other questions, such as whether your data is in a table and, if so, why you are not using table syntax.
i copied your data and paste in my workbook, so the data is not a table
which you can use structured reference. if you can accept answers in other language, i could explain more. meanwhile, i am using cell phone now, i can get access to my computer in about one hour.
 
Upvote 0
i copied your data and paste in my workbook, so the data is not a table
which you can use structured reference. if you can accept answers in other language, i could explain more. meanwhile, i am using cell phone now, i can get access to my computer in about one hour.
My data is in a table, so I am only interested in a solution that will work in a table.

If this is because of a language problem, then I apologize. I don't speak another language well enough to do anything useful. And if you have done this from your cell phone, then I am impressed. But the solutions proposed do not work.

I have a solution that works. I was just asking if there might be a better way.
 
Upvote 0
My data is in a table, so I am only interested in a solution that will work in a table.

If this is because of a language problem, then I apologize. I don't speak another language well enough to do anything useful. And if you have done this from your cell phone, then I am impressed. But the solutions proposed do not work.

I have a solution that works. I was just asking if there might be a better way.
i made the formula yesterday on my computer, now i am preparing go to my office and using phone now. i will explain later, using table reference.
 
Upvote 0
Please see my formula in E4, because you add more rows, so the range of the second parameter should be change to the cell outof your table range, hilighted in red. This is used to create an error for the last Event when matching. if use B5:B$15, the last row will return 1 becuase the last match will be match(B15,B15:B16,0).

I entered my formula in table E4, and select apply the formula for the whole column.

1718152335953.png

工作簿1.xlsx
ABCDEFG
1
2
3EventDate(Good) Match Solution(Fails) Absolute Range w/MatchComments
4D2024-6-1188New row 1TRUE
5A2024-6-1033TRUE
6B2024-6-833TRUE
7C2024-6-544TRUE
8A2024-6-422New row in niddleTRUE
9B2024-6-255TRUE
10A2024-5-3055TRUE
11C2024-5-25n/an/aTRUE
12D2024-5-2011TRUE
13D2024-5-19n/an/aTRUE
14B2024-5-10n/an/aTRUE
15A2024-5-5n/an/aNew last rowTRUE
16
17AEvent text added outside table
18
19BEvent text added outside table
20
21CEvent text added outside table
22
23DEvent text added outside table
24
Sheet1
Cell Formulas
RangeFormula
D4:D15D4=IFERROR(MATCH([@Event],DROP([Event],ROW()-ROW(Table4[[#Headers],[Event]])),0),"n/a")
E4:E15E4=IFNA(MATCH([@Event],B5:B$16,),"n/a")
G4:G15G4=E4=D4
 
Upvote 0
some errors occures in your post #7 because the last cell reference in the second parameter of Match unchanged for the expanded table range, still kept as B$13.
 
Upvote 0
Ok, but is this better than my solution? It works in all scenarios.

Excel Formula:
=IFERROR(XMATCH([@Event],DROP([Event],ROW()-ROW(Table4[[#Headers],[Event]])),0),"n/a")
 
Upvote 0
Ok, but is this better than my solution? It works in all scenarios.

Excel Formula:
=IFERROR(XMATCH([@Event],DROP([Event],ROW()-ROW(Table4[[#Headers],[Event]])),0),"n/a")
I think it depends on each person's choice. If I use it in my work, I will use my formula because there is only one match, which is clearer and easier to maintain. most importantly, i will always get right answers because i know how and why the formula runs and return values. For a same problem, there are often many ways to solve it, and I think it is better for users to be able to grasp, apply, and maintain it.

The above content is automatically translated by the machine.
 
Upvote 0
I think it depends on each person's choice. If I use it in my work, I will use my formula because there is only one match, which is clearer and easier to maintain. most importantly, i will always get right answers because i know how and why the formula runs and return values. For a same problem, there are often many ways to solve it, and I think it is better for users to be able to grasp, apply, and maintain it.

The above content is automatically translated by the machine.
If it works for you, great. It does not work for me.

I don't know what you mean by "only 1 match". Mine gets only 1 match, too. And since it uses table syntax, it grows and shrinks with the table. It requires zero maintenance.

I still have a couple of questions:
  1. Is there a better way than what I came up with?
  2. Is there any reason to choose XMatch over Match?
  3. Is there any reason to choose IFNA over IFERROR?
 
Upvote 0
If it works for you, great. It does not work for me.

I don't know what you mean by "only 1 match". Mine gets only 1 match, too. And since it uses table syntax, it grows and shrinks with the table. It requires zero maintenance.

I still have a couple of questions:
  1. Is there a better way than what I came up with?
  2. Is there any reason to choose XMatch over Match?
  3. Is there any reason to choose IFNA over IFERROR?
"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.

my answers to your 3 questions:
1. i'm not sure how to answer ,because the standard about "better" really depends on everyone's like.
2.if you just make normal precise match, I think it is no difference between XMatch and Match. XMatch is a more powerfu match function, for example, if you want to match from the last to the first, XMatch is a good choice.
3.if the errors just come from there are no matching items for Match or Xmatch, so the errors must be #N/A, so IFNA could handle this error well. IFERROR could handle more errors and it certainly can deal with #N/A, in this case IFNA and IFERROR i don't see any difference. Since Microsoft has already invented the IFNA function, I think we can just use it to handle #N/A errors. Anyway, it seems a bit shorter.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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