Excel function to calculate pairwise date difference - excel

I have a excel sheet with all employees of my company. This list also includes the sick days of each employee. I want to calculate the sick days of each year.
My excel sheet looks like this (shortened version):
A B C D E F G ...
1 Name From To From To From To ...
2 Max 2015/06/15 2015/06/16 2016/08/17 2016/08/17
3 Sarah 2016/01/20 2016/01/20
4 Phil
...
Explanation: Max was sick for two times. First from 2015/06/15 to 2015/06/16 and second from 2016/08/17 to 2016/08/17. Every time he gets sick the table is expanded to the right. Sarah was sick only on 2016/01/20 and Phil wasn't sick.
Now, I want to calculate the sick days of a year.
I could do something like
A B
10 2015 Sum of sick days in 2015
11 2016 Sum of sick days in 2016
So what needs to be done is:
check if date is in the right year
pairwise calculation of networkdays of all dates in a row, that means
=SUM(NETWORKDAYS(B2;C2)) for 2015 and
=SUM(NETWORKDAYS(D2;E2);NETWORKDAYS(B3;C3)) for 2016.
But it should work more dynamically. I only want to choose the matrix, e.g. A1 to I8 for each year and the rest should be calculated automatically. Does anybody know how I can do that?

You can use the below formula. (paste this in the cell B10
=SUMPRODUCT((YEAR($B$2:$N$8)=$A10)*(MOD(COLUMN($B$2:$N$8),2)=0)*(($C$2:$O$8-$B$2:$N$8)+1))
Copy the above cell into B11. It should work.

The following seems quite unruly:
=IF(YEAR(C2)=A10,NETWORKDAYS(B2,C2))+IF(YEAR(G2)=A10,NETWORKDAYS(F2,G2))+IF(YEAR(K2)=A10,NETWORKDAYS(J2,K2))+IF(YEAR(C3)=A10,NETWORKDAYS(B3,C3))+IF(YEAR(G3)=A10,NETWORKDAYS(F3,G3))+IF(YEAR(K3)=A10,NETWORKDAYS(J3,K3))+IF(YEAR(C4)=A10,NETWORKDAYS(B4,C4))+IF(YEAR(G4)=A10,NETWORKDAYS(F4,G4))+IF(YEAR(K4)=A10,NETWORKDAYS(J4,K4))
Alternatively you could either write a macro to loop through the data or add an additional two columns in between each From and To column. D2 would be
=IF(ISBLANK(C2),"",YEAR(C2))
and copy down. E2 would be
=IF(ISBLANK(C2),"",NETWORKDAYS(B2,C2))
and copy down. E11 would be
=SUMIF(D2:D4,$A11,E2:E4)
and copy across to I11 and M11 etc. You can then just
=SUM(C11:M11)
These new columns can be hidden if you needed.

Related

Only calculate when finished entering week

I needed help generating a formula week wise. My goal is to have the First Contact (FC) and Follow-up (FU) calculated from Wednesday of a week to the Tuesday of next week by looking at the dates. So, in this case on cell M10 the FC should be 5 and on cell N10 the FU should be 3 and then again it would calculate for the next week from Wednesday until Tuesday so on M16 it should calculate FC as 1 and on N16 the FU as 4.
I need to continuously record data for further weeks therefore need a formula which could do so. Would a SUMIF be a good option here?
Currently I have the following formulas:
=COUNTIF(A:A,"First Contact") which calculates 6
=COUNTIF(A:A,"Follow-up") which calculates 7
Formula in cell C2:
=IF(A2<>"";"";COUNTIFS(A:A;"First Contact";B:B;"<="&B1;B:B;">="&B1-6))
Formula in D2:
=IF(A2<>"";"";COUNTIFS(A:A;"Follow_up";B:B;"<="&B1;B:B;">="&B1-6))
Drag to bottom.
Since your data is sorted you could use the following:
In M10 use: =COUNTIF($A$1:$A10,"First Contact")-SUM(M$1:M9)
And in N10 use: =COUNTIF($A$1:$A10,"Follow-up")-SUM(N$1:N9)
And since your results show in an empty row, if you want a draggable solution, wrap it in IF like this: =IF($A10="",COUNTIF($A$1:$A10,"FC")-SUM(M$1:M9),"")
The $ in the range reference for column letter and row number will lock the position if you drag the formula or copy it and paste it elsewhere.

Formula for if a cell is blank, It will consider the previous cell as the value for that cell and sum the cells together

I am trying to figure out how to sum a row of cells where if a cell in the row has a letter, for example a letter "X", to consider the cell to the left of it in the row as a value for that cell when summing up the row. For my table the reason behind this is I have a table that is for a construction project work week where each day I list the amount of employees work that day. On their off day I still need to consider them for when I sum the project up and pay for their hotel on the days they don't work. Also the employee count per day can change throughout the duration of the project. Example of this bellow:
A B C D E F G H
5 5 5 5 4 4 X 4
So for this A through G is considered Monday-Saturday. Monday-Thursday I have 5 employees, while on Friday-Sunday I will have 4 Employees even though the 4 employees didn't work on Sunday for their off day I still need to sum the week up to charge a daily hotel price since the 4 employees will come back the next monday, which is "H" above and continue to work.
So is there a formula where I if I am summing up the row and it finds an X then it will sum the previous cell two times? And I do not want to add employees to the Sunday column because I have another formula that sums the work days to charge their hourly pay rate.
Enter this array formula in next cell ( with ctrl shift enter )
=SUM(IF(COLUMN(A1:H1)<=COLUMNS(B1:H1),NOT(ISNUMBER(B1:H1)))*(IF(ISNUMBER(A1:H1),A1:H1,0)))+SUM(A1:H1)
Obviously it won't consider if first cell contains an X. Rest in any of the cell if X is there, it will add up the previous value twice
Moreover it'll work if cell contains any numeric character
If it's always about the Sunday containing an X or not then I guess this is what you mean:
=IF(G2="X",SUM(A2:F2)+F2,SUM(A2:G2))

excel vba shifting formulas based on sales lead time

I trying to find the correct code to move my formulas over x number of cells. The x would represent sales lead time. Lets say I make a January sale, but that revenue wont actually be received until 7 months later. So for Jan I wont receive until July, for Feb I wont receive until August, for March I wont receive until September and so on.
I'm looking for something to automate or shift my formulas that total my sales over based on a lead time cell. So lets say I change my lead time to 5 months everything shifts over to reflect 5 months instead of 7.
Any help would be greatly appreciated.
Hope thats not too confusing.
Thanks for any help!
You seem to be looking at the problem from the wrong end: Instead of asking when your January sales proceeds will be received, try asking which proceeds you will receive in July. Then you arrive at a structure like this:-
Column A = Date of sale
Column B = Description
Column C = Sales amount
Column D = Expected delay in months
Column E and up = Jan, Feb, etc. one column for each month
Write the starting date in cell E1, say 1/1/2017.
In F1 paste this formula: =DATE(YEAR(E1),MONTH(E1)+1,1)
Format the range E1:F1 as 'Custom' using the string MMM yy
Copy F1 to the right as far into the future as you require
Now you have a column for each month. If you change the value in E1 all captions will change accordingly. That's good when you need a new sheet for next year.
Write this formula in E2: =IF(MONTH($A2) + $D2 = MONTH(E$1), $C2,0)
Copy right and down as far as you require
Now, make an entry. Say on Jan 17 you had a sale of $1200 and you expect to receive the money 6 months later.
Enter the date in column A = 17/1/2017
Enter the amount in column C = 1200
Enter the expected delay in column D = 6
The amount appears in the July column. Change the value in column D to 3 and the amount moves to April.
All the other columns show a zero. If you don't like that, go to File ->Options -> Advanced >>Display options for this worksheet = uncheck the checkbox for "Show a zero in cells that have zero value".

SUM values based on date in date range in array

i'm realy newbie in excel and i need to make advanced functions
I have excel (from Google Calendar) with booking system
A B C
12-01-2012 14-01-2012 8
13-01-2012 17-01-2012 11
15-01-2012 21-01-2012 3
A - start date
B - end date
C - number of guests
Now I need to SUM number of guest for all days in year and find every days where was more than 10 reservations.
For example, I need to return
12-01-2012 - 08 guests
13-01-2012 - 19 guests
14-01-2012 - 19 guests
15-01-2012 - 14 guests
May I do it without creating large excel file with all days combinations? And how?
I'm working on excel 2013.
This can be done quite simply by using the SUMIFS function. First you need to put each date in column D (set the first date you care about manually, then in D2 below, just say =D1+1 and copy down). Then to count the people for each of those days, put this in E1 and drag down:
=SUMIFS(C:C, A:A, ">="&D1, B:B,"<="&D1)
This will Sum the passengers listed in column C (must be formatted as numbers, not text - it looks like you might have some text values in there), if that row in column D (each day in the year) is within the range of Column A & Column B.
To find days larger than 10 people, simply have column F (in F1 and dragged down) say
=E1>=10
This will say TRUE if column E shows more than 10 people for that day.

Sum values in a column based on date

I have written this function that will give me a monthly sum for two columns: one has the date of each order, one has the cost of each order.
=SUMIF($C$1:$C$1000,">="&DATE(2010,6,1),$D$1:$D$1000)-SUMIF($C$1:$C$1000,">="&DATE(2010,7,1),$D$1:$D$1000)
Using data like this:
8/16/10 17:00 7.99
8/16/10 14:25 7.99
8/15/10 22:42 7.99
I end up with a table like this:
May 998
June 968.28
July 1239.76
August 514.96
However, now I would like to do daily sums and using my way I have to hand edit each row.
How can I do this better in Excel?
Use a column to let each date be shown as month number; another column for day number:
A B C D
----- ----- ----------- --------
1 8 6 8/6/2010 12.70
2 8 7 8/7/2010 10.50
3 8 7 8/7/2010 7.10
4 8 9 8/9/2010 10.50
5 8 10 8/10/2010 15.00
The formula for A1 is =Month(C1)
The formula for B1 is =Day(C1)
For Month sums, put the month number next to each month:
E F G
----- ----- -------------
1 7 July $1,000,010
2 8 Aug $1,200,300
The formula for G1 is =SumIf($A$1:$A$100, E1, $D$1:$D$100). This is a portable formula; just copy it down.
Total for the day will be be a bit more complicated, but you can probably see how to do it.
Use pivot tables, it will definitely save you time. If you are using excel 2007+ use tables (structured references) to keep your table dynamic. However if you insist on using functions, go with Smandoli's suggestion. Again, if you are on 2007+ use SUMIFS, it's faster compared to SUMIF.
Following up on Niketya's answer, there's a good explanation of Pivot Tables here:
http://peltiertech.com/WordPress/grouping-by-date-in-a-pivot-table/
For Excel 2007 you'd create the Pivot Table, make your Date column a Row Label, your Amount column a value. You'd then right click on one of the row labels (ie a date), right click and select Group. You'd then get the option to group by day, month, etc.
Personally that's the way I'd go.
If you prefer formulae, Smandoli's answer would get you most of the way there. To be able to use Sumif by day, you'd add a column with a formula like:
=DATE(YEAR(C1), MONTH(C1), DAY(C1))
where column C contains your datetimes.
You can then use this in your sumif.
Add a column to your existing data to get rid of the hour:minute:second time stamp on each row:
=DATE(YEAR(A1), MONTH(A1), DAY(A1))
Extend this down the length of your data. Even easier: quit collecting the hh:mm:ss data if you don't need it. Assuming your date/time was in column A, and your value was in column B, you'd put the above formula in column C, and auto-extend it for all your data.
Now, in another column (let's say E), create a series of dates corresponding to each day of the specific month you're interested in. Just type the first date, (for example, 10/7/2016 in E1), and auto-extend. Then, in the cell next to the first date, F1, enter:
=SUMIF(C:C, E1, B:B )
autoextend the formula to cover every date in the month, and you're done. Begin at 1/1/2016, and auto-extend for the whole year if you like.
If the second row has the same pattern as the first row, you just need edit first row manually, then you position your mouse pointer to the bottom-right corner, in the mean time, press ctrl key to drag the cell down. the pattern should be copied automatically.

Resources