Finding a shorter period of days within a longer period for calculation - excel

I have a set of dates, in total 7 periods spanning 6 months each with corresponding calculation factor.
I will have user input of period for which they'd want the calculation to be done, which can fall within one of the 6 months periods or encompass between two or more such periods wholly or partially.
Illustration:
The preset periods:
User input:
I've obtained decimal value (monthly basis) of the periods input by the user for calculation. For first instance (see user input), the decimal value for the period 01-01-2015 to 29-04-2015 will be 3.97 on monthly basis. The calculation for that period would be like:
n*3.97*113%
For the second instance, the decimal value for the period 30-04-2015 to 30-06-2015 would be 2.03, which would be used to do calculation at 113% and then the result will be added to the calculation done at 119% using the decimal value 5.65 for the period 01-07-2015 to 20-12-2015:
(n*2.03*113%)+(n*5.65*119)
Think I can handle the breaking up of periods since the revision event is bi-annual on particular dates but advice regards to that is welcome. More importantly, I need help tracing the preset calculation factor (say 132%) corresponding to the period input by the user as illustrated above. Is it feasible?

I will use the standard approach for finding the overlap between two dates and will split the task into three parts as in my answer to this recent question.
(1) The first part is finding the overlap between the user's range of dates and one or more revision periods in whole months and will need an array formula. I have chosen to use the Datedif function 1 to get the difference in months between the beginning and end of the overlaps. If there is no overlap, the start date fed in to Datedif will be after the end date, and it will return an error which can be trapped by Iferror. If the user's dates start in A2 and B2, this gives in C2:
=SUM(IFERROR((DATEDIF(IF(K$2:K$8>A2,K$2:K$8,A2),IF(L$2:L$8<B2,L$2:L$8,B2),"m")+1)*M$2:M$8,0))
which has to be entered as an array formula using CtrlShiftEnter
The above result includes the first and last months entered by the user even if they are incomplete months. It's then necessary to subtract any missing days in the first and last months.
(2) Missing days in first month as a fraction of the number of days in that month in D2:
=SUMIFS($M$2:$M$8,$K$2:$K$8,"<="&A2,$L$2:$L$8,">="&A2)*(A2-EOMONTH(A2,-1)-1)/(EOMONTH(A2,0)-EOMONTH(A2,-1))
A noted by OP, this could also have been done using sumproduct, vlookup or index/match.
(3) Missing days in last month as a fraction of days in that month in E2:
=SUMIFS($M$2:$M$8,$K$2:$K$8,"<="&B2,$L$2:$L$8,">="&B2)*(EOMONTH(B2,0)-B2)/(EOMONTH(B2,0)-EOMONTH(B2,-1))
The total is just (1)-(2)-(3) or
=C2-D2-E2
I have put the results of OP's two examples for comparison in H2 and H3: my results agree with them in the first 3 significant figures.
n*3.97*113%
(n*2.03*113%)+(n*5.65*119)
In all cases I have set n=1 and ignored the fact that the rate is a percentage.
This shows how the results would be calculated manually:
1 Pros of using Datedif:
(1) Works across year boundaries unlike just using Month function.
(2) Works conveniently with Iferror to identify non-matching date ranges.
Cons of using Datedif:
(1) It is an undocumented function and may be withdrawn in future.
(2) In this particular case, all date calculations are within the same year so Month would be useable.

Related

Excel formula to split an amount per year depending on expenditure days within a date range

I am in the process of building a formula to split a total cost (in column J) based on start and end expenditure periods that can vary from 2021 to 2031. Based on the days between the expenditure period dates (column M), I managed to work out to split the cost using the formulas below up to 2023 but it is not consistent and at times incorrect.
In cell P5 I have the following formula. For year 2021, I seem to get the correct split result.
=IF($K5>AS5,0,$J5/$M5*(AS5-$K5))
In cell Q5, I have the following formula. For year 2022, I seem to get the correct spit as well
=MIN(IF(SUM($N5:P5)>=$J5,0,IF($L5>=AS5,$J5/$M5*(AS5-AR5),$J5/$M5*($L5-MAX(AR5,$K5)))),K5)
However, I don't get the right result in cell Q6 which has the same formula but different dates
=MIN(IF(SUM($N6:P6)>=$J6,0,IF($L6>=AS6,$J6/$M6*(AS6-AR6),$J6/$M6*($L6-MAX(AR6,$K6)))),K6)
Cell R6 shouldn't return any result because it is out of date range. This is where things get mixed up.
Note that from column AR to BC, it is all year end dates from 2020 to 2031 as shown below.
Is there a better way to tackle this sort of formula as I seem to get dragged into a long and unreliable way of doing this.
Here single function(♣) that will create a series of pro-rata multipliers (of appropriate length) for any given start/end date:
EDIT: see end of soln for extended version per OP comment to original soln...
SINGLE FUNCTION
=J11*(LET(dates,EDATE(DATE(YEAR($K11),1,1),12*(SEQUENCE(1,YEAR($L11)-YEAR($K11)+2,1))),IF(dates<K11,K11,IF(dates<L11,dates,L11)))-LET(dates,EDATE(DATE(YEAR($K11)-1,1,1),12*(SEQUENCE(1,YEAR($L11)-YEAR($K11)+2,1))),IF(dates<K11,K11,IF(dates<L11,dates,L11))))/(L11-K11)
It may appear somewhat unwieldy in length, but it is far more robust (and concise) compared to the combination of steps/series you have created. What's more, it returns the precise answer RE: pro-rata payments and is guarenteed to never over/under-run RE: total payment (by design).
BREAK-DOWN
Comprises 3 distinct parts (some of which are similar in pattern/formation):
1] First part - create a series (array) of years spanning start-end dates:
=LET(dates,EDATE(DATE(YEAR($K5)-1,1,1),12*(SEQUENCE(1,YEAR($L5)-YEAR($K5)+2,1))),IF(dates<K5,K5,IF(dates<L5,dates,L5)))
Thanks to the lovely Spill functionality the new Office 365 variant Excel boasts, you never have to worry about how many years are required -- so long as you have the space to the right of this workbook (would be unusual otherwise - assuming you start in column O and clear any content to the right of this, you'd need an end date beyond the year 2557 (26th century) to run out of columns! ☺
2] Second part is merely a replica of the firs series, albeit shifted to the right 'once' (so starts with the 2nd element in the 1st series):
=LET(dates,EDATE(DATE(YEAR($K5),1,1),12*(SEQUENCE(1,YEAR($L5)-YEAR($K5)+2,1))),IF(dates<K5,K5,IF(dates<L5,dates,L5)))
3] Third part - you have the basic ingredients from parts 1 and 2 to complete the required task easily: simply deduct series 2 from 1 (giving days between successive dates in series 1 - i.e. days for each year between start and end dates), divide by total days (to yield pro-rata multipliers), and then multiply these by the total £amount and voila - you have your series!
=J5*(O6#-O5#)/(M5)
♣ Caveat(s) - assuming you have Office 365 compatible version of Excel (which is quite common nowadays)
*EDIT - EXTENDED VERSION
Given above, the following extends this to align monetary results (1st table, o11:w21) within respective calendar period columns (spanning the entire period in question).
This soln:
Determines header row based upon the number of columns & corresponding calender periods (financial yrs commencing 1/1) as an array function (i.e. dynamic range)
Utilises a modified version of the eq. provided for dates arrage (refer: "First Part", original soln)
Comment - same caveats as before - i.e. Office 365 etc.
Screenshot(s)/here refers:
DATES (HEADER) - Y10 (array)
=LET(y_,MIN(K11:K21),x_,MAX(L11:L21),EDATE(DATE(YEAR(y_)-1,1,1),12*(SEQUENCE(1,YEAR(x_)-YEAR(y_)+2,1))))
Comment - enter once within single cell Y10 - i.e. as an array function with Spill to right
ALIGNED/SHIFTED FINANCIALS - Y11:Y21 (each cell in col is an array)
=IFERROR(IF(Y$10#<EDATE(K11,-12),"",IF(Y$10#>EDATE(L11,12),"",INDEX(O11#,1,MATCH(Y$10#,EDATE(DATE(YEAR($K11)-1,1,1),12*(SEQUENCE(1,YEAR($L11)-YEAR($K11)+2,1))),0)))),"")
Comment - enter this as an array fn. (#SPILL! to the right) in each cell within column Y (can drag this function down Y11:Y21 as required)

Excel count number of dates (in rows) which fall under a predetermined period

So i have been playing with Excel for a while, and one thing led to another, i wanted to try this: How would i calculate the instances of dates (such as below) in one sheet
And count which of those dates fall under the 30/60/90 day period (such as below) I'm looking to see if i could have a final tally up the number of instances of dates which fall below 30/60/90 days to the current date in another sheet.
Using the table example gave that i would know that 2 dates fall under the 30/60 days period, and one fell under the 90 days period
Is there a solution to my predicament?
Edit: More accurate question
Use nested IF() function.
=IF(TODAY()-B2<=30,30,IF(TODAY()-B2<=60,60,IF(TODAY()-B2<=90,90,"Other period")))
Or try IFS() if you have Excel-365.
=IFS(TODAY()-B2<=30,30,TODAY()-B2<=60,60,TODAY()-B2<=90,90)
Another option is-
=XLOOKUP(TODAY()-B2,{30,60,90},{30,60,90},"Other Period",1)
You could try:
Formula in E2:
=SUMPRODUCT(--(CEILING(D$6-B$2:B$6,30)=D2))

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.

Complicated excel equation with multiple conditions

I have a sheet that I use to calculate my taxes, deductions, and 401k based on a timesheet. It also calculates my PTO, sick, and comp days (I don't get overtime, when I work overtime I get that time back at the end of the year as extra paid days off). My issue is that with the timesheet, it's tough to calculate my sick days, all other times are hour for hour or based on the pay period. But with sick days I get a specific amount every year.
Basically, I get 10 days (80 hours) per year. So I take that 80 hours, divide it by 26, and that gives me how much sick time I get per pay period. Problem is, since I get a biweekly pay, that's not actually correct. So for example, this month I have 2 paychecks, I'd get .92 days of sick time, but I should actually get 1. On the 2 months out of the year where I have 3 paychecks I would get 1.38 days of sick time, which of course isn't correct either.
So the issue is I'm trying to figure out how to write a formula to give me the the correct number of days. Refer to the screenshot:
So basiclaly on G6, the formula takes the rolled over sick days from the previous year (G39) and adds the current sick time to it. It decides that by checking if the gross pay for that pay period is there, then multiplies that amount by the sick time accrual rate (G40) and divides that by 8 to give me the days.
But what I want to do is to check how many months have been filled out and return that. So in this example May has been completed, so it would return 1 day. Since September has 3 pay periods, you will need to have all three September paychecks filled out for it to increment from 4 to 5 (may, jun, jul, aug makes 4).
Any ideas? Everything I've tried to do this it just fails. Keep in mind that those dates are dynamic, next year when I change the start date for the tax year, the months that have 3 months will change to match the actual pay periods for that year. So this formula will need to actually be able to count that any month has 3 pay periods to advance the sick day count, otherwise to do it if there are only 2 that have been filled.
Attempt #2 to answer! >8(
The end equation to do this all in one cell is going to get ugly,but it will work. In order to explain this and basically how I developed it, I am going to break it down into parts. At the end the parts will be back substituted into the big equation.
The first thing I did was determine what row was last filled, or as per the comments what is the last row of column P that has a value greater than 0. In order to determine this I used the aggregate function in a temporary cell of T15 (yes, in the middle of your spreadsheet but it wont matter in the end):
=AGGREGATE(14,6,(P5:P30>0)*(ROW(P5:P30)-ROW(P5)+1),1)
To break this function down:
14 tells it we want to do an array type calculation sorting the array from largest to smallest.
6 tells it to ignore errors
(P5:P30>0) tells it to build a true (or 1) and false (or 0) array of cells greater than 0
(ROW(P5:P30)-ROW(P5)+1) generates an array listing row numbers
1 tells it to return the largest value in the array, if it was 2 the second largest.
Now the important thing here is what happens when you multiply the greater than 0 array with the row number array. You wind up getting only the row numbers where there is a value in P greater than 0. And when we sort that and ask for the largest number we get the last row you have completed. Something to work with.
So now we can look up the last date completed, do some checks to see if its the end of the month or not and figure out how many sick days. The ugly formula starts out as:
=IF(MONTH(INDEX(I5:I30,T15))=MONTH(INDEX(I5:I30,T15)+14),MONTH(INDEX(I5:I30,T15))-1,MONTH(INDEX(I5:I30,T15)))
The logic test here is to find our if the last filled out date and the date 14 days in the future are still the same month. If they are the same month, you are not at the end of the month yet and there for have only earn up to the previous month's number in sick days. As such this part will tell us the previous month's number os sick days:
MONTH(INDEX(I5:I30,T15))-1
Now if the date 14 days in the future is not the same month then we know the last entry for the month has been completed and therefore we have accrued that month's number in sick days and use basically the same formula:
MONTH(INDEX(I5:I30,T15))-1
well I can see we have called on cell T15 4 times just to determine if we are subtracting 1 or 0. While the IF formula may feel more inline with your thought process, we can rearrange things and still get the same results but shortening the formula, reducing the calls to cell t15 by 1 and dropping the IF all together. This only works because we are dealing with 1 and 0 which is also true and false.
=MONTH(INDEX(I5:I30,T15))-(MONTH(INDEX(I5:I30,T15))=MONTH(INDEX(I5:I30,T15)+14))
Now lets bypass that T15 calculation and back substitute it in to the month formula above to get:
=MONTH(INDEX(I5:I30,AGGREGATE(14,6,(P5:P30>0)*(ROW(P5:P30)-ROW(P5)+1),1)))-(MONTH(INDEX(I5:I30,AGGREGATE(14,6,(P5:P30>0)*(ROW(P5:P30)-ROW(P5)+1),1)))=MONTH(INDEX(I5:I30,AGGREGATE(14,6,(P5:P30>0)*(ROW(P5:P30)-ROW(P5)+1),1))+14))
Not done yet. That only tells you the number of days you have accrued this year. Not what you really want to know. you need to convert it to hours. It also need to be reduced by the number of sick days used. The following need to be added to the big ugly above:
*8 for 8 sick hours to a sick day
-sum($L$5:$L$30) to account for sick time used
this results in:
=(MONTH(INDEX(I5:I30,AGGREGATE(14,6,(P5:P30>0)*(ROW(P5:P30)-ROW(P5)+1),1)))-(MONTH(INDEX(I5:I30,AGGREGATE(14,6,(P5:P30>0)*(ROW(P5:P30)-ROW(P5)+1),1)))=MONTH(INDEX(I5:I30,AGGREGATE(14,6,(P5:P30>0)*(ROW(P5:P30)-ROW(P5)+1),1))+14)))*8-sum($L$5:$L$30)
Now I did notice during testing that if no entries are in the spreadsheet, then the row of the last entry become 0 and this is simply not acceptable as it causes some strange results. So we will wrap this whole formula in a small error catcher to make sure 0 is the results when no payperiods have been completed.
=if(sum(P5:P30)=0,0,(MONTH(INDEX(I5:I30,AGGREGATE(14,6,(P5:P30>0)*(ROW(P5:P30)-ROW(P5)+1),1)))-(MONTH(INDEX(I5:I30,AGGREGATE(14,6,(P5:P30>0)*(ROW(P5:P30)-ROW(P5)+1),1)))=MONTH(INDEX(I5:I30,AGGREGATE(14,6,(P5:P30>0)*(ROW(P5:P30)-ROW(P5)+1),1))+14)))*8-sum($L$5:$L$30))
The icing on the cake is adding on the accrued sick days from the previous year. Since I am not sure how the sick rate and sick start work together I will leave that calculation up to you and simply let you know that whatever number gets carried over from the previous year, simply add it to the above formula after the very last ).
Here is my test bed showing proof of concept:
WARNING: This method ##WILL## produce false results for a pay period is =0 before the last date with a pay period >0 see example below:

Excel Subtracting periods from specific dates using 360-day calendar?

Got formulas for figuring my differences between date periods in days as follows:
=IF(F7="","0",DAYS360(F6,F7)+1)
This gives the days result for each period that I am interested in, but I then need to add each period of days together and subtract them from a current date (like doing service computation). The issue is that I need to do this second calculation within a 360-day calendar as well. If I just try to do a days360() formula with one value being the current date and the other being the total number of days that I need to backtrack, then the "original" date that it comes up with is drastically off.
This seems to be the equivalent of the difference between NETWORKDAYS and WORKDAY functions. The former counts working days between two dates, the latter adds working days to a date, you essentially want the WORKDAY equivalent for DAYS360, which I don't think exists.
You can manipulate DAYS360, though, e.g. with a date in A2 and number of days to subtract (in 360 day mode) in B2 you can use this formula for the date
=A2-B2-MATCH(B2,INDEX(DAYS360(A2-B2-ROW(INDIRECT("1:"&CEILING(B2/30,1))),A2),0),0)

Resources