Find matching variable in one column and then run date difference function according to the matching rows

filmfanatic1970

New Member
Joined
Mar 11, 2022
Messages
5
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I'm not sure if this can be done, but I am trying to get the day difference between two dates. The catch is that I'm looking to match multiple text in the first column and then run a day difference function for the first two matches...and then the next two matches, etc. through the entire sheet.

There are three variables in column A: P1, P2 and P3.

In the example below, I want to match the first P3 and then the next. Then, grab the two dates for the first match in column C and run a day difference function (In D5, I put =DAYS(D5,D3), because A5 and A3 match. It comes up with 2.0. Then I want to match the next P3, after the one in A5, which is A9. The day difference is shown as 5.0.

1647030823548.png


I'm trying to do the same thing for all P1, P2 and P3 matches, all the way down column A. Going forward, as an example, if I were to type in a P2 in A13 and put a date in column C, then it would match with the previous P2 in A8 and calculate the day difference between row 8 and row 13.

I don't think this can be done with traditional formulas and was thinking it needs to be done with VBA.

I hope this makes sense.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Welcome to the Board! There are several ways to do this with formulas. Here is one approach that works with Excel 365. I don't think you want 0 returned for MT to Failure if a match is not found above, as that will skew your average. In this example, I've left unmatched entries as blanks, which are then ignored by the AVERAGE function:
MrExcel_20220313.xlsx
ABCDE
147.875
2LevelTitleInitial/DateTimeMT To FailureEndDateTime
3P3Title12/2/2022 11:10 2/2/2022
4P2Title22/2/2022 13:05 2/3/2022
5P3Title32/4/2022 14:1022/7/2022
6P2Title42/4/2022 15:2222/7/2022
7P2Title52/6/2022 22:1522/7/2022
8P2Title62/7/2022 8:5012/15/2022
9P3Title72/9/2022 11:3552/10/2022
10P3Title82/11/2022 15:3522/15/2022
11P3Title92/15/2022 12:1542/15/2022
12P1Title103/7/2022 7:30 3/7/2022
13P2Title112/7/2023365
film
Cell Formulas
RangeFormula
D1D1=AVERAGE(D3:D13)
D3:D13D3=IFERROR(DAYS(C3,XLOOKUP(A3,A$2:A2,C$2:C2,"",0,-1)),"")
 
Last edited:
Upvote 0
Hi Kirk,

Thanks for the response.

I put the formulas in the corresponding cells and dragged D3 all the way down. The formulas don't seem to be working though. I used the XL2BB add-in and created a mini sheet. I pasted it below. Not sure if it came out correctly. If not, there is a screen shot below it showing what I see.

Incident Management Spreadsheet 2022_V3.0.xlsx
ABCDE
1#DIV/0!
2LevelTitleInitialDateTimeMT To FailureEndDateTime
3P3Title 12/2/2022 11:10 2/2/2022 18:00
4P2Title 22/2/2022 13:05 2/3/2022 8:50
5P3Title 22/4/2022 14:10 2/7/2022 10:25
6P22/4/2022 15:22 2/7/2022 11:41
7P22/6/2022 22:15 2/7/2022 11:00
8P22/7/2022 8:50 2/15/2022 17:00
9P32/9/2022 11:35 2/10/2022 10:44
10P32/11/2022 15:35 2/15/2022 9:33
11P32/15/2022 12:15 2/15/2022 15:15
12P13/7/2022 7:30 3/7/2022 10:56
MAIN
Cell Formulas
RangeFormula
D1D1=AVERAGE(D3:D13)
D3:D12D3=IFERROR(DAYS(C3,XLOOKUP(A3,A$2:A2,C$2:C2,"",0,-1)),"")
Named Ranges
NameRefers ToCells
query__1=MAIN!$A$2:$L$500D3:D12
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A3:A9,J501:L501,I502:L506,B3:G3,I3:XFD3,B4:C9,I4:J10,J11:J500,L101:L500,L4:XFD100,K4:K500,A507:L900,A501:G506,A10:C500,D4:G500Expression=$A3="P1"textNO



1647273756223.png
 
Upvote 0
Are you using Excel 365? XLOOKUP is a function available in 365, but not earlier versions of Excel. If you're using an earlier verion of Excel, try this:
MrExcel_20220313.xlsx
ABCDE
147.875
2LevelTitleInitial/DateTimeMT To FailureEndDateTime
3P3Title12/2/2022 11:10 2/2/2022
4P2Title22/2/2022 13:05 2/3/2022
5P3Title32/4/2022 14:1022/7/2022
6P2Title42/4/2022 15:2222/7/2022
7P2Title52/6/2022 22:1522/7/2022
8P2Title62/7/2022 8:5012/15/2022
9P3Title72/9/2022 11:3552/10/2022
10P3Title82/11/2022 15:3522/15/2022
11P3Title92/15/2022 12:1542/15/2022
12P1Title103/7/2022 7:30 3/7/2022
13P2Title112/7/2023365
film (2)
Cell Formulas
RangeFormula
D1D1=AVERAGE(D3:D13)
D3:D13D3=IFERROR(DAYS(C3,LOOKUP(2,1/(A$2:A2=A3),C$2:C2)),"")
 
Last edited:
Upvote 0
Ah....makes sense. Yes, we're using Office 2016 (moving soon to the cloud and Microsoft 365).

I think we are getting somewhere. I put the new formula in there, but still not getting the results in column D. See below. Seems like =AVERAGE(D3:D13) in D1 isn't working, correct?


1647278534764.png
 
Upvote 0
Can you post the mini-sheet produced by XL2BB so that I can see more details? I'm puzzled by the "FALSE" results in column D.
 
Upvote 0
Sorry about that. Here you go.

Incident Management Spreadsheet 2022_V4.0.xlsx
ABCD
1#DIV/0!
2LevelTitleInitialDateTimeMT To Failure
3P3Title 12/2/2022 11:10FALSE
4P2Title 22/2/2022 13:05FALSE
5P3Title 22/4/2022 14:10FALSE
6P22/4/2022 15:22FALSE
7P22/6/2022 22:15FALSE
8P22/7/2022 8:50FALSE
9P32/9/2022 11:35FALSE
10P32/11/2022 15:35FALSE
11P32/15/2022 12:15FALSE
12P13/7/2022 7:30FALSE
MAIN
Cell Formulas
RangeFormula
D1D1=AVERAGE(D3:D500)
D3:D12D3=IFERROR(DAYS(C3,LOOKUP(2,1/(A$2:A2=A3),C$2:C2)),"") =IFERROR(DAYS(C3,LOOKUP(2,1/(A$2:A2=A3),C$2:C2)),"") =IFERROR(DAYS(C3,LOOKUP(2,1/(A$2:A2=A3),C$2:C2)),"") =IFERROR(DAYS(C3,LOOKUP(2,1/(A$2:A2=A3),C$2:C2)),"")
Named Ranges
NameRefers ToCells
query__1=MAIN!$A$2:$F$500D3:D12
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A3:A9,B4:C9,A10:C500,D4:F500,A501:F900,G4:XFD100,B3:XFD3Expression=$A3="P1"textNO
 
Upvote 0
It appears that you've pasted the formula numerous times into each cell.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,025
Messages
6,122,734
Members
449,094
Latest member
dsharae57

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