Next n-th day of the month - excel - excel

Let's say I wanna calculate my next paycheck date every day in excel. I get money each 5th day of the month. How can i do it?
Expected outcome:

Perhaps like this:
=DATE(YEAR(A2),MONTH(A2)+IF(DAY(A2)<=5,0,1),5)

Or:
=EOMONTH(A2,-(DAY(A2)<=5))+5
Edit
As requested a short summary of how I got to the above:
Initially started of with thinking about checking the number of the day of the month in an IF-statement.
=IF(DAY(A2)<=5,A2+5-DAY(A2),EDATE(A2+5-DAY(A2),1))
This would check if the returned value is smaller or equal to 5. If so, then it would add the remainder of 5 minus the returned value to the current date. If false, then it would use EDATE to get the same date of the previous calculation in the next month.
Since this looked still like it had redundant parts I started looking for a different approach where I wouldn't have to do the same calculation twice. Ended up with:
=EOMONTH(A2,IF(DAY(A2)<=5,-1,0))+5
Yet, still the IF is redundant because I need either to get a -1 or a zero as the second argument in EOMONTH. Therefor we just need the expression to return TRUE or FALSE and multiply it by -1 or rather -.

Related

Get number of days between two dates using alternate day as a variable

I have an excel userform that contains four text boxes: start date, end date, Alternate Day number & result.
A user enters a starting and ending date. For example Start Date: 01/oct/2020 and End Date: 04/jan/2021.
In the Alternate Day number text box, the user enters an alternate day number, say 4. This represents that an action begins on every 4th day.
How could I code it so that VBA can work out the total number of alternate days between the two dates?
You could even do this within Excel without VBA if you want to take that route. You could simply use the formula
= (end_date - start_date)/alternate_day_number
You could of course program the same formula in VBA as well.
Basically your question is not clear to give only one answer.
Let's assume the following 2 dates:
You can get the total number as The Guru answered as
=(end_date - start_date)/alternate_day_number
this might come up with something like 3.5 if you only want full days as total number you need to take the integer
=INT(end_date - start_date)/alternate_day_number)
So for this example above the result is 3 but as you can see in the colored numbers depending wether you consider both start_date and end_date to be in the total number you need to add +1 to the result (to make it all 4 dates).

Excel: Count Total Schedules at 30 Minute intervals taking day into account

In assessing how many agents can be added to certain times of day without exceeding the number of seats in the call center, I'm trying to discern how many agents are scheduled for each half hour interval on each day of the week.
Using the =SUMPRODUCT(((A$2:A$1000<=D2)+(B$2:B$1000>D2)+(A$2:A$1000>B$2:B$1000)=2)+0) formula I've been able to identify how many total agents work for each interval, however this doesn't take the day of week into account.
I currently have my spreadsheet setup this way:
K is the start time of the shift, L is the end time of the shift, M to S pulls data from another sheet that shows a 1 if the agent works on that day of the week and 0 if they do not, and then U has all the time intervals listed out. In the example, it's cut off but the columns continue down as needed. U goes to 49 and I've just been using a range from 2 to 500 for the others as we currently do not have that many shifts and I'm leaving space for the moment.
After some Googling, I tried =SUMPRODUCT(--(M2:M500="1"),(((K$2:K$1000<=U2)+(L$2:L$1000>U2)+(K$2:K$1000>L$2:L$1000)=2)+0)) but it only returns #VALUE! so I'm not sure what I'm doing wrong.
Any suggestions of how I can make this work? Please let me know if more information would be useful. Thanks.
=sumproduct(($K$2:$K$1000<=U2)*($L$2:$L$1000>=U2))
That will count the number of occurrences where the start time is less than or equal to the time in U2 AND the end time is greater than or equal to U2. It will check time from row 2 to row 1000. Any time one condition is checked and its true the comparison will result in value of TRUE and FALSE when its not true. The * act like an AND condition while at the same time converts TRUE and FALSE values to 1 and 0. So both conditions have to be true for a value of 1 to result. Sumproduct then totals up all the 1 and 0 to get you a count.
In order to consider the days of the week, you will need one thing to be true. Your headers in M1:S1 will need to be unique (which I believe they are). You will need to either repeat them in adjacent columns to M or in say V1 you have a cell that can change to the header of the day of the week you are interested in. I would assume the former so you can see each day at the same time.
In order to do this you want to add more conditions and pay attention to you reference locks $.
In V2 you could use the following formula and copy down and to the right as needed:
=sumproduct(($K$2:$K$1000<=$U2)*($L$2:$L$1000>=$U2)*(M$2:M$1000))
UPDATE #1
ISSUE 1 Time ranges ending the following day (after midnight)
It is based on the assumption that the ending time is later than the start time. When a shift starts at 22:00 and end at 6:30 as an example, our mind says that the 0630 is later than 22:00 because it is the following day. Excel however has no clue that 0630 is the following day and without extra information assumes it to be the same day. In excel date is stored as an integer and time is stored as a decimal. When you only supply the time during entry it uses 0 for the date.
In excel the date is the number of days since 1900/01/00. So one way to deal with your time out is to add a day to it. This way excel will know your out time is after your in time when the hour is actually earlier in the day.
See your sample data below.
Using your sample data, I did a straight copy of the value in L and placed it in M (=L3 and copy down). I then changed the cell display format from time to general. This allows you to see how excel sees the time. Note how all the time is less than 1.
In column N I added 1 to the value when the out time was less than the in time to indicate that it was the following day and we had not actually invented time travel. I also used the trick of a math operation will convert a TRUE/FALSE result to 1 or 0 to shorten the equation. I used =M3+(L3<K3) and copied down. You will notice in the green bands that the values are now greater than 1.
In the next column I just copied the values from N over using =N3 copied down, and then I applied just a time display format for the cell. Because it is only time format, the date is ignored and visually your time in column O looks the same as column L. The big difference is excel now knows that it is the following day.
you can quickly fix your out times by using the following formula in a blank column and then copying the results and pasting them back over the source column using paste special values.
=M2+(L2<K2)
The next part is for your time check. When looking at the 12:00 time you need to look at 1200 of the current day incase a shift started at 12:00 and you need to look at the 1200 period of the following day. In order to do that we need to modify the the original formula as follows:
=sumproduct(($K$2:$K$1000<=$U2)*($L$2:$L$1000>=$U2)*(M$2:M$1000)+($K$2:$K$1000<=$U2+1)*($L$2:$L$1000>=$U2+1)*(M$2:M$1000))
Note that the + in the middle of (M$2:M$1000) + ($K$2:$K$1000<=$U2+1)? This + acts like an OR function.
Issue 2 Time In/Out 15 minute increments, range 30 minute increments
You may be able to achieve this with the ROUNDDOWN Function or MROUND. I would combine this with the TIME function. Basically you want to have any quarter hour start time be treated as 15 minutes sooner.
=ROUNDDOWN(E3/TIME(0,30,0),0)*TIME(0,30,0)
Where E3 is your time to be converted
So your formula may wind up looking something like:
=sumproduct((ROUNDDOWN($K$2:$K$1000/TIME(0,30,0),0)*TIME(0,30,0)<=$U2)*($L$2:$L$1000>=$U2)*(M$2:M$1000)+((ROUNDDOWN($K$2:$K$1000/TIME(0,30,0),0)*TIME(0,30,0)<=$U2+1)*($L$2:$L$1000>=$U2+1)*(M$2:M$1000))
similar option could be used for the leaving time and rounding it up to the next 30 minute interval. In which case just use the ROUNDUP function. Though I am not sure it is required.

EXCEL-Count weekend days within a number of dates within Countifs with overlap

I have to determine per specialty the total percentage of surgeries that started outside the regular working hours or in weekends.
This is what I have so far:
=COUNTIFS(Table1[Start surgery];">17:00")+COUNTIFS(Table1[Start surgery];"<09:00")+COUNTIFS(Table1[Surgery date];"MOD(WEEKDAY(cell), 7) <2")
The first 2 countifs work, but I'm not able to count the weekend days. And a second thing is that its possible for excel to count some surgeries twice, because a surgery can be after 17:00 and in the weekend, but that has to count as one.
Who can help me:)?
Since the first part of your formula is working for time I just modified the last part of your formula...
=COUNTIFS(Table1[Start surgery];">17:00")+COUNTIFS(Table1[Start surgery];"<09:00")+SUMPRODUCT((WEEKDAY(Table1[Surgery date])=1)+(WEEKDAY(Table1[Surgery date])=7)
Where 1 and 7 are equivalent to Saturday and Sunday.
The current problem with the formula above is that surgery time after 1700 and before 0900 on the weekend are counted twice!
To rectify this all conditions could be moved inside the sumproduct as one potential solution and you would wind up with:
=SUMPRODUCT((WEEKDAY(Table1[Surgery Date])=1)+(WEEKDAY(Table1[Surgery Date])=7)+(WEEKDAY(Table1[Surgery Date])=MEDIAN(WEEKDAY(Table1[Surgery Date]),2,6))*(Table1[Start surgery]<"09:00")+(WEEKDAY(Table1[Surgery Date])=MEDIAN(WEEKDAY(Table1[Surgery Date]),2,6))*(Table1[Start surgery]>"17:00"))
Now the bracket count may be off in there as I was not using excel at the time. The other potential issue we may have is with time. is your time n the time column text or a number formatted to display as time? This may involve modifying are time comparison to a different format, but the formula itself should remain the same.
Basic think here is we put each condition inside the sumproduct. The condition will evaluate to either TRUE or FALSE. If the condition undergoes a math operation it will be turned into 1 for TRUE and 0 for FALSE. + act like OR statements, and * act like AND statement.

Count recurring values without counting the first one as recurring

I need a true/false indicator, whether the value has already appeared before. However, the first value within the same month should not be flagged as TRUE but as false.
E.g. in the sample below, the first 1#a.com in April should be false, but the second one should be true, as well as the 1#a.com in May. I'm getting a bit confused on how to use the COUNTIF formula properly. The end product should be whether a customer has already booked before, either in the same month or any month before....
Thanks for your time and your help!
Assuming data starts in A2: =COUNTIF($A$2:A2,A2)>1

Using dateRestrict to determinate new cases weekly

I'm trying to get 2 date limits from date restrict to calculate the difference between them.
So for example, I get January 1 and January 7, and subtract the second result from the first one.
Theoretically I should end up with the amount of new references to the given searched words in the week, right?
The thing is, sometimes I get a negative result for this, like -60000. How come 60000 references could disappear from one week to another?
Am i misinterpreting the way dateRestrict works?
my url so far is like:
first query:
https://www.googleapis.com/customsearch/v1?q=MYSEARCH&dateRestrict=d209&key=MYKEY&cx=MYID
second query:
https://www.googleapis.com/customsearch/v1?q=MYSEARCH&dateRestrict=d203&key=MYKEY&cx=MYID
The field of the response I'm using is the totalResults.

Resources