Rounding to two specific times - excel

I need a formula that will round to either a time 15 minutes or 45 minutes past the hour based on the minutes in a time..
Example: If I have a time in A1 and the minutes are 46 and 14, I want the formula to return the time of 15 past the hour.
If A1 has 11:47 then return 12:15.
If I have a time in A1 and the minutes are between 16 and 44, I want the formula to return the time of 45 past the hour.
If A1 has 12:23 then return a time of 12:45
Any time that is either 15 or 45 past the hour will not change.

=CEILING(A1+TIME(0,15,0),TIME(0,30,0))-TIME(0,15,0)
Add 15 minutes
Roundup to the nearest 30 minutes
Subtract 15 minutes

This is a bit complicated but... (updated)
=(CEILING((A1-(15/(24*60)))/(1/48), 1)*(1/48))+(15/(24*60))
It goes something like this:
Subtract 15m to the time
Divide by 30m
Round up this value
Multiply by 30m
Add 15m

Here is a formula using the logic from my comment above:
=IF(AND(A1>=0,A1<=15),15,(IF(AND(A1>15,A1<=45),45,15)))
Remember, you will also need to add 1 to the hour in the final case (where A1 > 45).
Here is a breakdown of the above formula:
1. If A1 is between 0-15, then it will return 15.
2. If A1 is between 16-45, then it will return 45.
3. If A1 is between 46-59, then it will return 15. (the logic to add an hour is not handled in this formula)

Related

How to convert UTC to PST in Excel for day and time formats?

In excel, I have a cell which is
13 05:58:57
which represents day 13, 5 am, 58 minutes, 57 seconds in UTC time.
I would like to convert to PST, which should be
12 10:58:57
which is day 12, 10 pm, 58 minutes, 57 seconds.
The day is just a day of the month. Is there a way to do this quickly in excel?
Excel stores date_times as numbers, where each day is 1, and the decimal part of the number is the time (so 0.25 is 6 hours, and 0.7 is 16 hours + 48 minutes)
Time 0 is the beginning of 1899/12/31, so =today() formatted as a decimal currently shows me as 44730.4556, being 44730 days since 1899 and almost eleven a.m.
Once you have your data in that format it is trivial to E.g.:
subtract 7 hours = A2 - 7/24
add 2 days 6 hours and 15 minutes = A2 + 2 + 6/24 + 15/24/60
find the difference between two date_times
So you want to get the input data into that form, from which point you just use formatting.
To see where you are going, enter in A1 = now(), in B1 =A1, in C1 = B1.
Change the format on B1 - Right Click | Format Cells | Custom | yyyy-mm-dd hh:mm:ss.
Then in C1 try changing to a format you want: dd hh:mm:ss AM/PM.
For a full list of options see https://support.microsoft.com/en-au/office/format-numbers-as-dates-or-times-418bd3fe-0577-47c8-8caa-b4d30c528309
I will assume that your data starts at A4, but I can't tell what is actually stored in the cell, as opposed to what it displays.
If your input data is actually already a date_time (what does it look like if you format is as a decimal?) all you need to do is =A4-7/24.
If your input data is actually a string, then separate it into day, hour, minute, second in C, D, E, F.
If single-digit days have leading zeros then just =left(A4,2) then =mid(A4,4,2) etc.
If there are no leading zeros put in column B =find(" ",A4) and point the lefts and mids to that intermediate result.
Then in G put = C4 + D4/24 + E4/24/60 + F4/24/60/60 so you have the input data as an Excel-formatted time.
And subtract the 7 hours difference with =G4-7/24
P.S. If you need to cope with month-ends and DST then you need to add year and month to the data in G - currently it has date_times in January 1899.

SUBSTITUTE formula to separate hours and minute by comma

I have formula to separate hours and minutes by comma.
=CEILING(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(P96;"s";"");" min";"");" hour ";",");0,1)
This works for 4 hours 40 mins and 1 hour 10 mins. However there might case like 20 mins. Then my formula is displaying 20 that is not correct and should be 0,2. Any suggestions how to fix this issue?
Note! Values like 4 hours 40 mins are in one cell, in this case in cell P96. Information comes straight from Google Maps xml.
=CEILING(IF(ISNUMBER(SEARCH("hour";P96));SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(P96;"s";"");" min";"");" hour ";",");"0,"&SUBSTITUTE(SUBSTITUTE(P96;"s";"");" min";""));0,1)
Conditionally add a dummy prefix that satisfies the hours substitution.
=CEILING(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(IF(ISERROR(SEARCH("hour"; P96)); "0 hours "; "")&P96;"s";"");" min";"");" hour ";",");0,1)
Type your time in cell A1. As an example, you could type "2:30", "14:30" or "2:30 PM".
Type "=HOUR(A1)" in cell A2 to produce only the hour. As an example, extracting the hour "2:30 PM" would give you "14," because 2 PM is the fourteenth hour in the day.
Type "=MINUTE(A1)" in cell A3 to extract the minutes.
Type "=SECOND(A1)" in cell A4 to extract the seconds.

Time Lookback Calculation

I have two columns:
Column A: Time (seconds from midnight)
Column B: Value (arbitrary value I've created)
How would I find the average value (column B) at any instance in time(row) looking back over the previous x seconds (column A)?
Lets assume A1 = Seconds after midnight
Seconds after midnight Value
0 27
2 29
6 2
16 29
20 19
24 4
34 2
40 1
44 4
54 12
64 12
71 3
81 30
91 21
92 1
93 27
97 12
104 30
112 25
Note that time deltas are variable so I can't just look at the last x rows.
A specific question would be:
In a new column (column C) return the average Value of the last 10 seconds of data.
I have no idea how to do this. Can anyone help? It'd be greatly appreciated.
EDIT:
The output in the first 4 rows of column C would be:
Seconds after midnight Value Result
0 27 No values prior to this one
2 29 27 Average(B2)
6 2 28 Average(B2:B3)
16 29 2 Average(B4)
For each row, I'm taking the current time (16 in the last case)...going back x seconds (10 seconds in this case) and then averaging the values from all the cells in that time range (not including the current value).
My main issue is the time calculation. I don't know how to calculate it as I roll forward in time and I continue to get more instances as we move forward. If we go 10 seconds without any new data point then there would be no output since. If we got one instance in the previous 10 seconds then the output would be that value. If we got 100 instances in the previous 10 seconds, I need to return the average of that 100 instances.
Again, I'd appreciate any help, hints, links. This is driving me crazy.
Try the following formula in cell C3:
=IFERROR(SUMIFS(B:B,A:A,"<"&A3,A:A,">="&A3-10)/COUNTIFS(A:A,"<"&A3,A:A,">="&A3-10),0)
SUMIFS will get the sum of all values for which the seconds are:
less than A3 (i.e. less than 2 in this case)
more or equal to A3-10 (i.e. above -8 in this case)
And this sum is divided by the number of lines found with the same criteria.
If now there is an error (more specifically when the COUNTIFS returns 0, you would get #DIV/0!) you simply get 0 instead of the error.

Only display hours if time is greater than 60 minutes

I'm using a time-tracking sheet that calculates the time spent on a job by doing a simple subtraction between a start time and an end time in two cells.
The hh:mm format would suffice in this case, or as a small modification I'm using [h] "h" mm "m", but the issue with both is that I do not want the hours to display when there are none (when hours = 0).
If for example 35 minutes are logged I would like the output to be 35 m and not 0 h 35 m. The [mm] format might be an option then, but I would still like my time above 60 minutes to be displayed as 1 h 30 m, and not 90 m.
Please try:
[<0.04166666][mm] "m";[h] "h" mm "m"
Not at all elegant, but how about this:
=IF(HOUR(time)<1,MINUTE(time)&"m",HOUR(time)&"h "&MINUTE(time)&"m")
Returns a cell value 02:24:00 as "2h 24m"
and a cell value 00:14:24 as "14m"

Rounding Up Minutes above 8, 23, 38, 53 to the nearest quarter hour

Here’s a challenge for someone. I’m trying to round session times up to the nearest quarter hour (I report my total client hours for license credentialing)
8 minutes or above: round up to 15
23 minutes or above: round up to 30
38 minutes or above: round up to 45
53 minutes or above: round up to 60
Ex: in the first quarter hour, minutes below 8 will be their exact value: 1=1, 2=2, 3=3.
When 8 is entered, it is automatically rounded up to 15 (the same holds true for the rest of the hour: ex: 16=16, 17=17, 18=18, 19=19, 20=20, 21=21, 22=22. But 23 through 29 are all rounded up to 30.
Ideally, I could enter both hours and minutes in a single column, ex: 1.54
However, I realize that it may be necessary to create a separate column for hours and minutes in order to make this work (i.e., so that my formula is only concerned with rounding up minutes. I can add my hours and minutes together after the minutes are rounded.) Thus:
Column A = Hours (3 hours maximum)
Column B = Minutes
Column C = Minutes Rounded up to nearest ¼ hour
Column D = Col A + Col C
In column B I would like to enter minutes as 1 through 60 (no decimal- i.e., in General, not Time format)
38 minutes in column B would automatically be rounded up to 45 minutes in column C
Does anyone have any ideas? How can I do this using the fewest number of columns?
[A Previously posted question - "Round up to nearest quarter" - introduces the concept of Math.Ceiling. Is this something I should use? I couldn't wrap my head around the answer).
With Grateful Thanks,
~ Jay
How's this go?
DECLARE #time DATETIME = '2014-03-19T09:59:00'
SELECT CASE
WHEN DATEPART(mi, #time) BETWEEN 8 AND 15 THEN DATEADD(mi, 15-DATEPART(mi, #time), #time)
WHEN DATEPART(mi, #time) BETWEEN 23 AND 30 THEN DATEADD(mi, 30-DATEPART(mi, #time), #time)
WHEN DATEPART(mi, #time) BETWEEN 38 AND 45 THEN DATEADD(mi, 45-DATEPART(mi, #time), #time)
WHEN DATEPART(mi, #time) BETWEEN 53 AND 59 THEN DATEADD(mi, 60-DATEPART(mi, #time), #time)
ELSE #time
END
Assume "sessions" is your table (CTE below contains 2 sample records), with session start time & end time stored (as noted in comments above, just store these data points, don't store the calculated values). You might be able to do the rounding as below. (not sure if this is what you want, since it either rounds up or down... do you not want to round down?)
;WITH sessions AS (
SELECT CAST('20140317 12:00' AS DATETIME) AS session_start, CAST('20140317 12:38' AS DATETIME) AS session_end
UNION ALL
SELECT CAST('20140317 12:00' AS DATETIME), CAST('20140317 12:37:59' AS DATETIME) AS session_end
)
SELECT *, DATEDIFF(MINUTE, session_start, session_end) AS session_time
, ROUND(DATEDIFF(MINUTE, session_start, session_end)/15.0, 0) * 15.0 AS bill_time
FROM sessions;
EDIT:
Hi Jay, I don't think you mentioned it is an Excel problem! I was assuming SQL. As Stuart suggested in a comment above, it would be helpful if you modified your question to indicate it is for Excel, so that others can possibly get help from this dialog in the future.
With Excel, you can do it with two columns that contain the session start date and time (column A) and session end date and time (column B), plus two formulas:
Column C (Actual Minutes) = ROUND((B1-A1) * 1440,0)
Column D (Billing Minutes) = (FLOOR(C1/15, 1) * 15) + IF(MOD(C1,15) >= 8, 15, MOD(C1,15))
This is what my table looks like:
3/18/2014 12:00 3/18/2014 12:38 38 45
3/18/2014 14:00 3/18/2014 14:37 37 37

Resources