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 Rice.

What I really need is to extract and report the filtered Dates for each of the four examples. So for Eg3 I need a list of dates for every day in the period Nov2017, Dec2017, Jan2018 etc to Jun2018, then in the period between Nov2018, Dec2018, Jan2019 etc to Jun2019, and onward. After that, an average value for is obviously easy to compute.

New Eg5 should simplify:

MrExcel posts18.xlsx
VW
2Example5
3StartEnd
4121
5292
620192020
7
8
9DateAverage Value
10extracted dates-1.5
1112/29/185
1212/30/18-10
1312/31/18-8
141/01/19-10
151/02/197
1612/29/198
1712/30/19-3
1812/31/194
191/01/200
201/02/20-8
Sheet11
Cell Formulas
RangeFormula
W10W10=AVERAGE(W11:W20)
 
Last edited:
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I guess I still do not understand what you want. Let's just isolate on example three for now:

mrexcel_20200408.xlsx
RS
1
2Example3
3StartEnd
4111
5616
620172020
711/6/20171/16/2020
8
9Start DateEnd Date
1011/6/20171/16/2018
1111/6/20181/16/2019
1211/6/20191/16/2020
Sheet10
Cell Formulas
RangeFormula
R7:S7R7=DATE(R6,R4,R5)
R10R10=R$7
S10S10=DATE(IF(IFERROR(DATEDIF(R$7,DATE(R$6,S$4,S$5),"d"),9E+99)=9E+99,R$6+1,R$6),S$4,S$5)
R11:R12R11=IF(OR(S10=S$7,S10=""),"",DATE(YEAR(R10)+1,MONTH(R10),DAY(R10)))
S11:S12S11=IF(R11="","",DATE(YEAR(S10)+1,MONTH(S10),DAY(S10)))


Is it true that. in this specific case, you want three sets of information described by the start and end dates shown in the yellow cells above? I am confused by your last couple of posts where you mention Nov2017, Dec2017, etc. The start date shown is 6 Nov 2017. Do you want to ignore the day in November and include all of November 2017, or does "Nov2017" mean 6 Nov 2017 through 30 Nov 2017? The answer to these questions affects my next question, but I would like to clarify what information you would like to extract.

For any one set of information, do you want a direct listing of the date and its associated value for every day between x and y...where x and y are either the start and end dates I show in yellow cells (meaning 71 data points between 6 November and 16 January), or 91 data points between 1 November and 31 January.

And then you want to extract the same month/day range for every year described by your row 6 entries in the example blocks?
 
Upvote 0
Did you see my Example5, which I included for simplicity? (And V6 should be 2018. I'm confusing everyone - sorry.)

I want all the dates between the starts and ends for the years specified. As in Eg5, Eg3 means all the dates (official Excel Date Serial numbers) Nov 6 2017 through Jan 16 2018, Nov 6 2018 through Jan 16 2019, and Nov 6 2019 through Jan 16 2020, all in a column with their Values beside them.

I want all the dates between the starts and ends for the years specified. But maybe we can forget the years for now - let's just figure out how to do the dates for all the years in the Dataset.
 
Last edited:
Upvote 0
Sorry, I missed the details in Example 5. I see now that you do want a daily extraction for the month/day range. So the start and end dates in the yellow cells in my post #12 can be used to extract the data. I left the last version of my worksheet intact, but I expanded the number of columns to the right of example 3 to hold the extracted data sets. I also added some helper cells above each list of dates for each data set to define the start and end dates for that particular data set. For now, those helper cells are manually entered...I haven't figured out a clever way to automatically expand the table to accommodate the number of data sets that might be required, and to insert the start and end dates into every other column. The formulas to generate those date endpoints work okay, but placing them (in this case, in U5:U6, W5:W6, and Y5:Y6) and potentially expanding the number of columns needs to the resolved. I've left the data table out of my example, but you should be able to wire the formulas into yours, adjust any ranges, and generate something similar. Let me know if this is closer to the mark.

Cell Formulas
RangeFormula
U5U5=R10
U6U6=S10
W5W5=R11
W6W6=S11
Y5Y5=R12
Y6Y6=S12
R7:S7R7=DATE(R6,R4,R5)
V8,Z8,X8V8=SUBTOTAL(1,V10:V82)
R10R10=R$7
S10S10=DATE(IF(IFERROR(DATEDIF(R$7,DATE(R$6,S$4,S$5),"d"),9E+99)=9E+99,R$6+1,R$6),S$4,S$5)
T10:T19T10=IFERROR(AVERAGEIFS($F$6:$F$3659,$B$6:$B$3659,">="&R10,$B$6:$B$3659,"<="&S10),"")
R11:R19R11=IF(OR(S10=S$7,S10=""),"",DATE(YEAR(R10)+1,MONTH(R10),DAY(R10)))
S11:S19S11=IF(R11="","",DATE(YEAR(S10)+1,MONTH(S10),DAY(S10)))
U10,Y10,W10U10=U5
V10:V30,Z10:Z30,X10:X30V10=IFERROR(INDEX($F$6:$F$3659,MATCH(U10,$B$6:$B$3659,0)),"")
U11:U30,Y11:Y30,W11:W30U11=IF(U10<U$6,U$10+ROWS(U$10:U10),"")
 
Upvote 0
ABCDEFGH
1
2Date131 Oct 19
3Date215 Jan 23
4
5DateListValuesAverages
61 Dec 196
726 Dec 194531 Oct 1931 Oct 2031 Oct 2131 Oct 22
815 Jan 20615 Jan 2015 Jan 2115 Jan 2215 Jan 23
929 Feb 2012
103 Mar 203319.0023.0032.3322.67
111 Dec 2017
1226 Dec 2019
1315 Jan 2133
1428 Feb 2149
153 Mar 2116
161 Dec 2123
1726 Dec 2129
1815 Jan 2245
1928 Feb 2249
203 Mar 2230
2115 May 226
2218 Aug 2218
231 Dec 227
2426 Dec 2212
2515 Jan 2349
2628 Feb 231
273 Mar 236
Sheet1
Cell Formulas
RangeFormula
E7:H7E7=EDATE(Date1,SEQUENCE(,DATEDIF(Date1,Date2,"y")+1,0,12))
E8:H8E8=EDATE(DATE(YEAR(Date1)+(DATE(YEAR(Date1),MONTH(Date2),DAY(Date2))<Date1),MONTH(Date2),DAY(Date2)),SEQUENCE(,DATEDIF(Date1,Date2,"y")+1,0,12))
E10:H10E10=IFERROR(MMULT(SEQUENCE(,ROWS(DateList),,0),(DateList>=E7#)*(DateList<=E8#)*Values)/MMULT(SEQUENCE(,ROWS(DateList),,0),(DateList>=E7#)*(DateList<=E8#)),"-")
Dynamic array formulas.
Named Ranges
NameRefers ToCells
Date1=Sheet1!$B$2E7:E8
Date2=Sheet1!$B$3E7:E8
DateList=Sheet1!$B$6:$B$27E10
Values=Sheet1!$C$6:$C$27E10
 
Upvote 0
Here is one more attempt. I left the time block summary intact, and used the start/end dates in that list to construct long sequential lists of dates, similar to what you show in Example 5 of your post #11 (see my Example 2 below for comparison). I needed to add a helper column called "index" to help construct the sequential date lists. Finally, I pulled the values from the main data table and computed an overall average, shown in row 23. The averages of the individual date lists were shown previously in the summary blocks found in rows 10-22.

mrexcel_20200408.xlsx
ABCDEFGHIJKLMNOPQRSTUVWX
1
2Example1Example2Example3Example4
3StartEndStartEndStartEndStartEnd
4Month411121111102
5DateMonthDayYearValueDay1114292616239
64/11/201041120108Year20102013201820202017202020192020
74/12/201041220107Date4/11/201011/14/201312/29/20181/2/202011/6/20171/16/202010/23/20192/9/2020
84/13/20104132010-60.0163.1000.1390.527
94/14/201041420101Start DateEnd DateAvg ValueStart DateEnd DateAvg ValueStart DateEnd DateAvg ValueStart DateEnd DateAvg Value
104/15/2010415201084/11/201011/14/20100.62412/29/20181/2/20194.80011/6/20171/16/2018-0.48610/23/20192/9/20200.527
114/16/2010-34/11/201111/14/2011-0.40412/29/20191/2/20201.40011/6/20181/16/20190.458   
124/17/2010-64/11/201211/14/2012-0.454   11/6/20191/16/20200.444   
134/18/2010-94/11/201311/14/20130.298         
144/19/20108            
154/20/20108            
164/21/2010-3            
174/22/2010-10            
184/23/20103            
194/24/2010-5            
204/25/201010            
214/26/20100            
224/27/20108            
234/28/2010100.016<-Avg3.100<-Avg0.139<-Avg0.527<-Avg
244/29/20100DateValueIndexDateValueIndexDateValueIndexDateValueIndex
254/30/2010-104/11/20108112/29/20187111/6/2017-6110/23/201931
265/1/2010-34/12/20107112/30/20189111/7/20177110/24/2019-41
275/2/201064/13/2010-6112/31/20182111/8/2017-8110/25/2019101
285/3/201024/14/2010111/1/2019-4111/9/2017-9110/26/201951
295/4/2010104/15/2010811/2/201910111/10/2017-9110/27/2019-101
305/5/201044/16/2010-3112/29/2019-4211/11/2017-10110/28/201961
315/6/2010-14/17/2010-6112/30/20196211/12/2017-5110/29/201921
325/7/201044/18/2010-9112/31/20190211/13/2017-9110/30/201991
335/8/201014/19/2010811/1/20204211/14/201710110/31/201941
345/9/201044/20/2010811/2/20201211/15/20171111/1/2019-41
355/10/2010-14/21/2010-31  311/16/20175111/2/2019-71
365/11/201074/22/2010-101  411/17/201710111/3/2019-101
375/12/201024/23/201031  511/18/20178111/4/2019-61
385/13/201004/24/2010-5111/19/2017-3111/5/201931
395/14/2010-94/25/201010111/20/20179111/6/201971
405/15/2010104/26/20100111/21/2017-4111/7/2019-41
415/16/201024/27/20108111/22/20173111/8/201921
Sheet10
Cell Formulas
RangeFormula
C6:C10C6=MONTH(B6)
D6:D10D6=DAY(B6)
E6:E10E6=YEAR(B6)
V7:W7,R7:S7,N7:O7,J7:K7J7=DATE(J6,J4,J5)
L8,X8,T8,P8L8=AVERAGE(L10:L22)
J10,V10,R10,N10J10=J$7
V11:V22,R11:R22,N11:N22,J11:J22J11=IF(OR(K10=K$7,K10=""),"",DATE(YEAR(J10)+1,MONTH(J10),DAY(J10)))
X10:X22,T10:T22,P10:P22,L10:L22L10=IFERROR(AVERAGEIFS($F$6:$F$3659,$B$6:$B$3659,">="&J10,$B$6:$B$3659,"<="&K10),"")
K10,W10,S10,O10K10=DATE(IF(IFERROR(DATEDIF(J$7,DATE(J$6,K$4,K$5),"d"),9E+99)=9E+99,J$6+1,J$6),K$4,K$5)
W11:W22,S11:S22,O11:O22,K11:K22K11=IF(J11="","",DATE(YEAR(K10)+1,MONTH(K10),DAY(K10)))
K23,W23,S23,O23K23=SUBTOTAL(1,K25:K4000)
J25,V25,R25,N25J25=J10
K25:K41,W25:W41,S25:S41,O25:O37K25=IFERROR(INDEX($F$6:$F$3659,MATCH(J25,$B$6:$B$3659,0)),"")
V26:V41,R26:R41,N26:N37,J26:J41J26=IF(L25=0,"",IF(L25=L26,J25+1,OFFSET(J$9,L26,0)))
X26:X41,T26:T41,P26:P37,L26:L41L26=IF(J25=OFFSET(J$9,L25,1),IF(OFFSET(J$9,L25+1,0)=0,0,L25+1),L25)
F6:F41F6=RANDBETWEEN(-10,10)
 
Upvote 0
Rice, thank you immensely for all your efforts. I'm just trying to test all this and make sure it works right. I haven't forgotten about you......
 
Upvote 0
That's fine...please let me know how that goes. I ran a few checks on the setup by comparing the overall average (K23 in my post #16) to the average of the average values for each time period that repeats year after year in the time block summary (e.g., L8 in post #16)...normally taking the average of averages is ill-advised, but in this case, it's okay since each component is equally weighted (the number of days represented by each time period is the same, unless the time span of interest includes the end of February in a leap year, which would introduce a discrepancy). The L8 formula can be eliminated in the final setup, as could columns C, D, and E, as they are not used elsewhere (unless you are using them for something else).

I recommend focusing on setting up just one example block to get the ranges adjusted for your setup. And you might even want to do that in two steps: 1) set up just the time block summary first (e.g., J9:J22 in post #16), and confirm that the time period end points are correct, that the range references are correct, and that an average value is reported back for each period; and then in step 2) add in the lengthy sequential listing of dates and values. When doing this second step, note that the top two rows require special attention (J25:L26 in example 1 of post #16) in order to tie them into the time block summary list above. J25 references the first starting date in the time block summary, K25 pulls the value corresponding to the date in J25, and for the helper cell L25, you enter a "1" to establish that the starting date belongs to index 1 of the dates shown above in the time summary block. K26 is trivial, as it is pulled down from K25. Then L26 looks at the previous date (e.g., J25) to determine if the end of its corresponding time block has been reached...if so, then the index is increased by 1 to move to the next time block, and if not, the index remains unchanged so that the next date in the sequence can be generated. Then J26 either increments the date by 1 day, or it jumps to the stating date of the next time block, depending on the value in the L26 helper cell. Once these two rows are set up, select K26:L26 and pull down until no more valid dates are generated.
 
Upvote 0
I think I got it to work. This extracts all the dates and other data from the periods desired and spills them down in one column. Here, I wanted to extract all the data over Christmas for the years 2017 to 2020.

The raw data is in an Excel Table called T_Data, which resides in B5. [It's the same data as before without the SEQUENCE and RANDBETWEEN formulas, which I put there so everyone could re-create the data easily.]

There is another Table called T_Parameters which resides in I3; it contains the conditions.

The dynamic array formula has an IF statement because the appropriate FILTER formula depends upon whether the Start month is a larger number than the End month. Notice there is a new stat I created to ease formula construction; it's a conglomeration of the month + day/100.

Change the values in J4 to L5 and everything should function right. There will be an error if the Year set is illogical.

I would be very grateful if I could try all your patience a little more to test this out. Thanks ever so much for everything already.

MrExcel posts18.xlsx
BCDEFGHIJKLMN
3PeriodMonthDayYearDateMD
4Start1225201712/25/201712.25
5DateMonthDayYearValueMDEnd1220201/2/20201.02
64/11/104112010-34.11
74/12/104122010-104.12DateMonthDayYearValueMD
84/13/10413201004.1312/25/201712252017412.25
94/14/10414201004.1412/26/201712262017412.26
104/15/10415201044.1512/27/201712272017-512.27
114/16/104162010-24.1612/28/201712282017912.28
124/17/104172010-64.1712/29/201712292017412.29
134/18/10418201034.1812/30/201712302017412.3
144/19/10419201094.1912/31/201712312017-612.31
154/20/104202010-94.201/1/2018112018-91.01
164/21/104212010104.211/2/2018122018-21.02
174/22/104222010-64.2212/25/201812252018-612.25
184/23/10423201094.2312/26/201812262018812.26
194/24/104242010-94.2412/27/201812272018612.27
204/25/10425201024.2512/28/201812282018412.28
214/26/104262010-94.2612/29/201812292018-212.29
224/27/104272010-84.2712/30/201812302018-412.3
234/28/104282010-74.2812/31/201812312018412.31
244/29/10429201064.291/1/201911201921.01
254/30/104302010-64.301/2/2019122019-41.02
265/1/1051201065.0112/25/201912252019812.25
275/2/10522010-65.0212/26/201912262019-112.26
285/3/1053201025.0312/27/201912272019612.27
295/4/1054201035.0412/28/2019122820191012.28
305/5/1055201095.0512/29/201912292019412.29
315/6/10562010-95.0612/30/2019123020191012.3
325/7/1057201015.0712/31/2019123120191012.31
335/8/1058201055.081/1/2020112020-81.01
345/9/10592010-55.091/2/2020122020-101.02
355/10/105102010-35.10
Sheet11 (2)
Cell Formulas
RangeFormula
M4:M5M4=DATE(L4,J4,K4)
N4:N5,G6:G35N4=[@Month]+[@Day]/100
I8:N34I8=IF(J4>J5,FILTER(T_Data,((T_Data[MD]>=N4)+(T_Data[MD]<=N5)*(T_Data[Year]>=L4))*(T_Data[Year]<=L5)*(T_Data[Date]>=M4)*(T_Data[Date]<=M5)),FILTER(T_Data,(T_Data[MD]>=N4)*(T_Data[MD]<=N5)*(T_Data[Year]>=L4)*(T_Data[Year]<=L5)*(T_Data[Date]>=M4)*(T_Data[Date]<=M5)))
Dynamic array formulas.
 
Upvote 0
Hi Don, I'm not sure whether you saw Post #15?

I've since simplified the formula for the period end dates, and these now adjust correctly for leap years. Here are two different ways to get the desired results:

ABCDEFGHIJK
1
2Date123 Dec 20
3Date215 Jan 23
4
5DateListValuesAverages
61 Dec 1981
726 Dec 194623 Dec 2023 Dec 2123 Dec 22
829 Feb 204515 Jan 2115 Jan 2215 Jan 23
91 Dec 2087
1026 Dec 207045.0063.5051.8045.0063.5051.80
1112 Jan 2155
1215 Jan 2110DatesDates
1328 Feb 212826 Dec 2026 Dec 2026 Dec 2126 Dec 22
143 Mar 215312 Jan 2112 Jan 2115 Jan 2227 Dec 22
151 Dec 211015 Jan 2115 Jan 2128 Dec 22
1626 Dec 218626 Dec 2129 Dec 22
1715 Jan 224115 Jan 2215 Jan 23
1828 Feb 228226 Dec 22
193 Mar 229627 Dec 22
204 Mar 225928 Dec 22
2110 Mar 222429 Dec 22
2211 Mar 22515 Jan 23
2314 Mar 2248
2415 May 2256
2518 Aug 2225
261 Dec 2269
2726 Dec 2257
2827 Dec 2212
2928 Dec 2217
3029 Dec 2276
3115 Jan 2397
3228 Feb 231
333 Mar 2384
Sheet1
Cell Formulas
RangeFormula
E7:G7E7=EDATE(Date1,SEQUENCE(,DATEDIF(Date1,Date2,"y")+1,0,12))
E8:G8E8=EDATE(Date2,SEQUENCE(,DATEDIF(Date1,Date2,"y")+1,-12*DATEDIF(Date1,Date2,"y"),12))
E10:G10E10=IFERROR(MMULT(SEQUENCE(,ROWS(DateList),,0),(DateList>=E7#)*(DateList<=E8#)*Values)/MMULT(SEQUENCE(,ROWS(DateList),,0),(DateList>=E7#)*(DateList<=E8#)),"-")
I10:K10I10=AVERAGE(FILTER(Values,(DateList>=E7)*(DateList<=E8)))
E13:E22E13=FILTER(DateList,MMULT((DateList>=E7#)*(DateList<=E8#),SEQUENCE(DATEDIF(Date1,Date2,"y")+1,,,0)))
I13:I15,K13:K17,J13:J14I13=FILTER(DateList,(DateList>=E7)*(DateList<=E8))
Dynamic array formulas.
Named Ranges
NameRefers ToCells
Date1=Sheet1!$B$2E13, E7:E8
Date2=Sheet1!$B$3E13, E7:E8
DateList=Sheet1!$B$6:$B$33I13:K13, I10:K10, E13, E10
Values=Sheet1!$C$6:$C$33I10:K10, E10
 
Upvote 0

Forum statistics

Threads
1,215,659
Messages
6,126,077
Members
449,286
Latest member
Lantern

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