No of days between 2 dates - excel

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);

Related

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

EXCEL: SUMIFS criterion applied to a INDEX MATCH search equals a value

I've spent pretty much all day trying to figure this out. I've read so many threads on here and on various other sites. This is what I'm trying to do:
I've got the total sales output. It's large and the number of items on it varies depending on the time frame it's looked at. There is a major lack in the system where I cannot get the figures by region. That information is not stored in the system. The records only store the customer's name, the product information, number of units, price, and purchase date. I want to get the total number of each item sold by region so that I can compare item popularity across regions.
There are only about 50 customers, so it is feasible for me to create a separate sheet assigning a region to the customers.
So, I have three sheets:
Sheet 1: Sales
+-----------------------------------------------------+
|Customer Name | Product | Amount | Price | Date |
-------------------------------------------------------
| Joe's Fish | RT-01 | 7 | 5.45 | 2020/5/20 |
-------------------------------------------------------
| Joe's Fish | CB-23 | 17 | 0.55 | 2020/5/20 |
-------------------------------------------------------
| Mack's Bugs | RT-01 | 4 | 4.45 | 2020/4/20 |
-------------------------------------------------------
| Joe's Fish | VX-28 | 1 | 1.20 | 2020/5/13 |
-------------------------------------------------------
| Karen's \/ | RT-01 | 9 | 3.45 | 2020/3/20 |
+-----------------------------------------------------+
Sheet 2: Regions
+----------------------+
| Customer | Region |
------------------------
| Joe's Fish | NA |
------------------------
| Mack's Bugs | NA |
------------------------
| Karen's \/ | EU |
+----------------------+
And my results are going in Sheet 3:
+----------------------+
| | NA | EU |
------------------------
| RT-01 | 11 | 9 |
+----------------------+
So looking at the data I made up for this question, I want to compare the number of RW-01's sold in North America to those sold in Europe. I can do it if I add an INDEX MATCH column to the end of the sales sheet, but I would have to do that every time I update the sales information.
Is there some way to do a SUMIFS like:
SUMIFS(Sheet1!$D:$D,Sheet1!$A:$A,INDEX(Sheet2!$B:$B,MATCH(Sheet1!#Current A#,Sheet2!$A:$A))=Sheet3!$B2,Sheet1!$B:$B,Sheet3!$A3)
?
I think it's difficult to do it with a SUMIFS because the columns you're matching have to be ranges, but you can certainly do it with a SUMPRODUCT and COUNTIFS:
=SUMPRODUCT(Sheet1!$C$2:$C$10*(Sheet1!$B$2:$B$10=$A2)*COUNTIFS(Sheet2!$A$2:$A$5,Sheet1!$A$2:$A$10,Sheet2!$B$2:$B$5,B$1))
I don't recommend using full-column references because it could be slow.
BTW I was assuming that there were no duplicates in Sheet2 for a particular combination of customer and region - if there were, you could use
=SUMPRODUCT(Sheet1!$C$2:$C$10*(Sheet1!$B$2:$B$10=$A2)*
(COUNTIFS(Sheet2!$A$2:$A$5,Sheet1!$A$2:$A$10,Sheet2!$B$2:$B$5,B$1)>0))
EDIT
It is worth using a dynamic version of the formula, though it is not elegant:
=SUM(Sheet1!$C2:INDEX(Sheet1!$C:$C,MATCH(2,1/(Sheet1!$C:$C<>"")))*(Sheet1!$B2:INDEX(Sheet1!$B:$B,MATCH(2,1/(Sheet1!$B:$B<>"")))=$A2)*
(COUNTIFS(Sheet2!$A$2:INDEX(Sheet2!$A:$A,MATCH(2,1/(Sheet2!$A:$A<>""))),Sheet1!$A2:INDEX(Sheet1!$A:$A,MATCH(2,1/(Sheet1!$A:$A<>""))),Sheet2!$B$2:INDEX(Sheet2!$B:$B,MATCH(2,1/(Sheet2!$B:$B<>""))),B$1)>0))
As you would need to make the match in memory I don't think it's feasible in Excel, you'll have to use a vba dictionary.
On the other hand, if the number of columns is fixed in your sales sheet, you can just format as table and add your index match in F.
When updating the sales data delete all lines as of line 3 and copy paste the update value. Excel will automatically apply the index match on all rows.

Using IF AND to calculate based on one or more criteria

Within a resource planner, my data has a row for each employee, and columns detailing the team they work for. Another column details the available days they will work in the year. The teams are also displayed along a row at the top, see below :
A | B | C | D | E | F | G |
1 Employee | Team 1 | Team 2 | Days | Finance | Risk | IT |
2 Employee 1 | Finance | | 170 | | | |
3 Employee 2 | Risk | Finance | 170 | | | |
4 Employee 3 | Finance | | 170 | | | |
5 Employee 4 | IT | Risk | 170 | | | |
6 Employee 5 | IT | Finance | 170 | | | |
I want to use columns E:G as a supply calculator per team. Therefore, the formula in cell E2 would be "=IF(B2=E1,D2,0)" and copied along the row, returning the 170 days under Finance and 0 under the rest.
The issue lies where an employee divides his time between two different teams. As you can see, some employees can work for 2 different teams (Employee 2 works for both Finance and Risk, for example). The formula in E3 would therefore need to be some kind of IF AND, where if a value is present in the Team 2 column (C), the value in the Days column (D) would be divided by two and split across the relevent team columns.
I've tried a few options, IF AND, nested IFS etc but cant seem to get the syntax correct. Any help greatly appreciated.
=IF(ISNUMBER(MATCH(E$1,$B2:$C2,0)),$D2/COUNTA($B2:$C2),0)
You actually want OR and COUNTA:
=IF(OR($B2=E$1,$C2=E$1),$D2/COUNTA($B2:$C2),0)

Excel: How to sum a column to date from a specified date where date ranges are involved

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.

Moving range/array reference in Excel table

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.

Resources