Filtering for First and Last day of the Year or Month from Years of Workdate Data

LakeDog

New Member
Joined
Oct 18, 2017
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Hi, looking at historical stock data downloaded from Yahoo. It is basically 6 columns: Date, Open, High, Low, Close, AdjClose, Volume.

I am trying to Filter by the first date of the year and the last date of the year to get the Close. The issue I am having is that all the data is on working days, so it's not typically the 1st of the month. Actuallly, January 1st is always a holiday so it's the 2nd, 3rd or 4th of the month. Same with the last day day of the year, not all are the 31st and may even the last working day is the 28th of the month. I have used a formula to reduce it down by Filtering (using 365) for the DAY equal to or less than 3 and that helps, but want to clean it up to just the first working day of the year. Attached is an image of an example worksheet, but that formula is:

=FILTER(E:E,(MONTH(A:A)=1)*(DAY(A:A)<=3))

=FILTER(E:E,(MONTH(A:A)=1)*(DAY(A:A)=1),(DAY(A:A)=2))

=FILTER(E:E,(MONTH(A:A)=1)*(DAY(A:A)=2),(DAY(A:A)=3))

I have altered it to filter to the 2nd as well as the 3rd, as well as I have attempted multiple combined nested IF's, AND's and OR's without success to generate a single formula. Looking for any suggestions to what is likely a straight forward solution that I can no longer see. I've put those other formulas on the attached image.

I have tried multiple times but can not get 365 to allow me to let Xl2bb macros work even though MSFT trust center allows. It's loaded on my sheet so sorry, best I can do is an image of the start of the sheet. Understand, I am looking at 9347 rows of data, not just the few in the image.

Thanks!
 

Attachments

  • 2023-04-16_11-52-23.png
    2023-04-16_11-52-23.png
    103.6 KB · Views: 37
I'm filtering the "Close" data using the dates. Need the 1st value in A to be the first day in January shown for each year, obrtaining the corresponding Close value (Column E). Here is a manually generated table for the above data.

Sample File Filtering Dates.xlsx
NOPQR
15What I'm after (Manually calculated results):
16
17DateClosePriceDate:ClosePrice
18"No data"12/31/19860.1675
191/2/19870.165812/31/19870.3767
201/4/19880.388912/30/19880.3698
211/4/19890.3767etc
22
23**Note the 1/3/1989 in columns I and J.
Sheet1
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
And for the record MARK858, my error in XL2BB was apparently checking the "Properties" in the file XL2BB in Windows Explorer, not fully unzipped. When I saved it to another directory extracted, I found the needed checkbox.......just as you suggested. Thank you for all your help and efforts.
 
Upvote 0
Please note that my dates are in dd/mm/yyyy format, Column T is a list of Public holidays (also the 2nd of January 1989 was a Monday so if it is to be classed as a public holiday it needs adding to the list in column T but even then the first working day would be the 3rd [at least in the UK as we get 1 day in lieu of the 1st falling on a weekend] not the 4th)

Book1.xlsb
ABCDEFGHIJKLMNOPQRST
1OpenHighLowCloseAdj CloseVolume
226/12/19860.1714410.1727430.1710070.1710070.106691371520001/01/1986
329/12/19860.1710070.1727430.1640630.1640630.1023594170240001/01/1987
430/12/19860.1640630.1666670.1623260.1657990.1034422540160001/01/1988
531/12/19860.1657990.1701390.1657990.1675350.1045252335680001/01/1989
602/01/19870.1675350.1692710.1649310.1657990.1034421264320001/01/1990
705/01/19870.1657990.1770830.1649310.1753470.1093994849920001/01/1991
806/01/19870.1753470.1788190.1744790.1779510.1110234003200001/01/1992
907/01/19870.1779510.18750.1779510.18750.1169816099840001/01/1993
1008/01/19870.1883680.1953130.1883680.1935760.1207726148800001/01/1994
1109/01/19870.1935760.2022570.1927080.2013890.1256466370560001/01/1995
1212/01/19870.2013890.2161460.1996530.214410.1337712879360001/01/1996
1324/12/19870.3697920.3732640.3680560.3697920.2307131267200001/01/1997
1428/12/19870.3680560.3697920.3472220.3559030.2220475071680001/01/1998
1529/12/19870.3559030.3819440.3506940.3784720.2361287945920001/01/1999
1630/12/19870.3784720.3888890.3767360.3871530.2415447070400001/01/2000
1731/12/19870.3836810.3871530.3732640.3767360.2350456154560001/01/2001
1804/01/19880.3802080.3923610.3784720.3888890.242627110347200None0.1675401/01/2002
1905/01/19880.3975690.4027780.3923610.3958330.246961555200000.1657990.3767401/01/2003
2006/01/19880.3975690.4149310.3958330.406250.2534591216800000.3888890.3697901/01/2004
2107/01/19880.4027780.4210070.3993060.4201390.26212490676800None`None01/01/2005
2208/01/19880.4184030.4288190.3862850.3888890.242627145440000NoneNone01/01/2006
2311/01/19880.3871530.4027780.3802080.401910.25075114356800001/01/2007
2422/12/19880.3645830.3680560.3593750.3611110.2252977459200001/01/2008
2523/12/19880.3593750.3611110.3576390.3611110.2252971712160001/01/2009
2627/12/19880.3611110.3645830.3593750.3593750.2242133077280001/01/2010
2728/12/19880.3611110.3628470.3593750.3628470.226382126880001/01/2011
2829/12/19880.3611110.3715280.3611110.3697920.2307133827520001/01/2012
2930/12/19880.3715280.3767360.3697920.3697920.2307136382080001/01/2013
3003/01/19890.3715280.3732640.3663190.3723960.2323375182560001/01/2014
3104/01/19890.3715280.3810760.3715280.3767360.2350455389920001/01/2015
3201/01/2016
3301/01/2017
3401/01/2018
3501/01/2019
3601/01/2020
3701/01/2021
3801/01/2022
3901/01/2023
4001/01/2024
4101/01/2025
4201/01/2026
4301/01/2027
4401/01/2028
4501/01/2029
4601/01/2030
4701/01/2031
4801/01/2032
Sheet1
Cell Formulas
RangeFormula
O18:O22O18=FILTER(E:E,A:A=WORKDAY(DATE(ROW(Z1986),1,1)-1,1,$T$2:$T$48),"None")
R18:R22R18=FILTER(E:E,A:A=WORKDAY("1JAN"&(ROW(Z1986)+1),-1),"None")
 
Last edited:
Upvote 0
Solution
Major strides forward! Appreciate it. The formula for the last day of the year seems to work well. But I'm struggling with the first day of the year. It seems to work variably and I can't sort out why. I've attached the results I get using them in the entire database. Column O is the formula for the first day of the year and column R is the last day. Column N is the same first day formula filtering the date column. Similarly, Column Q is the last day formula filtering the date. The dates match the close prices. Tried several changes, but can't get the first day to work. Any thoughts?

Cell Formulas
RangeFormula
N2N2=FILTER(A:A,A:A=WORKDAY(DATE(ROW(X1986),1,1)-1,1,$T$2:$T$41))
O2:O34O2=FILTER(E:E,A:A=WORKDAY(DATE(ROW(X1986),1,1)-1,1,$S$2:$S$21),"None")
N3:N34N3=FILTER(A:A,A:A=WORKDAY(DATE(ROW(X1987),1,1)-1,1,$S$2:$S$21))
Q2:Q38Q2=FILTER(A:A,A:A=WORKDAY("1JAN"&(ROW(X1986)+1),-1),"None")
R2:R38R2=FILTER(E:E,A:A=WORKDAY("1JAN"&(ROW(X1986)+1),-1),"None")
 
Upvote 0
Oops, found one simple error. Corrected and it helps. Still missing something.

Cell Formulas
RangeFormula
N2:N34N2=FILTER(A:A,A:A=WORKDAY(DATE(ROW(X1986),1,1)-1,1,$T$2:$T$41))
O2:O34O2=FILTER(E:E,A:A=WORKDAY(DATE(ROW(X1986),1,1)-1,1,$T$2:$T$21),"None")
 
Upvote 0
Not home but have you added any dates that are holidays other than the 1st January to column T?
You don't appear to on my phone.
 
Upvote 0
No, I did check to make sure I had every year listed.
You need to add them (and adjust the range in the formula if necessary) as Excel has no way of knowing what is a public holiday in each country, as I put in post 13....

Column T is a list of Public holidays (also the 2nd of January 1989 was a Monday so if it is to be classed as a public holiday it needs adding to the list in column T but even then the first working day would be the 3rd [at least in the UK as we get 1 day in lieu of the 1st falling on a weekend] not the 4th)
 
Upvote 0
You need to add them (and adjust the range in the formula if necessary) as Excel has no way of knowing what is a public holiday in each country, as I put in post 13....
Yep, that is likely the issue.

But have you ever worked for days on an issue and them realized your perspective is wrong?? I sincerely appreciate MARK858's efforts and insight. I've learned a lot, which is really why I do this. I'm retired, this is better than zigsaw puzzles. But it finally hit me. Using the solution, which is perfect from MARK858, I need a stable list of dates for the 1st of the month and holidays. There's actually 10 holidays a year (including the 1st of January). And they can change so that list of holidays for 40 years of data could be 400. Okay, probably more like 350-360 given New Years day is consistent. Regardless, that is a big list.

But then I wokeup. I am working with different ticker databases, but all in the same years. The dates are the most stable variable. At the maximum, 40 years of data or 80 consistent reference dates. A simple XLOOKUP would work. Indeed, a simple such formula would give me the data I need. I just needed to keep a relatively small data of the actual dates in a column to use as the array for a search. Below is just the January first day of such data.

I sincerely appreciate MARK858's efforts and I have learned a lot. Thank you!!!!!!!!!!!!!!!!!!!


MSFT Data work Trial A.xlsx
IJKL
3
41/2/19870.1658
51/4/19880.3889
61/3/19890.3724
71/2/19900.6163
81/2/19911.0382
91/2/19922.3750
101/4/19932.6602
111/3/19942.5039
121/3/19953.7891
131/2/19965.4297
141/2/199710.5781
151/2/199835.2500
161/4/199958.2813
171/3/200056.3125
181/2/200123.9688
191/2/200234.6150
201/2/200326.8950
211/2/200426.7400
221/3/200526.8400
231/3/200626.9700
241/3/200729.8100
251/2/200835.3700
261/2/200930.9500
271/4/201027.9800
281/3/201128.0900
291/3/201227.4000
301/2/201327.2500
311/2/201436.9100
321/2/201554.8000
331/4/201662.5800
341/3/201762.3000
351/2/201886.3500
361/2/201997.4000
371/2/2020158.6200
381/4/2021334.7500
391/3/2022329.0100
401/3/2023229.1000
MSFT
 
Upvote 0
You're welcome, happy you have a solution that works for you.
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

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