If function with formula to calculate difference between dates - excel

I am trying to find a difference between 2 dates using networkdays and additionally if both dates are equal, have to make the value as 0. But unable to get the result.

You can try
if(DATEVALUE(A1)=DATEVALUE(A2),0,NETWORKDAYS(A1+1,B1)

#Ron Rosenfeld has pointed out that my testing led to wrong conclusions. Therefore I increased the volume of my tests. They continue to show that ...
Comparing the two dates and adding a day gives erratic results, and that
The naked NETWORKDAYS function as designed by Microsoft gives the most useful result.
In the table below the original function is in column C, your function (modified to work by #Ron Rosenfeld) in column D and two variations thereof in columns E and F. The lower part of the table shows results where both days are the same while the dates are 2 days apart in the upper part.
The tables show that the original formula counts both the start and the end days as well as all days between them, subtracting weekend days, except when start and end days are the same. In other words, the original function already treats the case of A1=B1 differently.
However, if an adjustment is to be made it should preferably be made to the result of the formula, not be modifying the date before they are processed. This is demonstrated in column F.
The formula in column E takes up your attempt to modify one of the dates (the end date could be modified too) and makes this conditional. The difference as compared to your formula is that you apply in fact two separate calculations, one where the result is always 0 (when dates are equal) and the other where the start date is advanced by one day unconditionally. If it suits your purposes that method can still be applied but the formula in column E shows how the date modification can be made conditional.
The need to apply conditions arises from the essential shortcoming of your formula in that it applies the wrong condition. Different treatment for when dates are equal is already incorporated in the basic function. Any further modification must take the involved weekdays into account, not (necessarily) only the interval between the dates.

Why do you put your formula arguments in quotes? That just changes them into strings, which is why your formula doesn't work.
I assume from your formula that you just don't want to count the first day. In which case it will work as you have it, if you merely remove the double quote marks.

Related

Dates are not recognized

I have written a conditional formula to recognize two dates (greater than and last than). However, there are a few dates that are returning 'false'. All of the columns are formatted as a date. Can someone please help?
This is not a direct answer to the question asked, but as Scott said, the formula you use is much more complex than necessary. If you need to return a number for a date that falls within a period and the periods do not overlap, the following formula can be used:
=SUMPRODUCT((AQ3>=$DF$3:$DF$15)*(AQ3<=$DF$3:$DF$15),$DH$3:$DH$15)
If you need to process text values, it is more complicated. For example, if 'expired' is the only value (also for dates that do not fit into any period), then we can add the IF function to the previous formula:
=IF(SUMPRODUCT((AQ3>=$DF$3:$DF$15)*(AQ3<=$DF$3:$DF$15),$DH$3:$DH$15)>0,SUMPRODUCT((AQ3>=$DF$3:$DF$15)*(AQ3<=$DF$3:$DF$15),$DH$3:$DH$15),"expired")

excel count/sum stop count/sum match?

I have tried to see if this question has been asked before, but I can't seem to find an answer.
I have a column of cells (>3000 rows), with either a value of 1 or 0 (call this column A). The value will depend on the value in column B (which will contain either a value or nothing). The values in column B are a SUMIFS function based, summing from column C, and based on months in column D.
The values in B are paid out on the first business day of the next month. So, the SUMIFS function will calculate the dates that match the last month. This works well in theory, however, not every first business day is the first day of the month. This leads the SUMIFS function to not include everything in the correct month, and allows for some discrepancy, which, when you are dealing with people's money is not great. Further, this discrepancy is compounded across multiple periods (in some cases, there are over 100 periods, and a discrepancy of $1 in period 1 amounts to nearly $1000 in period 100)
What I am wondering is:
Is there any way that I can tell the SUMIFS function (column B) to stop when the value in column A is 0? This would tell the SUM function start the summing from the current value in column B and continue the function to the cell below the preceding value in column B.
I've seen suggestions that the MATCH function may work, but I can't see how to do this with either COUNT or SUM.
For security reasons, this solution needs to be entered into the cell, and can't be VBA. Also, it can't be too large, as it will need to be replicated across 200 worksheets in the workbook (not my file originally, and I would have done it differently, but that is another story). There is no problem entering another column or two if that is required.
Any help is gratefully appreciated.
EDIT:
Unfortunately, I can't post an image of the screenshot. I've included a similar screenshot (columns are not the same layout, but hopefully it gives the idea) here:
Rates calculations
The SUMIF formula is (for B2)
=SUMIFS(C2:C35,D2:D35,D2-1,A2:A35,1)
This works fine if I want all the values in the month, irrelevant of when the payment was made.
However, what I need the formula to do is:
SUM (C2:C35,D2:D35,D2-1, but stop when the first 0 is encountered in A2:A35)
Thanks
The INDEX function can provide a valid cell reference to stop using a MATCH function to find an exact match on 0.
This formula is a bit of a guess as there was no sample data to reference but I believe I have understood your parameters.
=SUMIFS(C2:index(C2:C35, match(0, A2:A35, 0)), D2:index(D2:D35, match(0, A2:A35, 0)), D2-1)
This seems to be something that will stand-alone and not be filled down so I have left the cell addresses relative as per your sample(s).

Excel: Subtracting Dates Without Reference Cells

I want to program a cell to calculate the number of days I have left before I meet a deadline.
I would like to do this without reference cells, unlike this tutorial.
An (incorrectly formatted) example of the kind of formula I want would be:=(3/2/2015-TODAY()), where 3/2/2015 is my deadline. This yields some negative serial number,-42051.00, which yields a #NUM! error when put into the DAY formula.
Any idea how to do this without putting TODAY() and 3/2/2015 into their own reference cells? I would like to use functions to keep these paraments completely embedded in the formula.
Right clock the cell with the answer and reformat it as NUMBER. You want to use the Days function not the date function.
=DATE(2015,3,2)-TODAY() is what you want, but I would recommend doing the date in a separate cell for a number of reasons and using "today()" in the formula.
EDIT: Anyone trying to find midpoints would use the date function in this case.
Also, as a general rule for people trying to subtract dates the two trouble shooting methods you want are
A) Check your format-If you want number of dates, it needs to be set as number, if you want a date, you need it to set as date. If you get a long decimal it means you have it formatted as general OR your expression returns a date value rather than a number value. Refer to my original answer.
B) Reverse your dates. Depending on the function and what you want, you may need to move the dates around.
=DATEDIF(DATEVALUE("03/02/2015"), TODAY(), "d")

Excel: Counting how many rows fall within a time period

I have an excel sheet with 2 columns
Column A = "CA", "CR" or "IN"
Column B = Date & time format DD/MM/YYYY HH:MM
I want to make a count at the bottom of a column for each row that has this criteria:
i) Row 1-8 = "CA"
ii) Row 1-8 needs to check for a time range, namely > "17:00" and < "04:59"
This is what I've come up with so far:
=COUNTIFS(A2:A8,"CA",B2:B8,RIGHT(TEXT(B2:B8,"hh:mm"),5)>"04:59"), B2:B8,RIGHT(TEXT(B2:B8,"hh:mm"),5)<"17:00")
I presume using a range within a the text function is wrong, but don't know how to resolve this.
Because Column B is in a date and time format, I'm having to change it to a string within the function so I can make a test on just the time - Maybe there's a better way?)
Thanks
The problem with COUNTIFS is that you can't manipulate the conditions a lot. You could insert a column containing the time only, in which case you could use COUNTIFS but if you cannot, you can use SUMPRODUCT for substitute:
=SUMPRODUCT((A1:A6="CA")*(((TEXT(B1:B6,"hh:mm")*1>TIME(17,0,0))+(TEXT(B1:B6,"hh:mm")*1<TIME(5,0,0)))>0))
This applies a few conditions on an example range A1:B6:
(A1:A6="CA") that ensures that the row has CA,
(TEXT(B1:B6,"hh:mm")*1>TIME(17,0,0)) that ensures that the time is above 17:00
(TEXT(B1:B6,"hh:mm")*1<TIME(5,0,0)) that ensures that the time is before 05:00 (You have 4:59 in your question, if you really meant less than 4:59 then change this part).
The + for the last two conditions 'OR's the two conditions, then the whole thing is 'AND'ed with the first condition.
This is difficult to do with COUNTIFS because you can't modify ranges with functions.....but you can do that with SUMPRODUCT - try this
=SUMPRODUCT((A2:A8="CA")*(MOD(B2:B8,1)<"17:00"+0)*(MOD(B2:B8,1)>"04:59"+0))
I'm assuming that you want between 04:59 and 17:00 - in your point ii) you show it the opposite way to what you have in your formula
MOD extracts the time from the date/time so it can be compared against a time range. If you are counting within a range including whole hours, e.g. 5 to 16 inclusive you can use HOUR function without MOD, i.e.
=SUMPRODUCT((A2:A8="CA")*(HOUR(B2:B8)>=5)*(HOUR(B2:B8)<=16))
You do not need to deal with strings at all for the time. Excel provides with quite a few date&time functions, enough in your case.
A concise formula is
=SUMPRODUCT((A1:A8="CA")*(MOD(B1:B8,1)<=TIME(17,0,0))*(MOD(B1:B8,1)>TIME(4,59,0)))
Notes:
I assume you meant <=17:00, instead of <17:00. It is easy to modify the formula if I was wrong.
You used A2:A8, but you probably wanted A1:A8, as per the text. It is easy to modify the formula if I was wrong.
COUNTIFS is usually much less flexible than SUMPRODUCT (combined with other functions as MATCH, INDEX, SUM, etc.)
PS: as barry houdini points out, the OP asks for two opposite time ranges. I have chosen here one of them.

Date comparison not working in Excel formula

If I put the date 31/12/2013 in A1 and another date 1/1/2014 in A2 then a formula like
=A1<A2
gives the expected result, TRUE.
If I put the formula
=A1<1/1/2014
in another cell, it gives the result FALSE.
The question is how to adjust the second formula to make it give the correct result, and why it doesn't work as it stands.
I've been looking at this for a while and have found some related posts like
Comparing computed dates with entered dates
but not one which directly answers the question.
1/1/2014 is 1 divided by 1 divided by 2014.
Instead; =A1<DATEVALUE("1/1/2014")
I can answer my own question now.
The answer as mentioned in related posts is to use the DATE or DATEVALUE functions i.e.
=A1<date(2014,1,1)
or
=A1<datevalue("1/1/2014")
The reason it doesn't work is that in this context Excel just sees 1/1/2014 as an arithmetic expression, one divided by one divided by 2014 which is a small number. Dates (number of days since 1/1/1900) generally evaluate to large numbers so the comparison fails.
If you just type 1/1/2014 into a cell you get a date, but if you type =1/1/2014 you get a small number.
I just thought it was interesting to share because to a human =A1<1/1/2014 looks as if it's comparing a cell with a date, but it isn't.

Resources