Taking the difference between dates in a table that can be filtered

Derick_T

New Member
Joined
Mar 30, 2019
Messages
11
Hello,
Iam trying to calculate the days between failures in column E. My goal is to beable to take the difference between the respective date in column A and get thedifference between the date previous cell.


Theproblem I am running into is being able to do this considering I want to beable to filter by "Site" (column B). All my failure dates aresequential across multiple sites (column A data). My existing formula workswhen trying to get the difference between days for all sites but not when I filterby sites.




Just an added piece of information in case it helps understand the question alittle better or even some insight into why I am focused on getting thedifference between failures in Column A; I am then turning around and gettingthe column E average (in column F) which would represent the average daysbetween failures for the specific event I am analyzing. Being able to filter bysite (column B) is important so I can compare the different average daysbetween failures across the different sites.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi Derick,

With your site criteria it may be easier to extract records first then calculate;


Book1
ABCDEFG
1Date FailureSiteStart Date3/01/2017
23/01/2017AEnd Date19/01/2017
33/01/2017ACount Records9
45/01/2017CSiteAverageAvg Excluding Zero
55/01/2017CC1.4444444442.166666667
66/01/2017A
76/01/2017CDate FailureSiteCalc days betweenDays Between
86/01/2017C5/01/2017C01.444444444
99/01/2017A5/01/2017C01.444444444
109/01/2017A6/01/2017C11.444444444
119/01/2017A6/01/2017C01.444444444
129/01/2017A11/01/2017C51.444444444
139/01/2017A13/01/2017C21.444444444
149/01/2017A15/01/2017C21.444444444
1511/01/2017C17/01/2017C21.444444444
1613/01/2017C18/01/2017C11.444444444
1715/01/2017A
1815/01/2017C
1917/01/2017C
2018/01/2017C
2119/01/2017A
2219/01/2017A
Sheet1
Cell Formulas
RangeFormula
E3=COUNTIFS($A$2:$A$22,">="&$E$1,$A$2:$A$22,"<="&$E$2,$B$2:$B$22,$D$5)
E8{=IF(ROWS($D$8:E8)>$E$3,"",INDEX(B$2:B$22,SMALL(IF($B$2:$B$22=$D$5,ROW($A$2:$A$22)-ROW($A$2)+1),ROWS($D$8:E8))))}
F5=AVERAGEIFS($F$8:$F$21,$D$8:$D$21,">="&$E$1,$D$8:$D$21,"<="&$E$2)
F9=IF(D9="","",D9-D8)
G5=AVERAGEIF($F$8:$F$21,"<>0")
G8=IF(F8="","",AVERAGE($F$8:$F$21))
D8{=IF(ROWS($D$8:D8)>$E$3,"",INDEX(A$2:A$22,SMALL(IF($B$2:$B$22=$D$5,ROW($A$2:$A$22)-ROW($A$2)+1),ROWS($D$8:D8))))}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Thank you RasGhul! You understood the application perfectly and I even like your idea about averaging with and without 0's included.

A follow up question:

My data comes from a rather large dynamic table, Table1, which is on another tab in my workbook. This table constantly has data being added to it.

That said, is there someway I can extract the records directly from Table1, without having to know the finite row # where Table1 ends?

I guess a simpler, perhaps more direct way of asking my question is; how would the array formulas look if I wanted to reference Table1, with the specific column headers, Date Failure and Site just as below.
 
Upvote 0
Ok same formulas with table referencing, so now the array formulas will calculate each time new data is added to table1.

Note though that the array formula needs to be copied down far enough to return all possible records. eg. if Site A has the most rows with 5000 lines, then I would drag the formula down to say 10000 to cover future data. So it doesn't matter how much data gets added to table1 the array formula area needs to cover total possible rows. But then you can also convert this whole area into eg table2 for the Count records, avg, & avgif calculations.


Book1
ABCDEFG
1Date FailureSiteStart Date03-01-17
203-01-17AEnd Date19-01-17
303-01-17ACount Records12
405-01-17CSiteAverageAvg Excluding Zero
505-01-17CA1.3333333334
606-01-17A
706-01-17CDate FailureSiteCalc days betweenDays Between
806-01-17C03-01-17A01.333333333
909-01-17A03-01-17A01.333333333
1009-01-17A06-01-17A31.333333333
1109-01-17A09-01-17A31.333333333
1209-01-17A09-01-17A01.333333333
1309-01-17A09-01-17A01.333333333
1409-01-17A09-01-17A01.333333333
1511-01-17C09-01-17A01.333333333
1613-01-17C09-01-17A01.333333333
1715-01-17A15-01-17A61.333333333
1815-01-17C19-01-17A41.333333333
1917-01-17C19-01-17A01.333333333
2018-01-17C
2119-01-17A
2219-01-17A
Sheet1
Cell Formulas
RangeFormula
E3=COUNTIFS(Table1[Date Failure],">="&$E$1,Table1[Date Failure],"<="&$E$2,Table1[Site],$D$5)
E8{=IF(ROWS($E$8:E8)>$E$3,"",INDEX(Table1[[Site]:[Site]],SMALL(IF(Table1[[Site]:[Site]]=$D$5,ROW(Table1[[Date Failure]:[Date Failure]])-ROW($A$2)+1),ROWS($D$8:D8))))}
F5=AVERAGEIFS(Table2[Calc days between],Table2[Date Failure],">="&$E$1,Table2[Date Failure],"<="&$E$2)
F9=IF(D9="","",D9-D8)
G5=AVERAGEIF(Table2[Calc days between],"<>0")
G8=IF(F8="","",AVERAGE([Calc days between]))
D8{=IF(ROWS($D$8:D8)>$E$3,"",INDEX(Table1[[Date Failure]:[Date Failure]],SMALL(IF(Table1[[Site]:[Site]]=$D$5,ROW(Table1[[Date Failure]:[Date Failure]])-ROW($A$2)+1),ROWS($D$8:D8))))}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Thanks.

I am not getting the formulas to work for me. I event went to the first example above (not trying to reference a Table on another worksheet) to try something more fundamental. The issues I am having when I apply those formulas are:
1. When I type A in D5, I get C in E8 and vice versa.
2. The first date referenced doesn't quite align to the correct value for either site. Using the exact scenario above I get 06-01-17 as my first event date for Site A (even though it's referencing site in the next column over, but that is also the wrong date for Site C).
3. I am not getting valid data when I copy the D8 and E8 formulas down. I just get an invalid number (#NUM!) reference.

What I am seeing when I apply all the formulas:

Date FailureSiteStart Date
AEnd Date
ACount Records
CSite
CA
A
CDate FailureSiteCalc days betweenDays Between
CC
A
#NUM!
A
#NUM!
A
#NUM!
A
#NUM!
A
#NUM!
A
#NUM!
C
#NUM!
C
#NUM!
A
#NUM!
C
#NUM!
C
#NUM!
C
A
A

<thead style="border-collapse: collapse; border-spacing: 0px 0px; font-size: 13px; margin-bottom: 0px;">
[TH="align: center"][/TH]
[TH="align: center"]A[/TH]
[TH="align: center"]B[/TH]
[TH="align: center"]C[/TH]
[TH="align: center"]D[/TH]
[TH="align: center"]E[/TH]
[TH="align: center"]F[/TH]
[TH="align: center"]G[/TH]

</thead><tbody style="border-collapse: collapse; border-spacing: 0px 0px; font-size: 13px; margin-bottom: 0px;">
[TD="align: center"]1[/TD]

[TD="align: right"][/TD]

[TD="align: right"]03-01-17[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]2[/TD]
[TD="align: right"]03-01-17[/TD]

[TD="align: right"][/TD]

[TD="align: right"]19-01-17[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]3[/TD]
[TD="align: right"]03-01-17[/TD]

[TD="align: right"][/TD]

[TD="align: right"]12[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]4[/TD]
[TD="align: right"]05-01-17[/TD]

[TD="align: right"][/TD]

[TD="align: right"][/TD]
[TD="align: right"]Average[/TD]
[TD="align: right"]Avg Excluding Zero[/TD]

[TD="align: center"]5[/TD]
[TD="align: right"]05-01-17[/TD]

[TD="align: right"][/TD]

[TD="align: right"][/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]

[TD="align: center"]6[/TD]
[TD="align: right"]06-01-17[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]7[/TD]
[TD="align: right"]06-01-17[/TD]

[TD="align: right"][/TD]

[TD="align: center"]8[/TD]
[TD="align: right"]06-01-17[/TD]

[TD="align: right"][/TD]
[TD="align: right"]06-01-17[/TD]

[TD="align: right"]#NUM![/TD]
[TD="align: right"]
#NUM!
[/TD]

[TD="align: center"]9[/TD]
[TD="align: right"]09-01-17[/TD]

[TD="align: right"][/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]

[TD="align: center"]10[/TD]
[TD="align: right"]09-01-17[/TD]

[TD="align: right"][/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]

[TD="align: center"]11[/TD]
[TD="align: right"]09-01-17[/TD]

[TD="align: right"][/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]

[TD="align: center"]12[/TD]
[TD="align: right"]09-01-17[/TD]

[TD="align: right"][/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]

[TD="align: center"]13[/TD]
[TD="align: right"]09-01-17[/TD]

[TD="align: right"][/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]

[TD="align: center"]14[/TD]
[TD="align: right"]09-01-17[/TD]

[TD="align: right"][/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]

[TD="align: center"]15[/TD]
[TD="align: right"]11-01-17[/TD]

[TD="align: right"][/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]

[TD="align: center"]16[/TD]
[TD="align: right"]13-01-17[/TD]

[TD="align: right"][/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]

[TD="align: center"]17[/TD]
[TD="align: right"]15-01-17[/TD]

[TD="align: right"][/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]

[TD="align: center"]18[/TD]
[TD="align: right"]15-01-17[/TD]

[TD="align: right"][/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]

[TD="align: center"]19[/TD]
[TD="align: right"]17-01-17[/TD]

[TD="align: right"][/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]
[TD="align: right"]
#NUM!
[/TD]

[TD="align: center"]20[/TD]
[TD="align: right"]18-01-17[/TD]

[TD="align: right"][/TD]

[TD="align: center"]21[/TD]
[TD="align: right"]19-01-17[/TD]

[TD="align: right"][/TD]

[TD="align: center"]22[/TD]
[TD="align: right"]19-01-17[/TD]

[TD="align: right"][/TD]

[TD="align: right"][/TD]

</tbody>

I've been over the formulas a couple of times, and I honestly can't wrap my head around the second if statement in the Array Formulas, so it's hard for me to dissect where the issue could possible lie.

Along those same lines, and if I'm able to get the above to work (moving onto the second version where I am trying to reference Table1) what is the purpose of the underlined parts in the array formula below?

Array Formulas[TABLE="width: 100%"]
<thead style="border-collapse: collapse; border-spacing: 0px 0px; font-size: 13px; margin-bottom: 0px;">[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]
[TH="width: 10, align: center"]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
</thead><tbody style="border-collapse: collapse; border-spacing: 0px 0px; font-size: 13px; margin-bottom: 0px;">[TR]
[TH="width: 10, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] , align: center"]D8[/TH]
[TD="align: left"]{=IF(ROWS($D$8:D8)>$E$3,"",INDEX(Table1[[Date Failure]:[Date Failure]],SMALL(IF(Table1[[Site]:[Site]]=$D$5,ROW(Table1[[Date Failure]:[Date Failure]])-ROW($A$2)+1),ROWS($D$8:D8))))}[/TD]
[/TR]
[TR]
[TH="width: 10, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] , align: center"]E8[/TH]
[TD="align: left"]{=IF(ROWS($E$8:E8)>$E$3,"",INDEX(Table1[[Site]:[Site]],SMALL(IF(Table1[[Site]:[Site]]=$D$5,ROW(Table1[[Date Failure]:[Date Failure]])-ROW($A$2)+1),ROWS($D$8:D8))))}[/TD]
[/TR]
</tbody>[/TABLE]


I ask because, now that I am going to extract the data from my original table, Table1, and essentially eliminate the need for having the copied that I was having to bring in manually, I'm not exactly sure what $A$2 will be referencing?

Should it perhaps be referencing a cell in Table 1?
 
Upvote 0
Hi,

Array formula with table referencing is written slightly differently to normal ranges and the row(a2)+1 was skewing the search area;


Book1
ABCDEF
1Start Date3/01/2017Site List
2End Date19/01/2017A
3Count Records12B
4SiteAverageAvg Excluding ZeroC
5A1.3333333334
6
7Date FailureSiteCalc days betweenDays Between
83/01/2017A01.333333333
93/01/2017A01.333333333
106/01/2017A31.333333333
119/01/2017A31.333333333
129/01/2017A01.333333333
139/01/2017A01.333333333
149/01/2017A01.333333333
159/01/2017A01.333333333
169/01/2017A01.333333333
1715/01/2017A61.333333333
1819/01/2017A41.333333333
1919/01/2017A01.333333333
Sheet2
Cell Formulas
RangeFormula
B3=COUNTIFS(Table1[Date Failure],">="&$B$1,Table1[Date Failure],"<="&$B$2,Table1[Site],$A$5)
B8{=IF(ROWS(B$7:B7)>$B$3,"",INDEX(Table1[Site],SMALL(IF(Table1[Site]=$A$5,ROW(Table1[Date Failure])-ROW(Table1[#Headers])),ROWS(B$7:B7))))}
C5=IF(A5="","",AVERAGEIFS(Table24[Calc days between],Table24[Date Failure],">="&$B$1,Table24[Date Failure],"<="&$B$2))
C9=IF(A9="","",A9-A8)
D5=IF(A5="","",AVERAGEIF(Table24[Calc days between],"<>0"))
D8=IF(C8="","",AVERAGE([Calc days between]))
A8{=IF(ROWS(A$7:A7)>$B$3,"",INDEX(Table1[Date Failure],SMALL(IF(Table1[Site]=$A$5,ROW(Table1[Date Failure])-ROW(Table1[#Headers])),ROWS(A$7:A7))))}
Press CTRL+SHIFT+ENTER to enter array formulas.


Here's a link to my mock up if this helps;

https://www.dropbox.com/s/4dyqk8ba9... failure sites data_derick_t_tables.xlsx?dl=0
 
Upvote 0
That almost works perfectly.

Only oddball thing I see happening is that I have to start the range of dates from 1/1/2017 or my correct Dates doesn't show-up in the Dates column (Table24), which as a result throws off my Average formula. The dates always show starting in January 2017 even if I select a range for 2018 or 2019 time frame.

The Counts formula seems to be right on though.

Link to the file: https://drive.google.com/file/d/0B1mgDeo6i5j2SHpfSmRfb0RMVmVjM2xYdk1MenJHQXJaNGc4/view?usp=sharing

Thanks for all the help!
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

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