I'm using the following formulas to obtain the start date and end date of the week number, given the week number and the year:
Start of week:=MAX(DATE(A2,1,1),DATE(A2,1,1)-WEEKDAY(DATE(A2,1,1),2)+(B2-1)*7+1)
End of week: =MIN(DATE(A2+1,1,0),DATE(A2,1,1)-WEEKDAY(DATE(A2,1,1),2)+B2*7)
It seems to work well except that week 1 of 2019 begins on 31-12-2018 but my formula for the start date of the week 1 shows it as 01-01-2019. What is the problem with my formula?
Just subtract 6 from the end date formula
=(MIN(DATE(A2+1,1,0),DATE(A2,1,1)-WEEKDAY(DATE(A2,1,1),2)+B2*7))-6
As I noted in my comment, the easiest way is just to subtract 6 from your end date. If your data is in a table, your formula would be =[#EndDate]-6. Otherwise, it would be something like =E2-6.
Related
How can one return the next date of a given weekday (it could be either a number 1-7 or names Sunday-Saturday).
For example, if today, on Friday 16-Oct-2009 I passed in:
Friday, it would return today's date 16-Oct-2009
Saturday returns 17-Oct-2009
Thursday returns 22-Oct-2009
Here is the Spreadsheet
I have tried this formula:
=A12+(C12+ MOD(7-day(A12),7))
I worked out an answer to my own question
Here is the formula
=date(year(A12),month(A12), day(A12) + mod(C12+ 7-WEEKDAY(A12),7))
This worked for me:
=TODAY()+MOD(4 + 7-WEEKDAY(TODAY()),7)
Just replace TODAY() and 4 (weekday#) for your context.
This is the formula you are seeking.
=A12+(7-WEEKDAY(A12)+B12)
A12 holds the start date. B12 holds the ID of the targeted weekday, e.g. 1 for "Sunday".
A11 must hold a true date, as opposed to a text string that looks like a date. If the latter is the case an error will be displayed. The result is always a date. You can format that date as "dddd" to display a day's name or "dddd, dd-mm-yyyy" to display both date and day.
And here are the mechanics of the formula:- A12-WEEKDAY(A12) returns always the previous Saturday, a date that is always in the past. A12+(7-WEEKDAY(A12)) inverts that calculation, returning always the next Saturday. This date is always in the future except when A12 is a Saturday. That's when the result will be = A12.
Having found a stable base, you can just add the day you want to the Saturday you have. Add 1 (for Sunday) and you get a Sunday. Add 7 and you get a Saturday. All these dates will be in the future.
That's all as you want it except that if WEEKDAY(A12) = B12 you want the same date as in A12 which is a week earlier than the one the formula prefers. This is where I went wrong while in haste. My apologies! This is the correct modification of the formula's result, to wit, conditionally deduct 7 from the calculation.
=A12+(7-WEEKDAY(A12)+B12)-IF(WEEKDAY(A12)=B12,7,0)
I'm trying to calculate the number of months in a given time range that fall within 2019.
My data looks like this:
I changed the format of my Start Date and End Date columns to "date". The "Duration in months" and output column have number formatting.
I approached it as follows:
If Start Date (year) < 2019 AND End date (year) = 2019, take the number of months between 1-1-2019 and the end date.
If start Date (year) < 2019 AND End date (year) > 2019, the number of months in 2019 = 12
If start Date (year) = 2019 AND End date (year) = 2019, take the number of months between Start Date and End date.
If start Date (year) = 2019 AND End date (year) > 2019, take the number of months between Start Date and 1-1-2020.
For all other cases, the number of months = 0.
I then followed the instructions from this website for nested IF functions.
I came up with the following formula: (Edit: changed last datedif to 2020 instead of 2019)
=IF(AND(YEAR(A3)<2019;YEAR(C3)=2019);DATEDIF(DATE(2019;1;1);C3;"m");IF(AND(YEAR(A3)<2019;YEAR(C3)>2019);12;IF(AND(YEAR(A3)=2019;YEAR(C3)=2019);DATEDIF(A3;C3;"m");IF(AND(YEAR(A3)=2019;YEAR(C3)>2019); DATEDIF(A3;DATE(2020;1;1);m);0))))
For the first 4 rows, it correctly returns 12.
For row 7 and 8, however, it returns #NAME? .
No matter what I try, I can't seem to get it to work. Any ideas on how I can solve this?
Much appreciated!
Amy
For the entire formula you have been consistent, apart from the last IF statement, where you went wrong within your DATEDIF( statement:
DATEDIF(A3;DATE(2019;1;1);m);0)
First of all you forgot to put m in quotes: "m"
Secondly you accidentally swapped the Date and Cell references, so it would have resolved #NUM. The full correct formula should be:
=IF(AND(YEAR(A7)<2019;YEAR(C7)=2019);DATEDIF(DATE(2019;1;1);C7;"m");IF(AND(YEAR(A7)<2019;YEAR(C7)>2019);12;IF(AND(YEAR(A7)=2019;YEAR(C7)=2019);DATEDIF(A7;C7;"m");IF(AND(YEAR(A7)=2019;YEAR(C7)>2019);DATEDIF(DATE(2019;1;1);A7;"m");0))))
Which will give you the result 4.
Alternatively, the following will give you the amount of full months between two dates. This works with your sample where your dates start on the first day of a month:
Formula in D2:
=SUMPRODUCT((DATE(2019,ROW($1:$12),1)>=A2)*(DATE(2019,ROW($1:$12),1)<=C2))
You've missed the quotes round the final "m" - try
=IF(AND(YEAR(A3)<2019;YEAR(C3)=2019);DATEDIF(DATE(2019;1;1);C3;"m");IF(AND(YEAR(A3)<2019;YEAR(C3)>2019);12;IF(AND(YEAR(A3)=2019;YEAR(C3)=2019);DATEDIF(A3;C3;"m");IF(AND(YEAR(A3)=2019;YEAR(C3)>2019); DATEDIF(A3;DATE(2019;1;1);"m");0))))
I need an Excel formula that takes a given day and provides the next upcoming Thursday of that week. I have a list of dates with time stamp attached:
Date Week Ending
10/5/2015 10/8/2015
10/11/2015 10/15/2015
10/21/2015 10/22/2015
10/27/2015 10/29/2015
I want to convert it to the weekending. The example is of my date and the "Week Ending" I want the formula to show.
Assuming Date is in A1, if #Grade 'Eh' Bacon's solution is not working for you and given your mention of time stamp attached, maybe treat your dates as text and say, in B2 and copied down to suit:
=LEFT(A2,10)+(7-WEEKDAY(LEFT(A2,10),15))
Assuming your data is in A1, the formula is simply:
=A1+(7-WEEKDAY(A1,15))
Weekday(A1,15) gives the number of the day of the week of a given date, and the option ,15 says use a week starting with Fri and ending with Thurs. So, a given date, + (7-Weekday()) will give you the number of days required to get to the 'week ending with the Thursday on x date'.
If you want a Thursday to return the same date, then try:
=A1+7-WEEKDAY(A1+2)
The more generalized version would be:
=A1+7-WEEKDAY(A1+7-DOW)
where DOW (DayOfWeek) translates
Sun = 1
Mon = 2
...
Sat = 7
I have a list of periods (given by period start, period end) in Excel and I have to calculate exactly how many months (as a fraction) are during that period.
E.g. if given a period January 15th to March 7th, then this would be
15-01-2015 until 31-01-2015 = 17 days
01-02-2015 until 28-02-2015 = 28 days
01-03-2015 until 07-03-2015 = 7 days
Months during period = 17/31 + 28/28 + 7/31 = 1,774193548 months
Note that in this calculation, start date and end date are both counted as part of the period.
As the periods can be anything, then I haven't been able to figure out to calculate this with an Excel formula (or if it's even possible).
Any ideas?
Well it's not pretty, but if you have a start date at A1 and an end date at A2, then:
=12*(YEAR(A2)-YEAR(A1))+MONTH(A2)-MONTH(A1) +
(DAY(A2)/(DATE(YEAR(A2),MONTH(A2)+1,1)-DATE(YEAR(A2),MONTH(A2),1))) -
((DAY(A1)-1)/(DATE(YEAR(A1),MONTH(A1)+1,1)-DATE(YEAR(A1),MONTH(A1),1)))
The way this works is:
12*(YEAR(A2)-YEAR(A1)): Difference in year times 12.
+MONTH(A2)-MONTH(A1): (plus) Difference in months.
+(DAY(A2)/(DATE(YEAR(A2),MONTH(A2)+1,1)-DATE(YEAR(A2),MONTH(A2),1))): (plus) Day of end date divided by the total days in end month, calculated by the diff between that month's 1st and the following month's 1st.
-((DAY(A1)-1)/(DATE(YEAR(A1),MONTH(A1)+1,1)-DATE(YEAR(A1),MONTH(A1),1))) (minues) Day of start (minus 1 to not count that day) month divided by the total days in start month.
Of course you duplicate that for any set of cells...
Assuming column A lists the start dates and column B lists the end dates:
=SUMPRODUCT((1+B1:B3-A1:A3)/DAY(DATE(YEAR(A1:A3),MONTH(A1:A3)+1,)))
Note: you will need to increase the depth of the ranges to suit your needs... just change the 3s to a deeper row number.
UPDATE
Based upon your feedback and new requirement, here is a version of the above formula that will calculate the total, one row at a time:
=(1+B1-A1)/DAY(DATE(YEAR(A1),MONTH(A1)+1,))
UPDATE 2
Based on new information that the interval on one row can exceed one month, here is a new version of the formula:
=DATEDIF(A1-DAY(A1)+1,B1,"m")-1+(1+EOMONTH(A1,0)-A1)/DAY(DATE(YEAR(A1),MONTH(A1)+1,))+(B1-EOMONTH(B1,-1))/DAY(DATE(YEAR(B1),MONTH(B1)+1,))
IMPORTANT NOTE: After confirming this formula you will need to apply GENERAL number formatting to the cell. This is because Excel will try to apply DATE number formatting unnecessarily.
Why don't you use:
=12*YEARFRAC(StartDate,EndDate)
I've got a column full of dates. How can I check that column to find which date is within a month of todays date, and then return it?
If there is no date within a month, just return blank
Lets say my dates are:
01-Jan-12
01-Apr-12
01-Jul-12
01-Oct-12
01-Jan-13
The code im using is below. A:A is the range of the dates above
=MIN(IF(A:A>TODAY(),A:A))
The issue im having is that if I use the above, it returns 01/01/12 and not 01/01/13. Also, if I change the dates so the next date is December 1st 2012, it still returns 01/01/12
So you really just want the earliest date if that's within a month? If so perhaps try
=IF(MIN(A:A)-TODAY()<=30,MIN(A:A),"")
Assumes dates in column A
If you have past and future dates try this formula
=IFERROR(SMALL(IF(A2:A100>=TODAY(),IF(A2:A100<=TODAY()+30,A2:A100)),1),"")
confirmed with CTRL+SHIFT+ENTER
or for exactly 1 month (rather than 30 days) try using EDATE, i.e.
=IFERROR(SMALL(IF(A2:A100>=TODAY(),IF(A2:A100<=EDATE(TODAY(),1),A2:A100)),1),"")