I'm using the new STOCKHISTORY function in Excel and I'd like to always display the past 17 trading days from the point I indicate. The problem is with long weekends and holidays this alters the amount of business between two dates. I'm not sure if this will be a difficult question because I think the solution is not dependent on the fact that I'm using the STOCKHISTORY function. I have attached a photo with a simple explanation. On the left the formula for STOCKHISTORY is =STOCKHISTORY(E2,C4-C6,C4,0,1,0,2). This displays 17 business days because 22 is the magic number. On the right though if I query July 22nd with 22 day difference I only get 16 days. This is further wrong on many other dates.
I am open to having a separate reference on another sheet that has dates/formulas. I tried this but couldn't figure out a formula to pull down. Photo B displays an example of the correct number of dates that would show 17 trading days. I am also open to displaying more than 17 trading days as in the future I will need to alter the amount of trading days needed (I might need to display 15 days or 20 days).
In my head I feel like the answer has something to do with the NETWORKDAYS function and/or I should make a list of all the trading days in a year and then make a formula taking the current day and taking away a specific day. Or I could be totally wrong and the answer is obvious.
So I figured out an answer after reading some documentation. There is most likely better answers but it solves the problem enough.
So I created a list of all trading days (business days) as you can see in column O. Then in Column L a list of nearby holidays (Only needed a few exceptions). Then using the formula =(O35)-(WORKDAY(O36,-17,$L$35:$L$36)) I get the right solution which I verified in my example photo posted earlier. You could theoretically get a different number when doing your own calculations (i.e the answer 24 and 23 are both correct).
I've been working on this for awhile and can't seem to find an appropriate way to code this.
Goal: Take a list of monthly returns that currently spans over three years (this is an automated list so it will keep adding months) and find MTD, QTD, & YTD using function so I can automate the process.
Problem: I've tried the OFFSET function, different versions of COUNT, IF statements and can't find a way to properly code the function.
NOTE: this is for stock data/earnings report
Just to clarify, I know how to do this manually, but want to automate the process :) Would be more than happy to use VBA but would like to stick to a simple function if possible...
I'd be more than happy to email the WS to someone. Thanks so much!!
If you want to keep it simple and avoid vba here is a suggestion
The manner in which the data sheet is presented matters.
I assume months go down, with there only being 1 column for 'monthly returns'?
On a separate area/worksheet, set up the months with a value for what they are in the column next to them. Ie
Jan 1
Feb 2
Mar 3
Apr 4
...
Dec 12
then have a drop down where you can select what month it is.
so if it is Jul, you can select Jul.
From here, do a vlookup/indexmatch to pull the number for the month through, so Jul would be 7
From here the MTD is easy.
Do an offset from the beginning of the year to the current month
=offset ([start point],[month number],[columns])
=OFFSET($D$1,$C$21,0)
start point will be one row above Jan, that way when Jan is chosen it will fall onto Jan.
Month number you will get from what was said earlier
and columns will depend on how you place it
YTD, you will do the same as above but have a sum
so
=sum(B3:offset ([start point],[month number],[columns]))
=SUM(D2:OFFSET($D$1,C21,0))
QTD would be easiest just plotting the sums as it would never change
So set out Q1,Q2,Q3 and Q4 as prefixed ones. Then lookup to them.
However, if you want to see the most recent quarter by a formula. On the month number list add a new column which says what quarter it is. Then
Jan 1 1
Feb 2 1
Mar 3 1
Apr 4 2
...
Dec 12 4
It should the read off a formula like so
=SUM(
OFFSET([start point],(INDEX([month number table],MATCH([month],[month list],0),3)-1)*3,0):
OFFSET([start point],INDEX(month number table],MATCH([month],[month list],0),2),0))
=SUM(
OFFSET($D$2,(INDEX($F$2:$H$13,MATCH($C$20,$F$2:$F$13,0),3)-1)*3,0):
OFFSET($D$1,INDEX($F$2:$H$13,MATCH($C$20,$F$2:$F$13,0),2),0)
)
What you are doing is to find the month at the beginning of the quarter and then sum to the current month.
Regarding this bit at the end
OFFSET([start point],(INDEX([month number table],MATCH([month],[month list],0),3)-1)*3,0):
You are finding the month quarter index number(1,2,3,4) and multiplying it by three. The reason you want to take away one before multiplying by three is because you want the start point of the quarter, thus in jan to march, this will be 0, and then you add a row to get the start point being Jan.
In Oct to Dec this will be (4-1) = 3. 3x3 = 9 which will be Oct to make Oct being the start point. Amend as need be.
The yellow section is what your actual information is presented as
The green section is your lookup area
The Blue area is information you are looking for with the lower bit reading from the lookup
Trying to show a rolling average of pieces produced from the last 5 work days.
Currently I am tracking all days Mon-Sun and taking the last 5 days.
Here is my current formula:
=SUMIFS(AL:AL,AK:AK,">="&TODAY()-5,AL:AL,"<="&TODAY())/5
With AL:AL being the date column and AK:AK being the pieces produced.
I Can't figure how to arrange the data and dates to get my last 5 days average.
This is not difficult with a helper column. As you indicated in the question, the arrangement is important. First, organize your data with the most recent values at the top. So AL1 will always hold today's date.
In AM1 enter:
=IF(OR(WEEKDAY(AL1)=1,WEEKDAY(AL1)=7),"",1)
In AM2 enter:
=IF(OR(WEEKDAY(AL2)=1,WEEKDAY(AL2)=7),"",IF(SUM(AM$1:AM1)=5,"",1))
(column AM defines the values to be averages)
Then in another cell enter:
=SUMPRODUCT(--(AM1:AM10=1)*(AK1:AK10))/5
Some beginners place the most recent data at the bottom of a column. This usually make it more difficult to analyze.
I think your SUMIFS() function is incorrectly reproduced. The first argument is the "sum_range," the data to be summed, so that should be the number of pieces.
Also, you are getting 6 days including today. If you want 5 days (and if you have data for today), then you have to subtract 4 instead of 5.
You can accomplish what you want with this formula, I think:
=SUMIFS(AK:AK,AL:AL,">="&WORKDAY(TODAY(),-4),AK:AK,"<="&TODAY())/5
If you don't actually have data for today, then subtract 5 instead of 4.
This also depends entirely on your not having any data for weekend days. If you have production data for weekend days, then the averages will be off (they will include extra days).
If you have data for both weekdays and weekend days, you will have to use something like #garysstudent suggested.
I have date format in the following manner in the excel which is in string format:
Apr 1, 2016 12:37:06 PM
Apr 2, 2016 12:00:00 AM
Apr 1, 2016 9:50:22 AM
Apr 1, 2016 12:09:38 PM
Apr 1, 2016 6:53:03 PM
Apr 1, 2016 1:02:10 PM
I have tried converting it from general to date however excel still does not recognize it as date format. Need your advise as what can I try more to solve this.
Thanks in adavance !!
try =DATEVALUE("date string").
hope this helps
here is how it works
if still didn't work, then it has to do with windows date time setting.
Open Region and Language setting from Control Panel. Change the Long Date format to "MMMM dd,yyyy". Because this is the format your date string is formatted. You have to do this to get DATEVALUE() in excel to work. Now go back to your formula and see if it works.
After that, copy paste the formulas as value. Then you can change back to your preferred long date format again (or just leave it).
Couple of options for you:
Option 1
Text to Columns built in excel feature
Select all Dates for starters. They need to be in one column. On the Data ribbon, select Text to Columns. Light green back ground in the image above.
On the ensuing window that pops up select Fixed Width. Then select Next.
Adjust the columns so you get either a single column or the date in one column with the time in the other. Then select NEXT.
In the bottom preview window select the first column. Then up top select the Date radio button and from the pull down select MDY format. when ready click on FINISH button
Option 2
Formulas
Ripping out the text from the string and rebuilding the date using some ugly long formulas. So we are going to do this in parts so you can see how the big ugly gets made. When we are done we will copy the smaller formulas into one formula which will be big ugly and hard to read.
One of the annoying thing is you have the month as an abbreviation. Somewhere off to the side you will need to build a table and put all the abbreviations in one column and the corresponding month number in the column to the right. There may be other ways to do this but this is what I am going with for now.
(AA) | (AB)
Jan | 1
Feb | 2
Mar | 3
Apr | 4
May | 5
Jun | 6
Jul | 7
Aug | 8
Sep | 9
Oct | 10
Nov | 11
Dec | 12
So we can either work from biggest (Year) to smallest (seconds) or from left to right. lets tackle this starting from the left.
First thing first we need to pull the month out. Thankfully its on the LEFT side and we know its only 3 characters long. So we can go straight to the excel formula and hard code this in. And alternative approach would be to find the space. So first formula to find month is
=Left(A1,3)
Since we are going to need to know the number of that month we can convert it with a VLOOKUP function (INDEX/MATCH is another option). Lets put this in column F for now.
=VLOOKUP(Left(A1,3),$AA$1:$AB$12,2,0)
That should return the number 3 for us. you will have to adjust the AA1:AB12 to suit where you put the table.
Next we are going to pull the day. We know its 1 or two characters to the left of the first comma. We will use the FIND to determine where that is since it is not in a fixed position.
=FIND(",",A1)
So we know the date will start either 1 or 2 characters to the left. No harm in taking two as it will be a space or a number. So now lets use that knowledge with the MID formula and place the following in column G for now:
=--MID(A1,FIND(",",A1)-2,2)
the -- changed the text to a number (they are not actually needed in this case). So the next one is a little trickier but still doable. We need to find the Year. wouldn't you know it, its a fixed 4 characters long and starts just after that same first coma. So lets reuse our previous formula but tweak it a little and put the following in column H for now:
=--MID(A1,FIND(",",A1)+2,4)
Well now that we have our Month, Day and Year as numbers we can build our date using the DATE formula which we can drop in say column I:
=DATE(H1,F1,G1)
now if you thing you are ready to get started on the big an ugly we can substitute each of our previous equation into our date equation so its all done in one cell. That big formula is going to look as follows:
=DATE(MID($A1,FIND(",",$A1)+2,4),VLOOKUP(LEFT($A1,3),$AA$1:$AB$12,2,0),MID($A1,FIND(",",$A1)-2,2))
Now when I say all in one cell, I should add the caveat that you do need that helper information of the list of months...though there is a way around that too. So that takes care of the date process. now to do the time is very very similar.
We have two options The first option may not work for you since the datevalue formula did not work for you.
Option A - TimeValue
The nice thing about your time set up, is that the time is either 10 or 11 characters long from start to finish and if we take all 11 when its really only 10 we again are just snagging a space. So lets grab the RIGHT time and place this formula in the K column:
=TIMEVALUE(RIGHT($A1,11))
Now if that option does not work for you then we break it down the same way we did for the date.
Option B - Stripping Time
So lets look at patterns to see what we can figure out...that first ":" looks like a great identifier so lets find it the same way we did up for the date. Let build our time formula in column N:
=FIND(":",A1)
Let sub that straight into the MID formula to pull the hour
=MID($A1,FIND(":",$A1)-2,2)
And in Column O we will do the same for minutes and again with a bit of tweaking
=MID($A1,FIND(":",$A1)+1,2)
And since we know that the minutes keep a leading zero for single digit minutes we also know exactly where seconds are going to start. Lets put this formula in column P:
=MID($A1,FIND(":",$A1)+4,2)
So you might be wondering what to do about the AM/PM Lets grab that and put that in column Q
=RIGHT($A1,2)
So now that we have all that information what are we going to do to build the time. Well first things first is you need to know that Excel like 24 hour clocks. However it really saves it time as a decimal of a day. So 12 noon is actually stored as 0.5 or half a day. Another important thing to know is that the official supported time range for Excel is 00:00 to 23:59. There is no 24:00. now having said that 24 as an hour makes VBA crash unless you specifically deal with it in some special code. You can however get away with supplying 24 as an hour in excel formulas and get away with it sometimes. ok enough with the time lesson let build this final formula in column R:
=TIME(N1+IF(AND(Q1="PM",N1<>"12"),12,IF(AND(Q1="AM",N1="12"),-12,0)),O1,P1)
You will note there is an IF formula in there that deal with the AM PM as well as the special case of the 12 hour. The 12 is in quotes because N1 is a string and I had not converted it to a number.
now for the uglyness of back substituting our formula so it all in one cell.
=TIME(MID($A1,FIND(":",$A1)-2,2)+IF(AND(RIGHT($A1,2)="PM",MID($A1,FIND(":",$A1)-2,2)<>"12"),12,IF(AND(RIGHT($A1,2)="AM",MID($A1,FIND(":",$A1)-2,2)="12"),-12,0)),MID($A1,FIND(":",$A1)+1,2),MID($A1,FIND(":",$A1)+4,2))
And yeah that is so easy to read! I asked if you needed this in one column or two. Well you have the full formula for the date on its on and the full formula for time on its on. So if you need them together here is an interesting tid bit of information. Since Time is stored as decimal days, that means everything to the right of the decimal is time. It also means that everything to the left (or the integer portion) are days. So to have date time in one column, you just need to add the two formulas together...or as I refer to it as, the really big ugly equation...or at least one of the many that fall into that category:
=DATE(MID($A1,FIND(",",$A1)+2,4),VLOOKUP(LEFT($A1,3),$AA$1:$AB$12,2,0),MID($A1,FIND(",",$A1)-2,2))+TIMEVALUE(RIGHT($A1,11))
or if time value did not work for you
=DATE(MID($A1,FIND(",",$A1)+2,4),VLOOKUP(LEFT($A1,3),$AA$1:$AB$12,2,0),MID($A1,FIND(",",$A1)-2,2))+TIME(MID($A1,FIND(":",$A1)-2,2)+IF(AND(RIGHT($A1,2)="PM",MID($A1,FIND(":",$A1)-2,2)<>"12"),12,IF(AND(RIGHT($A1,2)="AM",MID($A1,FIND(":",$A1)-2,2)="12"),-12,0)),MID($A1,FIND(":",$A1)+1,2),MID($A1,FIND(":",$A1)+4,2))
Now this is just one solution on how to break it down with formulas. There are other options or route I could have gone at a couple of steps, but it should work for you regardless of what your system settings are. Here is a screen capture of the results on my system. note I hid empty columns that were not in use in order to make the image narrower. Column T has custom formatting of
mmm, d, yyyy h:mm:ss AM/PM
applied to it.
Option 3
VBA
I am sure someone will supply at some point. And look it happened before I could get to that stage! I don't know why 8)
As for the VBA solution, you can open VBA editor, add a new module and paste this code:
Function STRTODATE(ByVal dcell As Range)
Dim datecon As Date
datecon = dcell
STRTODATE = datecon
End Function
then you can use STRTODATE as a formula. Only really helpful, if it's one off. If it's something you'd do regularly on different files, then it can be annoying to paste this code to every workbook.