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.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I'm not clear what the month/day problem is?

Isn't example 1 simply: =AVERAGE(FILTER(F6:F3655,(B6#>=J7)*(B6#<=K7)))
 
Upvote 0
I'm not clear on the issue either. I don't have FILTER available, but unless I am missing something, a solution with AVERAGEIFS looks like:
=IFERROR(AVERAGEIFS($F$6:$F$10,$B$6:$B$10,">="&J7,$B$6:$B$10,"<="&K7),"")
 
Upvote 0
maybe

+Fluff.xlsm
BCDEFGHIJK
1
2Example1
3StartEnd
4Month23
5DateMonthDayYearValueDay128
611/04/201041120103.825262712Year20192020
712/04/20104122010-2.643838346Date01/02/201928/03/2020
813/04/201041320109.95258901
914/04/201041420103.975197492DateAverage Value
1015/04/201041520105.928742595need dynamic formulas-0.063955777
1116/04/20104162010-2.250015218
1217/04/201041720106.249622423
1318/04/20104182010-4.73278813
List
Cell Formulas
RangeFormula
B6:B3655B6=SEQUENCE(3650,,40279)
C6:C3655C6=MONTH(B6#)
D6:D3655D6=DAY(B6#)
E6:E3655E6=YEAR(B6#)
F6:F3655F6=RANDARRAY(ROWS(B6#),1,-10,10)
J7:K7J7=DATE(J6,J4,J5)
K10K10=AVERAGE(FILTER(F6#,(B6#>=J7)*(B6#<=K7)))
Dynamic array formulas.
 
Upvote 0
Thank you for your responses Stephen, Fluff and Rice.

I made a mistake by including that DATE formula in row7 for each example. Many apologies! I should have been clearer.

What I want is the average value for a portion of a year across certain years. In Eg3, I want this period to be Nov1 to Jun16, but only for the years 2017-20. I'm not after the average between two Dates.
 
Upvote 0
Do you mean from 16 Jun to 1 Nov for each year?
 
Upvote 0
I do not. I mean the portion of each of those years between Nov and Jun.

So in Eg3, Nov2017, Dec2017, Jan2018 etc to Jun2018, then Nov2018, Dec2018, Jan2019 etc to Jun2019, and onward until it's covered.

Think about analysing the weather over winter for each of the years of interest. Winter in my locale starts in Nov and doesn't go away until June.
 
Upvote 0
In that case I have no idea.
 
Upvote 0
Try this...I didn't use FILTER, but if this is what you want, a FILTER version could be created.
mrexcel_20200408.xlsx
BCDEFHIJKLMNOPQRSTUVWX
1
2Example1Example2Example3Example4
3StartEndStartEndStartEndStartEnd
4Month41145111102
5DateMonthDayYearValueDay1114207616239
64/11/20104112010-10Year20102013201120152017202020192020
74/12/201041220108Date4/11/201011/14/20134/20/20115/7/201511/6/20171/16/202010/23/20192/9/2020
84/13/20104132010-7
94/14/201041420104Start DateEnd DateAvg ValueStart DateEnd DateAvg ValueStart DateEnd DateAvg ValueStart DateEnd DateAvg Value
104/15/2010415201094/11/201011/14/20100.6654/20/20115/7/2011-0.11111/6/20171/16/2018-0.41710/23/20192/9/20200.664
114/16/2010104/11/201111/14/2011-0.3074/20/20125/7/20120.50011/6/20181/16/20190.056   
124/17/201064/11/201211/14/20120.2164/20/20135/7/20131.88911/6/20191/16/20200.444   
134/18/2010-24/11/201311/14/20130.2844/20/20145/7/20141.889      
144/19/20105   4/20/20155/7/2015-1.000      
154/20/20106            
164/21/20105            
174/22/20106            
184/23/20106            
194/24/2010-10            
204/25/20103
214/26/20101
224/27/2010-3
234/28/20109
244/29/2010-8
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)
J10,V10,R10,N10J10=J$7
K10,W10,S10,O10K10=DATE(MAX(IFERROR(DATEDIF(J$7,DATE(J$6,K$4,K$5),"d"),J$6+1),J$6),K$4,K$5)
X10:X19,T10:T19,P10:P19,L10:L19L10=IFERROR(AVERAGEIFS($F$6:$F$3659,$B$6:$B$3659,">="&J10,$B$6:$B$3659,"<="&K10),"")
V11:V19,R11:R19,N11:N19,J11:J19J11=IF(OR(K10=K$7,K10=""),"",DATE(YEAR(J10)+1,MONTH(J10),DAY(J10)))
W11:W19,S11:S19,O11:O19,K11:K19K11=IF(J11="","",DATE(YEAR(K10)+1,MONTH(K10),DAY(K10)))
F6:F24F6=RANDBETWEEN(-10,10)
 
Upvote 0
Here is a slightly revised approach for the K10 formula. I didn't like comparing the number of days between two dates to a year...it works okay because of how the check is done to determine whether the month and day specified for the end date span into the next year, but this approach is a little cleaner. It sets a large dummy value if that condition is met, and then depending on whether the dummy value is generated, the year is set as either the same as the start year or the following year.

The revised version for cells K10,W10,S10,O10, pasted into K10 and then copied into the other cells mentioned:
=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)
 
Upvote 0

Forum statistics

Threads
1,215,491
Messages
6,125,101
Members
449,205
Latest member
ralemanygarcia

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