Dynamic Formula to extract data before and after dates

DRSteele

Well-known Member
Joined
Mar 31, 2015
Messages
2,640
Office Version
  1. 365
Platform
  1. Windows
Can someone please help me?

I am looking to create a formula using function FILTER to extract data using dates as criteria. As you can see, it is not as easy as expected. The problems arise when the Start Month is a higher value than the End Month because it wraps around the year and when the Start Day is a higher value than the End Day because it wraps around a month.

I want to use function FILTER to extract and report the filtered Dates and the average of Value for each of the four examples.

[The formula in F10 obviously goes all the way down. I did not paste all the data here.]

MrExcel posts18.xlsx
BCDEFGHIJKLMNOPQRST
1
2Example1Example2Example3Example4
3StartEndStartEndStartEndStartEnd
4Month2345111102
5DateMonthDayYearValueDay128207616239
64/11/20104112010-10Year20192020200520092017202020192020
74/12/201041220102Date2/1/20193/28/20204/20/20055/7/200911/6/20171/16/202010/23/20192/9/2020
84/13/20104132010-4
94/14/20104142010-9DateAverage ValueDateAverage ValueDateAverage ValueDateAverage Value
104/15/201041520100need dynamic formulasneed dynamic formulasneed dynamic formulasneed dynamic formulas
Sheet11
Cell Formulas
RangeFormula
B6:B3655B6=SEQUENCE(3650,,40279)
C6:C3655C6=MONTH(B6#)
D6:D3655D6=DAY(B6#)
E6:E3655E6=YEAR(B6#)
J7:K7,S7:T7,P7:Q7,M7:N7J7=DATE(J6,J4,J5)
F6:F10F6=RANDBETWEEN(-10,10)
Dynamic array formulas.
 
Thanks Stephen.

Your algorithm does not seem to extract the dates for the periods in question. See how my example in Post#19 extracts the relevant dates from the list; specifically, all the dates between Dec25 and Jan2 for the dates starting Dec 25, 2017 and ending Jan 2, 2020. It will also work for other dates; e.g., if you pick Apr1 to Apr14 for the dates starting Apr 1, 2016 and ending Apr 14, 2020.
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Your algorithm does not seem to extract the dates for the periods in question ...

Like this?

ABCDEFGH
1StartDate25 Dec 19
2EndDate2 Jan 23
3
4DatesValues25 Dec 1925 Dec 2025 Dec 2125 Dec 22
520 Dec 19812 Jan 202 Jan 212 Jan 222 Jan 23
621 Dec 1946
722 Dec 1945Averages
823 Dec 198751.2252.8958.8966.56
924 Dec 1970OR51.2252.8958.8966.56
1025 Dec 1955
1126 Dec 1910Filter
1227 Dec 192825 Dec 19
1328 Dec 195326 Dec 19
1429 Dec 191027 Dec 19
1530 Dec 198628 Dec 19
1631 Dec 194129 Dec 19
171 Jan 208230 Dec 19
182 Jan 209631 Dec 19
193 Jan 20591 Jan 20
204 Jan 20242 Jan 20
2120 Dec 20525 Dec 20
2221 Dec 204826 Dec 20
2322 Dec 205627 Dec 20
2423 Dec 202528 Dec 20
2524 Dec 206929 Dec 20
2625 Dec 205730 Dec 20
2726 Dec 201231 Dec 20
2827 Dec 20171 Jan 21
2928 Dec 20762 Jan 21
3029 Dec 209725 Dec 21
3130 Dec 20126 Dec 21
3231 Dec 208427 Dec 21
331 Jan 219128 Dec 21
342 Jan 214129 Dec 21
353 Jan 21330 Dec 21
364 Jan 218231 Dec 21
3720 Dec 21691 Jan 22
3821 Dec 21772 Jan 22
3922 Dec 211025 Dec 22
4023 Dec 218826 Dec 22
4124 Dec 216227 Dec 22
4225 Dec 217928 Dec 22
4326 Dec 213229 Dec 22
4427 Dec 21430 Dec 22
4528 Dec 218731 Dec 22
4629 Dec 21871 Jan 23
4730 Dec 21642 Jan 23
4831 Dec 2160
491 Jan 2293
502 Jan 2224
513 Jan 2260
524 Jan 225
5320 Dec 2287
5421 Dec 2233
5522 Dec 2239
5623 Dec 2296
5724 Dec 2220
5825 Dec 2259
5926 Dec 2295
6027 Dec 2288
6128 Dec 2262
6229 Dec 2232
6330 Dec 2273
6431 Dec 2289
651 Jan 2346
662 Jan 2355
673 Jan 2390
684 Jan 2338
69
Sheet1
Cell Formulas
RangeFormula
E4:H4E4=EDATE(StartDate,SEQUENCE(,DATEDIF(StartDate,EndDate,"y")+1,0,12))
E5:H5E5=EDATE(EndDate,SEQUENCE(,DATEDIF(StartDate,EndDate,"y")+1,-12*DATEDIF(StartDate,EndDate,"y"),12))
E8:H8E8=IFERROR(MMULT(SEQUENCE(,ROWS(MyTable),,0),(MyTable[Dates]>=E4#)*(MyTable[Dates]<=E5#)*MyTable[Values])/MMULT(SEQUENCE(,ROWS(MyTable),,0),(MyTable[Dates]>=E4#)*(MyTable[Dates]<=E5#)),"-")
E9:H9E9=AVERAGE(FILTER(MyTable[Values],(MyTable[Dates]>=E4)*(MyTable[Dates]<=E5)))
E12:E47E12=FILTER(MyTable[Dates],MMULT((MyTable[Dates]>=E4#)*(MyTable[Dates]<=E5#),SEQUENCE(DATEDIF(StartDate,EndDate,"y")+1,,,0)))
Dynamic array formulas.
Named Ranges
NameRefers ToCells
EndDate=Sheet1!$F$2E12, E4:E5
StartDate=Sheet1!$F$1E12, E4:E5
 
Upvote 0
Yes, that produces the result. Thanks tons, Stephen. Now we have two ways!
 
Upvote 0
My post #16 offers a 3rd solution to obtain the desired sequential date segments in a single list, although the approach is clunky since it relies on some helper cells above to create start and end dates for each segment, and another helper column to provide an index for navigating those date segments. Your efficient solutions illustrate some advantages of the improved function set available in 365, which has me considering moving on to that version. Still, I wanted to mention this in case someone without 365 is attempting to do something similar.
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,726
Members
449,093
Latest member
Mnur

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