Excel function for First Row and Last Row of group - excel

I have a gate keeping report with a number of entry/exit times for an employee over a 24hr period.
I need another formula to go into I40 which is the difference between the first entry time - last entry time for each employee eg. I40 = F50 - D40.
Dont worry about the formula regarding the subtraction of dates as I have this. I really just need the formula that will allow me to get the Last Exit time cell and the First Entry time cell for each employee.

The best way is to always store datetime values (ie, 2018-05-24 13:454) instead of just the times. You could still display it as a time by changing the cell's formatting to a time format.
Shortcut to Number Formatting options: Ctrl+1
There are many advantages, including that "regular math" will still work even if a shift starts in a different day than it ends.
If you must stick with only times, you can still calculate it correctly (up to a 23.9-hour shift) with an IF statement to add a day if the returned value is negative.
For example, if your existing formula works for same-day shift, and is:
=F50-D40
...then you could change it to:
=IF(F50-D40<0,F50+1-D40,F50-D40)
More Information:
Office.com : How to use dates and times in Excel
Office.com : Add or subtract time (Excel)
EDIT:
Looking at your question again, perhaps I misunderstood what you were trying to ndo. It's a little unclear, but you mention the fist and last times.
If you mean the "earliest and latest", you can get those using MIN and MAX. If the crossing-midnight is an issue here too, you'll need to see my first suggestion above, or else add a "helper column" to determine which times are before which.
Storing datetime is still best and this all would have been avoided.

try the below to get the difference in hours.
=(E50+F50)-(C40+D40)

Related

Subtracting times across a day in excel

I am working on the capstone project in of the Google Career Certificate in Data Analytics. I am using Microsoft Excel. I have to calculate the ride length based on the start and end ride times. I've inputted the formula =F2(end time)-D2(start time) which returns the ride length. Going through my entire list I have some areas where the start time is like 11pm and the end time is 1am and this is returning ###### because it is a negative number with the regular formula. I've found a modified formula that can kind of do the conversion I am looking for but it is still a bit problematic. The modified formula is =(F2-D2+(F2<D2))*24 and it seems to give an accurate ride length if I reformat the answer to number. The issue is the rest of my data is in time format and the modified ones are in number format. If I convert the number values to time, the ride length values are inaccurate.
It is tricky to make the numeric value change as well due to me using a formula. I can correct them one by one after I save Excel and it no longer stores the numbers as the formula, but there are lots of data points to change and that would be time consuming. I'm hoping to find a more concise way to solve this problem. Maybe with a better formula.
[Snippet of the chart 1
Just like everything in life, there are multiple ways to achieve things. I would have formatted the date and time into a single cell; but. if you're gathering the data from another source, that's understandable.
A simple IF statement here will work. IF the days are one apart, then take '1' day off the starting time, else do your original formula:
=IF(E4-C4=1,F4-(D4-1),F4-D4)

I need to average cells in a column based on the time of day in another column in excel, i.e. morning, afternoon and night

I am trying to get the averages of a column that meet certain criteria into a single cell.
The following formula works: =AVERAGEIFS(tblData[Sys],tblData[Time],">=12:00 PM",tblData[Time],"<6:00 PM") but when I adjust the time values to: =AVERAGEIFS(tblData[Sys],tblData[Time],">=6:00 PM",tblData[Time],"<4:00 AM") I get an error. I'm guessing it's because the time range goes into the next day.
Is there a better function to use or a workaround?
This formula seems to work for you scenario. It checks for the possibility that the end time is before the start time (but on the next day) and changes the logic accordingly:
=AVERAGE(
IF(
IF(tmStart>=tmEnd,
(tblData[Time]>=tmStart)+(tblData[Time]<=tmEnd),
(tblData[Time]>=tmStart)*(tblData[Time]<=tmEnd))
=1,tblData[Sys],""))
It's important to understand this function does, and the underlying intentions are - I elaborate in the what follows, proffer a workable solution, provide a reconciliation, as well as a link to the workbook with screenshot below.
You will need to specify # days spanned - even the scenario that 'worked' for you (i.e.. =AVERAGEIFS(tblData[Sys],tblData[Time],">=12:00 PM",tblData[Time],"<6:00 PM") could, in theory, span 2 (or more) days. The fact that this 'works' (doesn't return #DIV/0!) is that the 'intersection' of conditions is an non-empty set (i.e. {12pm-5pm}).
As I say, if this was intended to be ">=12:00" from day 1 through to "<6pm" the following, there is no way of determining whether this is indeed the case by simply 'comparing the times' (e.g. 12pmvs 6pm).
Screenshot/here refer::
=IF($I$5="Y",(SUM(1*($C$5:$C$28*(($D$5:$D$28>=$J$3)+1*($D$5:$D$28<$K$3)))))/SUM((($D$5:$D$28>=$J$3)+1*($D$5:$D$28<$K$3))),IFERROR(AVERAGEIFS(C5:C28,D5:D28,">="&J3,D5:D28,"<"&K3),"times don't intersect! "))
where: I5 = 'Y' or 'N' (i.e. multiple days'). When scenario B is selected, with multiple days = 'Y', outcome = 12.9 which reconciles to a manual calculation.

Excel Group rows based on time interval

I have the following excel result:
I want to group the above result in groups based on sessions i.e. if the time gap between two successive timestamps is greater than 5 minutes, it must be a new row.
For example :
I need some formula to achieve this. As I'm fairly new to Excel this is causing to be a major headache for me. Please help me, if anyone knows how to do it or at least point me in a direction.
Thanks a ton !!!
Judging by your screenshot, it appears your timestamps are actually text values. Text by default is usually left aligned where as numbers are right aligned. You seem to have a space at the end of your time stamp suggesting that it is probably left aligned and therefore text. You can test it with the following formula which will return TRUE if its text.
=ISTEXT(P2)
where P2 is one of your time stamps.
CONVERT TIMESTAMPS TO TIME
There are a variety of ways to do this. Some will depend on system settings. Take a look at the following functions as each might be useable depending on your system. The first two are a guarantee, the last two are more dependent on system settings.
DATE
TIME
DATEVALUE
TIMEVALUE
Something important to remember here is that in excel dates are integers counting the days since 1900/01/01 with that date being 1. Time is stored as a decimal and represents fraction/percentage of a day. 24:00:00 is not a valid time in excel though some functions may work with it.
So in order to convert your time stamp in P2 I used the following formula to pull out the date:
=DATE(LEFT(P2,4),MID(P2,FIND("-",P2)+1,2),MID(P2,FIND(" ",P2)-2,2))
Basically it goes into the text and strips out the individual numbers for Year, Month and Day.
To pull out the time, I could have done the same procedure but elected to demonstrate the TIMEVALUE method which is a little more robust than DATEVALUE and not a subjective to system settings as much. With the following formula I stripped out the whole time code (MINUS"UTC"):
=TIMEVALUE(TRIM(MID(P2,FIND(" ",P2)+1,FIND("UTC",P2)-FIND(" ",P2)-1)))
I also made an assumption that you are not mixing and matching UTC with other time zones which means it can be ignored. Now to get DATE and TIME all in one cell, you just need to add the two formulas together to get:
=DATE(LEFT(P2,4),MID(P2,FIND("-",P2)+1,2),MID(P2,FIND(" ",P2)-2,2))+TIMEVALUE(TRIM(MID(P2,FIND(" ",P2)+1,FIND("UTC",P2)-FIND(" ",P2)-1)))
In the example at the end, I placed that formula in Q2 and copied down
DELTA TIME
Since you want to break your groups out based on a time difference between individual entries, I used a helper column to store the time difference. In my example at the end I stored this difference in Column S. The first entry is blank as there is no time before it. I used the following formula in S3 and copied it downward.
=Q3-Q2
I applied the custom formatting of [h]:mm:ss to the cell to get it to display as shown.
FIND GROUP BREAK POINTS
In my example I am using helper column T to hold breakpoint flags. At a minimum, you will have two break points. Your first time entry and your last time entry. To make like simple I simply hard coded my first breakpoint flag in T2 as 1. Stating in T3, Three checks need to be made. If any of them are TRUE then the next flag needs to be added with a value increase by one. the three checks are:
Is this the last entry
Is the next time delta greater than 5 minutes (means end of a group)
Is this time delta greater than 5 minutes (means start of a group)
Based on those three checks I placed the following formula in T3 and copied down:
=IF(OR(S4="",S4>TIME(0,5,0),S3>TIME(0,5,0)),MAX($T$2:T2)+1,"")
Note the $ on the first part of the range for the MAX function. This will lock the start of the range while the formula gets copied down while the end of the range increases accordingly.
Also the row after the last time entry must be blank. IF it is not blank and has a set value in it, change the S4="" to S4="set value".
GENERATE TABLE
There are multiple ways to reference the flags and pull the corresponding times. a couple of formulas you can look into are:
INDEX / MATCH
LOOKUP
In this example I elected to use LOOKUP though I believe INDEX and MATCH are more appropriate and robust. For starters we want to generate a list of ODD number and EVEN numbers. These represent the start and end of the groups and correspond to the flags set in column T. One way to generate ODD and EVEN numbers as you copy down is:
=ROW(A1)*2-1 (ODD)
=ROW(A1)*2 (EVEN)
The next step is to find the generated number in Column T and then pull its corresponding timestamp in Column Q. I did this with the following formula in V2 and copied down.
=LOOKUP(ROW(A1)*2-1,T:T,Q:Q)
And in W2
=LOOKUP(ROW(A1)*2,T:T,Q:Q)

Adding numeric value based on tickbox in Sharepoint formula

I'm trying to create a simple leave planner application using Sharepoint. I've got the bulk of it working but I'm going back to do edge cases now like Bank Holidays and half days. So I've added a checkbox column and if ticked, I want it to deduct 0.5 from the total value (half-day). The formula that's working for the full days is:
=(DATEDIF(dateFrom,dateTo,"D"))-INT(DATEDIF(dateFrom,dateTo,"D")/7)*2-IF((WEEKDAY(dateTo)-WEEKDAY(dateFrom))<0,2,0)+1
So I just created another two columns called shalfday and ehalfday. If they're ticked then deduct 0.5 from the total (If dates match and both ticked then deduct 0.5 still).
I've tried playing round with things like
-IF([shalfday],"0.5")
and other variants as google results are not being too kind this morning but they're returning #NAME? variables.
Any pointers on the syntax or what I should be looking at?
I would suggest:
-IF([shalfday],0.5,0)
Since you are trying to subtract you should work with integers and not strings (i removed the quotation marks).
I ended up doing this another way. I instead asked the user to specify how many half days were in their leave in another site column and used this suffix in the formula. It also got rid of the validation check to make sure the user wasn't taking two half days on 1 days leave.
New site column called 'Total Half Days' set to number, default value of 0;
Appended to original formula:
-([Total Half Days]/2)+1
Full formula:
=(DATEDIF(dateFrom,dateTo,"D"))-INT(DATEDIF(dateFrom,dateTo,"D")/7)*2-IF((WEEKDAY(dateTo)-WEEKDAY(dateFrom))<0,2,0)-([Total Half Days]/2)+1

MS-Excel Negative times

I'm writing a spreadsheet for a shop manager. What it does is keep track of the number of hours a worker has worked.
So you enter times for Monday-Sunday, and then an adjustment - e.g. if they work 40/40/40/32 hours for the month, then you would have an adjustment of -2/-2/-2/+6 to bring the worker to the 38 hour week that he's being paid for. Some (most) weeks may be adjusted for overtime. The spreadsheet then totals the hours.
This spreadsheet is supposed to just be a self-calculating version of a paper form.
It needs to match the paper form as it has to be substituted for the old form which is given to some other member of the company (pay clerk, I don't know; I'm not rebuilding their whole system, just replacing a form)
I'm having trouble entering a negative time in the adj field - the field has a [h]:mm formatting. and when i enter a negative time (e.g. -2:00) it displays an error, saying "incorrectly formatted equation", with the suggestion that if I was entering a string then I should prefix with a apostrophe.
How do I overcome this?
Tools - Options - Calculation - 1904 date system
Check this box to use the 1904 (Mac) date system and you will be able to use negative dates and times. I'm not sure how this will effect existing spreadsheets, so maybe someone else can speak to that.
According to Excel...
"Dates and Times that are negative appear as ########"
Doesn't sound like you're going to be able to do that with an auto-summation formula. You'll have to set the formatting as none and just type it in (which defeats the purpose).
I am solving the same problem. Setting for date formatting "1904" is necessary for both below described solution.
You can enter an equation as a result of predeceasing cells like C5-C4-C3 (check out-check in-standard working time). The result is negative and it will be displayed like -1:15 and you can further process it.
Second way was already described above - to put into the requested cell a negative decimal value as a fraction of "1". "1,000"=24 hours, "0,5"=12 hours, "0,01"= 14 minutes, "0,041667"=1 hour. You have to find the correct decimal numbers first.

Resources