I am having some difficulty finding the optimal solution to a problem I am having with Excel. Right now, I have a large sheet which has time on one axis (column A is every date between 1/1/2018 to 1/1/2019), and in the sheet I have time using the h:mm:ss function in different cells on different intervals.
For example, for rows 1/1/2018 - 1/3/18, there is a starting time on 1/1/2018 (example, 8:00:00), and then an ending time on 1/3/18 (example, 16:00:00).
The time between those two dates and two times on those dates is what I am looking to calculate.
This is on a sheet with 2000+ rows, and each interval is different (some may be three days, some may be the same day). The difference between all of these is another column with location. This looks like:
Row 2) Date, Location [A], Arrival Time (8:00:00), Departure Time (blank)
Row 3) Date (the next day), Location [A] (the same as above), Arrival time (blank), Departure Time (16:00:00)
+---+----------+----------+---------+-----------+
| | A | B | C | D |
+---+----------+----------+---------+-----------+
| 1 | Date | Location | Arrival | Departure |
| 2 | 1/1/2018 | A | 8:00 | |
| 3 | 1/2/2018 | A | | 16:00 |
| 4 | 1/3/2018 | B | 8:00 | 16:00 |
| 5 | 1/4/2018 | C | 5:00 | 13:00 |
| 6 | 1/5/2018 | C | 5:00 | 10:00 |
+---+----------+----------+---------+-----------+
I need to calculate the time spend in Location [A] between Arrival time on one date to the Departure time on the next date.
Please let me know what you think the optimal solution is to this problem, I am open to anything!
+--------+----------+---------+-----------+------------------------+
| Date | Location | Arrival | Departure | Time Spent in Location |
| 1/1/18 | A | 8:00:00 | 16:00:00 | 8:00:00 |
| 1/2/18 | B | 8:00:00 | | |
| 1/3/18 | B | | 18:00:00 | 34:00:00 |
| 1/4/18 | C | 8:00:00 | | |
| 1/5/18 | C | | | |
| 1/6/18 | C | | 16:00:00 | 56:00:00 |
+--------+----------+---------+-----------+------------------------+
The post from above is dead accurate - The trouble I am having is from the time intervals for each location being different (and random). I am trying to simply have an additional column which calculates the time spent in each location - from there I will be collecting data on each location and how many hours will be spent there.
Also, a big THANK YOU for the help so far! I am shocked by how quickly comments were made - sorry for the time it took me to make the mock-up, hope it helps make my goal in this more clear.
Actual Snip from sheet
One possible solution is to use the following formula in E2 and copy down.
=IF(C3="",((A3+D3)-(A2+C2)),IF(D3<>"",D3-C3,""))
EDIT: D4-C4 changed to D3-C3 in formula above)
It works by combining the date to the time, doing a math operation as needed and then stripping it out. Tap in a couple of IF statements to deal with rows that are single line locations, or the arrival only. It also assumes the first entry cannot be a departure.
Credit to urdearboy for the correct custom formatting. You will need to format the cells of the column to be [h]:mm
OPTION 2
This assumes that locations can be repeated. The use of a helper column is used to create a unique combination.
In F2, use the following formula and copy down as required:
=COUNTIFS($B$2:B2,B2,$C$2:C2,"<>"&"")
The $ locking the cells is very important. Note how it creates an expanding range as you copy down.
now technically speaking you can combine the following into one monster formula, but it will be difficult to follow and therefore maintain if someone else has to deal with it or you come back to it at a much later date. So on that note I will break it into 2 more helper columns and formulas
In column G we will identify the first row where a unique location occurs. A unique location is created by compounding the location name and the unique ID number which is a count of how often it has occurred in the list so far. Ie. A1 is the first time there was an arrival at location A, E2 is the second arrival at location 2. In G2 place the following formula and copy down.
=AGGREGATE(15,6,ROW($A$2:$A$16)/(B2&F2=$B$2:$B$16&$F$2:$F$16),1)
Now that the arrival row is identified the same needs to be done for the departure row. The same formula can be used but change the 15 to a 14. In H2 place the following formula:
=AGGREGATE(14,6,ROW($A$2:$A$16)/(B2&F2=$B$2:$B$16&$F$2:$F$16),1)
Now that you have those locations identified, you can use the following formula to calculate time:
=IF(D2="","",(INDEX(A:A,H2)+INDEX(D:D,H2))-(INDEX(A:A,G2)+INDEX(C:C,G2)))
In the example, I placed it in I2 and then I copied F2:I2 down as required. but it could be placed in E2 and copied down just as easily.
If you do not want those additional helper columns in G and H, then you can substitute them into your formula for time and it will look like this:
=IF(D2="","",(INDEX(A:A,AGGREGATE(14,6,ROW($A$2:$A$16)/(B2&F2=$B$2:$B$16&$F$2:$F$16),1))+INDEX(D:D,AGGREGATE(14,6,ROW($A$2:$A$16)/(B2&F2=$B$2:$B$16&$F$2:$F$16),1)))-(INDEX(A:A,AGGREGATE(15,6,ROW($A$2:$A$16)/(B2&F2=$B$2:$B$16&$F$2:$F$16),1))+INDEX(C:C,AGGREGATE(15,6,ROW($A$2:$A$16)/(B2&F2=$B$2:$B$16&$F$2:$F$16),1))))
A tidied up version using the monster formula and therefore not needing column G and H formulas:
Remember to apply custom formatting to your time calculation column of [h]:mm
Related
In the sample data I have employee wise tour dates with month break up sorted by dates in ascending order. I would like to calculate days. Any formula suggestion please?
Conditions:
1. 1 date for 1 month and 1 employee
2. Should include start date also.
From the below example I may explain my requirement for one employee no.1531. All my dates are in DD/MM/YYYY format.
| 1531 | 3-4-2018 | 3-4-2018 | 4 |
| 1531 | 3-4-2018 | 4-4-2018 | 4 |
| 1531 | 3-4-2018 | 7-4-2018 | 4 |
| 1531 | 22-4-2018 | 2-5-2018 | 4 |
| 1531 | 3-5-2018 | 4-5-2018 | 5 |
| 1532 | 1-4-2018 | 2-4-2018 | 4 |
| 1532 | 22-4-2018 | 2-5-2018 | 4 |
| 1532 | 3-5-2018 | 4-5-2018 | 5 |
1531 employee were started his tour on 03/04/2018 and concluded on the same day for one location. Again he started on the same day 03/04/2018 and concluded on 04/04/2018 for 2nd location. Again the person mentioned he started on 03/04/2018 and concluded on 07/04/2018 for another location. In the above said the date 03/04/2018 were entered trice which was not right, but because I don't have control to stop this in application the entries were made by employee wrongly. So I want excel to consider the date 03/04/2018 as one time only while calculating days. I was tried my best to arrive the days but could not. I have attached my formula as separate file for your kind reference. Kindly look into this and give solution. Even this formula i got through from one among the stack overflow member only. In this i was not comfortable to get my dynamic starting date.
I have asked the similar question earlier also but i have not received any answer against my expectations
Try to do Date 1 - Date 2. Most libraries in most languages should have the - operator overridden.
If not, convert both dates to epoch timestamps(seconds) and then divide it by (24*60):
Date Date1 = new Date(03/04/2018);
Date Date2 = new Date(04/04/2018);
int days = (Date2.toTimeStamp() - Date1.toTimeStamp()) / (24*60);
I'm sure i have seen this done with a short formula but i am struggling to remember how to do it.
I am trying to find where a date falls in an interval and sum another column, from that point in the interval, to the end of all specified date intervals.
So, in the image below the intervals are in columns D and E and the date to find is in cell F1 i.e. 12/12/2016.
I want to find where 12/12/2016 falls within the ranges and sum column F accordingly i.e. 12/12/2016 - 13/12/2016 (2 days) and then all intervals after will be 2 + 6+2+1+3 = 14. I am returning this result in cell J14.
I have used the idea of histograms to calculate this currently, but the formula is large and unwieldy, and i just know i have seen a similar question, somewhere on SO but can't find it, that does this with SUMPRODUCT and OFFSET only. I guess FREQUENCY could also be used.
So what i have currently is:
=SUMPRODUCT(OFFSET(F6,MATCH(TRUE,OFFSET(E6,0,0,COUNT(E6:E1048576),)>F1,0)-1,0,COUNTA(F6:F1048576),))-(F1-OFFSET(D6,MATCH(TRUE,OFFSET(E6,0,0,COUNT(E6:E1048576),)>F1,0)-1,,,))
Where, if i broke it down into stages, i find which bucket (range) has the target value:
={MATCH(TRUE,OFFSET(E6,0,0,COUNT(E6:E1048576),)>F1,0)}
I calculate the distance into the range with:
=F1-OFFSET(D6,H13-1,,,)
And i sum from this point until the end of the range with:
=SUMPRODUCT(OFFSET(F6,H13-1,0,COUNTA(F6:F1048576),))-I13
So, can anyone help me with a shorter more efficient formula please?
Data:
| Start of measurement | 12/12/2016 | |
|----------------------|------------|----------------|
| | | |
| | | |
| | | |
| From | To | Number of days |
| 13/11/2016 | 17/11/2016 | 5 |
| 10/12/2016 | 13/12/2016 | 4 |
| 03/02/2017 | 08/02/2017 | 6 |
| 06/12/2017 | 07/12/2017 | 2 |
| 09/12/2017 | 09/12/2017 | 1 |
| 12/12/2017 | 14/12/2017 | 3 |
This should work for you:
=SUMIF(E6:E11,">="&F1,F6:F11)-F1+INDEX(D6:D11,MATCH(F1,D6:D11))
How about this?
=SUMIFS(F6:F11,D6:D11,">="&F1)+MINIFS(E6:E11,E6:E11,">="&F1)-F1+1
The SUMIFS() gives the 6+2+1+3 part and the MINIFS() - F1 + 1 gives the 2 part.
Note that the ___IFS() functions are more recent and not available in older versions of Excel.
Hope you can help me
My expected result is in column I,
where if the activity of this specific ID falls into the date range from columns C-D, i will get the minimum date and get the name column
however, in the formula i created, it always get the minimum date for that specific ID and its corresponding name
Not sure how to update the formula, to get the outcome that i need
Appreciate any help!
Thanks
excel sample
Put the dates in a single ordered column if you can:
| A | B
1 | 2016-01-01 | able
2 | 2016-04-01 | baker
3 | 2016-07-01 | charlie
4 | 2016-10-01 | dog
5 | 2017-01-01 | easy
Then if you can use Index-Match like this:
| IN | =Match(IN1, A:A) | =Index(B:B, Match(IN1, A:A))
1 | 2016-01-01 | 1 | able
2 | 2016-02-01 | 1 | able
3 | 2016-04-01 | 2 | baker
If your date ranges have to be in two columns - there's probably a much more complicated way to do this using array formulas.
Use this Array formula:
=INDEX($E$2:$E$7,MATCH(MIN(IF(($C$2:$C$7=G2)*($D$2:$D$7<=H2),H2-$D$2:$D$7)),IF(($C$2:$C$7=G2)*($D$2:$D$7<=H2),H2-$D$2:$D$7),0))
We need to find where the date is closest but less than the criteria.
Being an Array formula it must be confirmed with Ctrl-Shift-Enter when exiting Edit mode. If done correctly Excel will put {} around the formula.
I have 2 columns in Excel (like below) and I would like to identify (conditionally format) any rows that are exactly the same.
As you can see 326.001 1,000 HOUR are identical for the first 3 rows, I would like to highlight or mark these rows so I can see that they are not unique.
+---------+------------+
| ID | INTERVAL |
+---------+------------+
| 326.001 | 1,000 HOUR |
| 326.001 | 1,000 HOUR |
| 326.001 | 1,000 HOUR |
| 326.001 | 3,000 HOUR |
| 326.002 | 1 MONTH |
| 326.002 | 1 YEAR |
| 326.002 | 5 YEAR |
| 326.002 | 500 HOUR |
| 326.002 | 500 HOUR |
| 326.002 | 500 HOUR |
| 326.002 | 1,000 HOUR |
| 326.002 | 1,000 HOUR |
| 326.002 | 1,000 HOUR |
| 326.002 | 3,000 HOUR |
| 326.009 | 3 MONTH |
| 326.009 | 1 YEAR |
| 326.01 | 3 MONTH |
+---------+------------+
I would add a third column: EqualityTest, with a formula such as:
=AND([#ID]=A5,[#INTERVAL]=B5)
This assumes the data is sorted.
The above formula is for row 5, with ID and Interval in columns A and B. Copy-Paste the formula down, and apply conditional formatting to highlight False for unique values.
qroberts,
I'm not clear on your question.
If you're actually attempting to remove duplicate rows, Excel offers that as a function on the basic ribbon bar for the "Data" ribbon: the button (!) is "Remove Duplicates" in the "Data Tools" section on the "Data" ribbon.
If, instead, you are looking for a highlighter which will identify duplicates, things are a bit more complicated, as you need a macro which will find duplicates and then turn on some form of formatting/highlighting (I suggest formatting a background color).
For a macro which will highlight duplicates, we need to hear a bit more to understand your needs. If you have two different sets of duplicates, do you want them highlighted to different colors? If the number of duplicate sets
gets large, this could be a problem.
As another poster has noted, it also matters whether your candidate set is sorted. A full range search for duplicates would be an interesting bit of coding.
So use COUNTIFS():
=COUNTIFS($A$2:$A$10000,$A2,$B$2:$B$10000,$B2)>0
It will return true for any that has duplicates in both columns. This formula does not care if the data is sorted or not.
Friends, I'm hoping you can help. I'm fairly certain I found a solution to this problem below a while ago, but silly me didn't write it down and now I can't remember how I did it. I'm drawing a blank on what to search for (Google is flooded with answers on "dynamic named ranges", which is not what I'm aiming for here). So, the question:
How can I define a range within a formula (say, RANK for example) that moves as I progress down through a table? I'm trying to avoid using INDIRECT, because it becomes a bit of a memory/processor hog when repeated throughout a large table. Pretty sure there is another way, maybe with INDEX or MATCH?
A simplified version of the data would appear as follows:
Column A has a bunch of reference numbers, each one repeats a few times.
Column B has a bunch of timestamps
Column C is where I would like to rank the timestamp in column B, as compared to all other timestamps that share the same reference number in column A.
The result set should look like this:
| A | B | C |
| abc123 | 01/01/2014 12:30 | 1 |
| abc123 | 01/02/2014 12:30 | 2 |
| abc123 | 01/02/2014 13:30 | 3 |
| abc123 | 01/03/2014 09:30 | 4 |
| def456 | 01/01/2014 12:30 | 1 |
| def456 | 01/01/2014 12:45 | 2 |
| xyz987 | 01/02/2014 12:30 | 1 |
| xyz987 | 01/02/2014 16:30 | 2 |
| xyz987 | 01/03/2014 11:30 | 3 |
Any ideas on what would be the least taxing solution for the processor in this case?
So here is the trick:
copy and paste in C1 and drag and fill down till end.
=IF(A2=A1;SUMPRODUCT(--(A$1:A$9=A1);--(B1>B$1:B$9))+1;SUMPRODUCT(--(A$1:A$9=A1);--(B1>B$1:B$9))+1)
this is an array formula, so press ctrl+shift+enter to calculate the formula
Here is the example sheet in this file downloadable from this link
P.S. remember to adjust the formula to your regional settings by replacing the ";" with "," . Have fun.