Calculate total hours between cell like 9-5? - excel-formula

I'm looking to create a shift schedule sheet for my team. This is what I've got so far:
How do I calculate the total hours for the 7 days in between those times?
Thanks

How about splitting starting time and end time in two columns such as:
| Monday | Tuesday | ...
| 9am | 5pm | 9am | 5pm | ...
Then if you do an arithmetic operation, you'll have a result in days. So simply multiply by 24:
1 | A | B | C | D |
2 | Monday | Tuesday | ... |
3 | 9am | 5pm | 9am | 5pm | ... | =24*( (B3-A3) + (D3-C3) ...)
Tested in Excel 2013:
Provided that A3, B3, C3... are times. Formatting is up to you obviously.

Related

Trying to get start time and end time from a list of dates and times (Weekly)

I have a listing of production date and times and I want to calculate the time window that they were utilized. My list looks something like this but with many more weeks:
Action | Date Time Start | Date Time End | Week Num | Lot Num | Machine Num
Setup | 9/12/2022 1:00 | 9/12/2022 3:00 | 1 | 12345 | 101
MFG | 9/12/2022 3:00 | 9/13/2022 13:00 | 1 | 12345 | 101
Setup | 9/13/2022 14:00 | 9/13/2022 15:30 | 1 | 54321 | 101
MFG | 9/13/2022 15:30 | 9/16/2022 22:00 | 1 | 54321 | 101
So what I want to grab from this list is the start Date Time of the week and the End date time of the same week to give me the Total time of 117:00. I don't know the best way of doing this.
So, a quick example as shown:
But going across midnight will be an issue. Posted a solution to that on here before. Also, another tip is make sure of your formatting.

Counting Time within ranges in table

So I did look through a couple of existing answers, but they were all in some programing language (ie, RUBY, PHP, etc). I also managed to figure out a way to do what I want but some of my formulas felt either hard coded or verbose. So my question is, is there a cleaner way to write my formulas to achieve my goal.
What I am starting with is a simple line to calculate cost of electricity usage. It look like:
+─────────────+────────+───────+───────+─────────+──────────+───────────+───────────+────────+──────────+───────────+───────────+──────────+──────────────+─────────────+
| | | | | | KW used | | | | Rate | | | Usage | Delivery | Total Cost |
| Date | Start | Stop | KW/H | Season | On-Peak | Mid-Peak | Off-Peak | Total | On-Peak | Mid-Peak | Off-Peak | Cost | Cost | |
+─────────────+────────+───────+───────+─────────+──────────+───────────+───────────+────────+──────────+───────────+───────────+──────────+──────────────+─────────────+
| 2022/12/17 | 05:00 | 22:00 | 1.44 | Winter | 0 | 0 | 24.48 | 24.48 | 0.17 | 0.113 | 0.082 | 2.00736 | 1.141296768 | 3.15 |
+─────────────+────────+───────+───────+─────────+──────────+───────────+───────────+────────+──────────+───────────+───────────+──────────+──────────────+─────────────+
The first 4 columns are user entry fields and correspond to columns B through E. The remainder of the columns F through P are all formulas.
My seasonal time bands are contained in the following table
+─────────+──────────────+─────────────+───────────+──────────+──────────+──────────+
| Season | Start | Stop | Time | Start | Stop | Rate |
| | | | Period | (HH:mm) | (HH:mm) | ($/Kwh) |
+─────────+──────────────+─────────────+───────────+──────────+──────────+──────────+
| Summer | May 01 | October 31 | Off-Peak | 00:00 | 07:00 | 0.082 |
| | | | Mid-Peak | 07:00 | 11:00 | 0.113 |
| | | | On-Peak | 11:00 | 17:00 | 0.17 |
| | | | Mid-Peak | 17:00 | 19:00 | 0.113 |
| | | | Off-Peak | 19:00 | 24:00 | 0.082 |
+─────────+──────────────+─────────────+───────────+──────────+──────────+──────────+
| Winter | November 01 | April 30 | Off-Peak | 00:00 | 07:00 | 0.082 |
| | | | On-Peak | 07:00 | 11:00 | 0.17 |
| | | | Mid-Peak | 11:00 | 17:00 | 0.113 |
| | | | On-Peak | 17:00 | 19:00 | 0.17 |
| | | | Off-Peak | 19:00 | 24:00 | 0.082 |
+─────────+──────────────+─────────────+───────────+──────────+──────────+──────────+
Note: weekends and holidays are Off-Peak
To aid with formula reading (or so I thought) I used the following named ranges:
Season_1 =$B$75 ("Summer")
Season_1_Start =DATE(YEAR(TODAY()),5,1) ("May 01")
Season_1_End =DATE(YEAR(TODAY()),10,31) ("Oct 31")
Season_1_TimeRates =$E$75:$H$79 ("Off-Peak" to 0.82)
Season_2 =$B$80 ("Winter")
Season_2_Start =DATE(YEAR(TODAY()),11,1) ("Nov 01")
Season_2_End =DATE(YEAR(TODAY()),4,30) ("Apr 30")
Season_2_TimeRates =$E$80:$H$84 ("Off-Peak" to 0.82)
TimeRates =IF($F3="Summer",Season_1_TimeRates,Season_2_TimeRates)
Step 1
Determining the season in F4
I used the following formula in F4.
=IF(AND($B3>=DATE(YEAR($B3),MONTH(Season_1_Start),DAY(Season_1_Start)),$B3<=DATE(YEAR($B3),MONTH(Season_1_End),DAY(Season_1_End))),Season_1,Season_2)
It seamed reasonable and substituted the year of the actual entry for making date comparison. I took the approach of if its not within the range for summer, then by default it has to be winter. Part of me is thinking I should check the winter date check. If both date checks fail then toss up a check date statement or the like.
Step 2
Determine how many hours are in each category according to the table and place results in G through I. This is where things go a little ugly for me. After chasing my tail in circle for a little bit I said screw it and plunged in with some hard coding and got something that worked but looked rather ugly and lengthy to me
I started out by figuring out that each time range check had 6 possible outcomes. I boiled those outcomes down to 2 possible results: 0 or Formula.
SB = Start Before
SI = Start Inside
SA = Start After
FB = Finish Before
FI = Finish Inside
FA = Finish After
So the results were either 0 because the time range in C and D are outside of the Time range of the table. Or its some form of Finish - Start.
I couldn't figure out how to make it count the hours in the range using the title (ie On-Peak) because there were sometimes more than one entry depending on the season in F. So what I did as a working step was to create a formula that would count the hours for each of the 5 time ranges instead of the names of the time range:
To get the hours I used the following formulas for each row:
=IF(OR($D3<=INDEX(TimeRates,1,2),$C3>=INDEX(TimeRates,1,3)),0,IF($D3<=INDEX(TimeRates,1,3),$D3,INDEX(TimeRates,1,3))-IF($C3<=INDEX(TimeRates,1,2),INDEX(TimeRates,1,2),$C3))*24
=IF(OR($D3<=INDEX(TimeRates,2,2),$C3>=INDEX(TimeRates,2,3)),0,IF($D3<=INDEX(TimeRates,2,3),$D3,INDEX(TimeRates,2,3))-IF($C3<=INDEX(TimeRates,2,2),INDEX(TimeRates,2,2),$C3))*24
=IF(OR($D3<=INDEX(TimeRates,3,2),$C3>=INDEX(TimeRates,3,3)),0,IF($D3<=INDEX(TimeRates,3,3),$D3,INDEX(TimeRates,3,3))-IF($C3<=INDEX(TimeRates,3,2),INDEX(TimeRates,3,2),$C3))*24
=IF(OR($D3<=INDEX(TimeRates,4,2),$C3>=INDEX(TimeRates,4,3)),0,IF($D3<=INDEX(TimeRates,4,3),$D3,INDEX(TimeRates,4,3))-IF($C3<=INDEX(TimeRates,4,2),INDEX(TimeRates,4,2),$C3))*24
=IF(OR($D3<=INDEX(TimeRates,5,2),$C3>=INDEX(TimeRates,5,3)),0,IF($D3<=INDEX(TimeRates,5,3),$D3,INDEX(TimeRates,5,3))-IF($C3<=INDEX(TimeRates,5,2),INDEX(TimeRates,5,2),$C3))*24
I wound up hard coding the entries because ultimately these are going to wind up in the formula for G3 through I3. Otherwise I could have used the Range # column as index points.
So the next problem I had was adding ranges with the same name together. Could have used SUMIF if I was keeping the table, but in my head I could not as A) I was not keeping the table, B) This would eventually need to be in a row that could be copied down.
So I looked at what needed to be done for each season and it was not bad...IF the formula was short.
So basically need an IF statement looking for summer and follow the addition of appropriate seasonal ranges for each range type. Seems simple enough but it gave me the following formulas for G3 through I3:
=$E3*IF(OR(WEEKDAY($B3)={1,7}),0,IF($F3="Summer",IF(OR($D3<=INDEX(TimeRates,3,2),$C3>=INDEX(TimeRates,3,3)),0,IF($D3<=INDEX(TimeRates,3,3),$D3,INDEX(TimeRates,3,3))-IF($C3<=INDEX(TimeRates,3,2),INDEX(TimeRates,3,2),$C3)),IF(OR($D3<=INDEX(TimeRates,2,2),$C3>=INDEX(TimeRates,2,3)),0,IF($D3<=INDEX(TimeRates,2,3),$D3,INDEX(TimeRates,2,3))-IF($C3<=INDEX(TimeRates,2,2),INDEX(TimeRates,2,2),$C3))+IF(OR($D3<=INDEX(TimeRates,4,2),$C3>=INDEX(TimeRates,4,3)),0,IF($D3<=INDEX(TimeRates,4,3),$D3,INDEX(TimeRates,4,3))-IF($C3<=INDEX(TimeRates,4,2),INDEX(TimeRates,4,2),$C3))))*24
=$E3*IF(OR(WEEKDAY($B3)={1,7}),0,IF($F3="Summer",IF(OR($D3<=INDEX(TimeRates,2,2),$C3>=INDEX(TimeRates,2,3)),0,IF($D3<=INDEX(TimeRates,2,3),$D3,INDEX(TimeRates,2,3))-IF($C3<=INDEX(TimeRates,2,2),INDEX(TimeRates,2,2),$C3))+IF(OR($D3<=INDEX(TimeRates,4,2),$C3>=INDEX(TimeRates,4,3)),0,IF($D3<=INDEX(TimeRates,4,3),$D3,INDEX(TimeRates,4,3))-IF($C3<=INDEX(TimeRates,4,2),INDEX(TimeRates,4,2),$C3)),IF(OR($D3<=INDEX(TimeRates,3,2),$C3>=INDEX(TimeRates,3,3)),0,IF($D3<=INDEX(TimeRates,3,3),$D3,INDEX(TimeRates,3,3))-IF($C3<=INDEX(TimeRates,3,2),INDEX(TimeRates,3,2),$C3))))*24
=$E3*IF(OR(WEEKDAY($B3)={1,7}),$D3-$C3,IF(OR($D3<=INDEX(TimeRates,1,2),$C3>=INDEX(TimeRates,1,3)),0,IF($D3<=INDEX(TimeRates,1,3),$D3,INDEX(TimeRates,1,3))-IF($C3<=INDEX(TimeRates,1,2),INDEX(TimeRates,1,2),$C3))+IF(OR($D3<=INDEX(TimeRates,5,2),$C3>=INDEX(TimeRates,5,3)),0,IF($D3<=INDEX(TimeRates,5,3),$D3,INDEX(TimeRates,5,3))-IF($C3<=INDEX(TimeRates,5,2),INDEX(TimeRates,5,2),$C3)))*24
So the question is, is there way to use formulas that would allow for the tidying up of what is currently in place for cells G3 to I3 (last 3 formulas in this question) that can be copied downward?
UPDATE as requested
I am working on the table on the top, below it is my building block area for thoughts. You may recognize some of the shots above from it.
Using Excel 2013

How to add a point to a cell when the difference of a date is < 45 days in a range of cells

I have an excel spreadsheet where there is a start date and an end date. When a person closes a ticket in less than 45 days (the difference between the start date and the end date is <45days) they are awarded 0.5 points. They also need to be sorted by month. So dates in 11 would be allocated to the points in the November column. Here is what I have so far but is not working.
=SUM(IF(AND(MONTH(I2:I1000)=11)DATEDIF(I2:I1000,J2:J1000,"D")<=45,0.5,0)
I have tried a normal IF function as well but it doesnt seem to add properly when the ranges of cells are added.
| Start Date | End Date | November Points | December Points | |
|------------|------------|-----------------|-----------------|---|
| 11/12/2019 | 11/28/2019 | 1.0 | | |
| 12/1/2019 | 1/05/2019 | | 0.5 | |
| 11/12/2019 | 11/30/2019 | | | |
So here you can see when the start date is 11/12/2019 and the end date is 11/28/2019 0.5 points is added to the November Points cell(the total in that cell is 1.0 since there are 2 date ranges within 45 days for November). When the start date is 12/1/2019 to end date 1/05/2019 there is now 0.5 points in the December cell.
This might work:
| - | B | C | D | E | F | G |
|---|------------|------------|------|-----------------|-----------------|----------------|
| 2 | Start Date | End Date | Days | November Points | December Points | January Points |
| 3 | 12/11/2019 | 28/11/2019 | 16 | | | |
| 4 | 01/12/2019 | 05/01/2020 | 35 | | | |
| 5 | 12/11/2019 | 30/11/2019 | 18 | | | |
| 6 | | | | 1 | 0.5 | 0 |
Formula and formatting used:
Cell E2: 01/11/19 (or 11/01/19 in USA) and give the cell a custom number format of mmmm "Points" - do this for F2 & G2.
Column D: =DATEDIF(B3,C3,"d")
Totals in row 6: =COUNTIFS($D$3:$D$5,"<=45",$B$3:$B$5,">=" & E$2,$B$3:$B$5,"<" & F$2)/2
So the COUNTIFS will count how many days are <=45, will check that the dates in column B are >= 1st of the month, will check that the dates in column B are < 1st of the following month.

How to get the week number for day irrespective of the calender week number?

how to get the week number for the day irrespective of the calender week in excel?
Iam looking for the column "nth_week_number" .In the below table "2/5/2018" will fall in the 2nd week of the Feb 2018 but It was a 1st Monday of that Month.
Someone please help to resolve this in excel.
| Date | Weekday | week _of_the_month | nth_week_number |
|-----------|-----------|--------------------|-----------------|
| 2/1/2018 | Thursday | 1 | 1st Thrusday |
| 2/2/2018 | Friday | 1 | 1st Friday |
| 2/3/2018 | Saturday | 1 | 1st Saturday |
| 2/4/2018 | Sunday | 2 | 1st Sunday |
| 2/5/2018 | Monday | 2 | 1st Monday |
| 2/6/2018 | Tuesday | 2 | 1st Tuesday |
| 2/7/2018 | Wednesday | 2 | 1st Wednesday |
| 2/8/2018 | Thursday | 2 | 2nd Thrusday |
| 2/9/2018 | Friday | 2 | 2nd Friday |
| 2/10/2018 | Saturday | 2 | 2nd Saturday |
| 2/11/2018 | Sunday | 3 | 2nd Sunday |
| 2/12/2018 | Monday | 3 | 2nd Monday |
| 2/13/2018 | Tuesday | 3 | 2nd Tuesday |
Try this formula in row 2 and copy it down:
=(ROUNDUP(DAY(A1)/7;0)&". "&TEXT(A1;"dddd"))
This will output 2. Thursday for example, formatting it as 1st, 2nd, 3rd, etc. would require a bit more work. Please note that you might have to change the semicolons ; to regular commas , in an English version of Excel (I think).
Edit: Updated formula to fit your exact case, as suggested by #Ron Rosenfeld:
=(CHOOSE(ROUNDUP(DAY(B1)/7;0);"1st ";"2nd "; "3rd "; "4th "; "5th ")&TEXT(B1;"dddd"))

how to calculate overtime in excel using by weekly pay?

+-----------+-----------+------+
| Day | Reg.Hours | OT |
+-----------+-----------+------+
| Monday | 8 | 0.75 |
| Tuesday | 8 | 0.5 |
| Wednesday | 8 | 1 |
| Thursday | 8 | 0 |
| Friday | 8 | 2.25 |
| Saturday | 0 | 0 |
| Sunday | 0 | 0 |
| Monday | 8 | 0 |
| Tuesday | 5.5 | 0 |
| Wednesday | 8 | 3.25 |
| Thursday | 8 | 2.75 |
| Friday | 8 | 0.5 |
| Saturday | 0 | 0 |
| Sunday | 0 | 0 |
+-----------+-----------+------+
Rules are:
Monday to Sunday, at least, work 40 Reg.Hours to get any overtime for that week.
In the above data set for the 2nd week Monday to Sunday for Reg.Hours total is 37.5 that means, the company will deduct 2.5 OT hours from the 2nd week of Monday to Sunday OT hours.
How do I calculate in Excel with a formula for calculating both weeks OT in 1 excel formula?
Let's say per hour pay is $30.00
You could do this with an Array Formula, but as you only have the days of the week, not the actual dates (which are unique) in your table, I think this will be easier with a helper column.
Assuming the first 3 columns are A, B, and C I would add a helper column in column D, starting with D2 and copied down as follows:
=IF(A2="Sunday",MAX(0,SUM(OFFSET(B2,-6,0,7,2))-40),"")
What this does is: Once a week (on Sunday, at the end of the week), Excel will sum up the previous 7 days of data, including column B & C. This is the total number of hours worked. That amount, minus 40 [limited to 0, if < 40 hours are worked], represents the total number of OT hours worked that week.
The fact that you have a regular hours column and an OT column is a bit of a red herring - instead of checking whether any OT was worked on any day, and then subtracting by the number of regular hours NOT worked on other days, just compare the whole week's work to 40 and end the calculation there.
I think the easiest way would be to use a iamge to demonstrate the answer
Assuming Day is Column A, Row 1, Regular is Column B, Row 1, and OT is Column C, Row 1:
First, Calculate total regular and OT hours for each week in the pay period:
Col. A Col. B Col. C
On Row 17, Calculate Regular/OT Hours: Week 1 =SUM(B2:B8) =SUM(B9:C15)
On Row 18, Calculate Regular/OT Hours: Week 2 =SUM(C2:C8) =SUM(C9:C15)
On Row 19, Calculate:
Regular Hours =IF(B17+C17>=40,40,B17+C17)+IF(B18+C18>=40,40,B18+C18)
OT Hours =IF(B17+C17>=40,B17-40+C17,0)+IF(B18+C18>=40,B18-40+C18,0)
Hope this helps.

Resources