Average column by specific datetime associated values - excel

I have one column with the time in format "dd/mm/yyyy hh:mm" and another with the temperature for that time point. I am looking to calculate the average temperature of the day and night of each month separately. I.e. average all temperatures between 06:00 and 18:00 in May and all temperature between 18:00 and 06:00 in May and then the same for March and so on.
Time Celsius(C)
06/05/2016 10:49 28
06/05/2016 11:49 29
06/05/2016 12:49 31
06/05/2016 13:49 27.5
06/05/2016 14:49 24
06/05/2016 15:49 25
06/05/2016 16:49 24.5
06/05/2016 17:49 23.5
06/05/2016 18:49 23
06/05/2016 19:49 22.5
06/05/2016 20:49 22.5
I am currently using the following formula:
=AVERAGEIFS(C2:C3643,B2:B3643,">=01/05/2016",B2:B3643,"<=31/05/2016",B2:B3643,">=01/05/2016 06:00",B2:B3643,"<=31/05/2016 18:00")
To try and calculate an average if the date is within May and during the day - however it doesn't appear to be working and when I change the hour periods it still spits out the same number (which is the average for the month).

You can use a long SUMPRODUCT Formula:
For the 600 to 1800 in May:
=SUMPRODUCT(($A$2:$A$12>=DATE(2016,5,1))*($A$2:$A$12<=DATE(2016,5,31))*(MOD($A$2:$A$12,1)>=TIME(6,0,0))*(MOD($A$2:$A$12,1)<=TIME(18,0,0))*B2:B12)/SUMPRODUCT(($A$2:$A$12>=DATE(2016,5,1))*($A$2:$A$12<=DATE(2016,5,31))*(MOD($A$2:$A$12,1)>=TIME(6,0,0))*(MOD($A$2:$A$12,1)<=TIME(18,0,0)))
You can always replace all the DATE() and TIME() parts with cell references instead of hard coding them.
To get the between 1800 and 600 we need to shift it to an OR with + between the Time Boolean instead of *:
=SUMPRODUCT(($A$2:$A$12>=DATE(2016,5,1))*($A$2:$A$12<=DATE(2016,5,31))*((MOD($A$2:$A$12,1)<=TIME(6,0,0))+(MOD($A$2:$A$12,1)>=TIME(18,0,0)))*B2:B12)/SUMPRODUCT(($A$2:$A$12>=DATE(2016,5,1))*($A$2:$A$12<=DATE(2016,5,31))*((MOD($A$2:$A$12,1)<=TIME(6,0,0))+(MOD($A$2:$A$12,1)>=TIME(18,0,0))))

This relies on you creating a table of months and time ranges like below:
Enter this formula in E2 and drag around as needed. It's an array formula, so must be entered with Ctrl-Shift-Enter:
=AVERAGE(IF(
((MONTH($A$2:$A$101)=MONTH(E$1&1))*
((MOD(HOUR($A$2:$A$101)-LEFT($D2,2),24))>=0)*
((MOD(HOUR($A$2:$A$101)-LEFT($D2,2),24))<12)),
$B$2:$B$101))
Notes:
The MONTH(E$1&1) part lets you get a month number from text like
"Jan"
I used MOD and subtraction of the left part of the time range to get
the target hour in a range from 0 to 23. This made it possible to filter on values between 1 and 11.
If Barry Houdini were still around he could do it in half the space, I'm sure.

While working with datetime and when the actual day is reckoned beyond midnight like in 18 hrs to 6 hrs the next day, I find it useful to offset the time back and do the calculations.
6:00 to 18:00 =AVERAGE(IF((MONTH($A$2:$A$12-0.25)=D2)*(MOD($A$2:$A$12-0.25,1)<0.5),$B$2:$B$12,""))
18:00 to 6:00 =AVERAGE(IF((MONTH($A$2:$A$12-0.25)=D2)*(MOD($A$2:$A$12-0.25,1)>=0.5),$B$2:$B$12,""))
These are array formulas entered with Ctrl-Shift-Enter.
Here I am offsetting time by 0.25 days which is 6 hours.

Related

how to reference data from same point of time over last 30 days in Excel?

I have a data set of 1 minute data in Excel of last 30 days.
I am trying to compare today's value at 05:45:00 with average of last 30 days value at same time (05:45:00).
Completely stuck.
Can anyone help: Dataset looks like this:
Time Value
05:30:00 11.00
05:31:00 6.00
05:32:00 20.00
05:33:00 50.00
05:34:00 69.00
05:35:00 31.00
05:36:00 90.00
05:37:00 56.00
05:38:00 26.00
05:39:00 24.00
05:40:00 22.00
05:41:00 71.00
05:42:00 80.00
05:43:00 85.00
05:44:00 41.00
**05:45:00 44.00**
05:46:00 28.00
05:47:00 43.00
I did try to convert days in to minutes by doing
=(last day - first day)*1440
after that I am completely stuck what needs to be done. Not very proficient with Excel.
Are you aware how datetime works in Excel?
It is based on the value "1" being equal to one day.
So, the hour-value "05:30" equals "=5.5/24" as you can see from this screenshot:
So, If you want to add 30 days to a timestamp, just add the value 30.

Allocating staff to 30 minute intervals

I’m trying to create either a spreadsheet or macro that allows me to put and employee start and finish time in and then show me by each half hour how many half hours they are in.
For example;
Employee 1 - 08:30 to 10:15
The output I would want is;
08:00 0
08:30 1
09:00 1
09:30 1
10:00 0.5
10:30 0
11:00 0
So between 8:30 and 9:00 shows as one because that employee was in during that half hour. But between 10:00 and 10:30 it shows 0.5 because the employee was only in for half of that period
Then as I add more data it tells me which half hours they are covering.
I’ve tried using the ceiling and floor values, counta, countif, countifs, v and hlookups but just can’t get the desired effect.
I’m guessing it would need to be a macro.
Assuming C1 and D1 contain the start and end times of a shift and column A contains the times, use the following formula in column B:
=IF(A2>D$1,C2,IF(AND(A2>=C$1,A3<=D$1),1,MAX(0,A3-D$1)/(1/48)))
Limitations: shifts cannot cross over days, days cannot start before 00:00 or end after 23:59

How to split the time between two dates over daily columns

I am in much need of help please.
I have the table below.
It has about 300 rows and columns on the right for each day of the year starting 2015, ending 2019. I only entered a few columns here as an example.
What I need:
A formula to split the time (it can be hours or decimal days, it doesn't matter) between the two days on the left on the columns to the right.
In the rest of the days, which are not included in the period, I need a zero in those columns.
I manually entered the results I need so it can help you understand.
E.g.
On the first row:
The employee has spent
15 hours at destination in the first day of travel,
24 hours in the second day of travel
11:50 hours in the last day of travel.
All the rest of the columns to the right, which are not here, starting 1/4/2015 23:59 should give the result zero.
Thank You!!
Entry Date Exit Date 1/1/2015 23:59 1/2/2015 23:59 1/3/2015 23:59
1/1/15 9:00 1/3/15 11:50 15.00 24.00 11.50
As far as I can tell, it boils down to this
1.0 days 2.0 days 3.0 days 4.0 days
period=p in h/d.d p-(1-(so far)) p-(2-(so far)) p-(3-(so far)) 0
EDIT 2: I've come up with a formula.
NOTE:
This formula assumes existence of a column that specifies 0 hours of work between the "end date/time" column ("B") and first work hour ("D"), so you have it like this:
A B C D E F G H
1 Entry Date Exit Date ZERO 1/1/2015 23:59 1/2/2015 23:59 1/3/2015 23:59 1/4/2015 23:59 1/5/2015 23:59
2 1/1/15 9:00 1/3/2015 11:50 0 0.6243055556 1 0.49375 0 0.00
3
The extra column is there to avoid circular dependencies, since
we must "iteratively" accumulate the hours
The main formula for row 2 from column D being:
=MIN(D1-$A2,MIN(MAX(($B2-$A2) - SUM($C2:C2), 0),1))
Note: semicolons may be needed instead of commas in Excel as delimiters.
Note: the last digit in the formula depends on the unit. Here it is in days decimal. If hours, put 24.
In plain English, for a cell
Calculate sum of hours/days decimal in the range C to wherever - 1
Subtract result from the difference between start and end (total hours)
If less than 0, put 0 (MAX(x, 0))
If more than 1, put 1 (MIN(x, 1))
Choose between the result, or the difference between current date and start time, if it is smaller

Convert date & time in Excel, to time only, and round to nearest 30 minutes

I need a formula to convert excel date & time to time only and nearest 30 minutes. Example 1/5/2017 4:38:29 PM convert to 1630
A1 B1
1/5/2017 4:38:29 PM 1630
1/5/2017 5:03:40 PM 1700
1/5/2017 4:39:27 PM 1630
I tried if function & vlookup with
=MROUND((TEXT(A1,"hhmm")),1/48)
but I get 0:00 answer, and also
=MOD(Y5,"0:30")
but I get a 0 answer.
It looks like you were almost there. You need MOD to get the decimal portion of a datetime (i.e. the time) then MROUND to the nearest half-hour.
=MROUND(MOD(A1, 1),TIME(0,30,0))
FLOOR and CEILING operate the same as MROUND in case you require the lower or higher half-hour.
You may need to format the cell for the desired time format mask.
Assuming data starts in A1, copy the following down to suit:
=HOUR(A1)&IF(MINUTE(A1)>29,30,"00")

Determine if time is before or after specific time

I have an Excel sheet with two date-time columns: Start Date and End Date. I need to calculate overtime by determining how many hours and minutes were worked before 08:00 and after 16:30.
Examples:
COLUMN A COLUMN B
1 2017/01/04 16:00 2017/01/04 18:00
2 2017/01/05 18:00 2017/01/05 21:00
3 2017/01/08 08:00 2017/01/08 17:00
4 2017/01/09 07:00 2017/01/09 09:00
I know the calculation for duration between times (=TEXT(B1-A1,"h:mm")), but I'm struggling to determine if a date-time is before 08:00 or after 16:30.
I've played around with TIMEVALUE("8:00:00"), TIMEVALUE("8:00:00 AM") and TIME(8,00,00) in my calculation, but the it did not work,
e.g. =IF(A1<=TIMEVALUE("8:00:00 AM"),"Yes","No") returns "No" even for A4 (2017/01/09 07:00)
I'm struggling with the calculation and the logic on this one. So far i'm using,
=IF(MOD(F2,1)<8/24,8/24-MOD(F2,1),0)+IF(MOD(G2,1)>16.5/24,MO‌​D(G2,1)-16.5/24,0)
Try this:
=IF(TIMEVALUE(A1)<=TIMEVALUE("8:00:00 AM"),"Yes","No")
Stick with treating time as time; only treat time as text when you have bad data (time as text is bad data) and then only enough to get it to be time as time.
To get the number of hours and minutes before 08:00 AM,
=MAX(0, TIME(8, 0, 0)-TIMEVALUE(TEXT(A2, "hh:mm;0;#")))
To get the number of hours and minutes after 16:30 AM,
=MAX(0, TIMEVALUE(TEXT(B2, "hh:mm;0;#"))-TIME(16, 30, 0))
In the following image you can see that I have left one group as true time and the forced the following group to be time as text¹. Both return correct results.
¹ Unless specifically formatted otherwise, numbers and dates/times are right-aligned in a cell; text is left-aligned.

Resources