I need to have a count of business days grouped for each month. I have been able to figure out the total count but cannot get it to count only business days for each month...any suggestions???
If you only need to count weekdays then this formula will work. It subtracts all saturdays and sundays. If you need to exclude holidays, see Ken Hamady's FAQ.
Rosemary,
I compared the two formulas, using my method of finding first and last dates for the month, and am getting conflicting results. July 2002 is showing me 22 weekdays using your formula, while mine results in 23. It should be 23
I double checked using specific dates:
datevar fd:=date(2002,1,1);datevar ld:=date(2002,1,31);
numbervar week ;
week:=DateDiff ("d", fd, ld) -
DateDiff ("ww", fd, ld, crSaturday) -
DateDiff ("ww", fd, ld, crSunday) Mike
That works great, but I have specified where a date range needs to be entered (ex. 1-1-2002 to 6-30-2002). How would I get it to calculate the count for each month?
If the formula is in the details band and there is a date for the record, it will calculate for the days for that month only. Or if you have the formula in a group header, it will use the formula results for that group. Mike
You are right! I checked it out, and thought it was weird.
This is a standard Crystal Reports example formula!
I thought, hmm simply adding 1 should fix, since it is a datediff it must be excluding the first day. Wrong!
It fixes it for all months except for those that start or end on a Sunday, because those were right to begin with!
Also, it is right on some months. For example, your formula and my formula agree for June - 20 days for both!
In investigating this I found when you look at the help for the datediff("ww", ... it states:
//begin quote:
Use DateDiff with the "ww" parameter to calculate the number of firstDayOfWeek's occurring between two dates. For the DateDiff function, the "ww" parameter is the only one that makes use of the firstDayOfWeek argument. It is ignored for all the other interval type parameters. For example, if firstDayOfWeek is crWednesday, it counts the number of Wednesday's between startDateTime and endDateTime. It does not count startDateTime even if startDateTime falls on a Wednesday, but it does count endDateTime if endDateTime falls on a Wednesday..
//end quote
So the issue seems that for my formula to be correct I need to test to see if the First Date of the Month is a Sunday or Saturday.
Here is a corrected formula, using your bits for firstday and lastday:
//begin formula
datevar fd;
datevar ld;
fd:=date(year({currentdate}),month(currentdate),1);
if month(fd)=12 then ld:=date(year(fd)+1,1,1)-1
else ld:=date(year(fd),month(fd)+1,1)-1;
Mike, Ro -- Thanks for finding and fixing the error in the CR example business days formula. I have been using that formula for some time and had not noticed the error before.
First off, thanks for the Star and the compliment. It feels good getting them from the people I consider gurus.
I like the formula supplied. I'm stealing it for my reports.
I've made one small change to the fomula you supplied:
numbervar week ;
week:=DateDiff ("d", fd, ld) -
DateDiff ("ww", fd, ld, crSaturday) -
DateDiff ("ww", fd, ld, crSunday);
if dayofweek(fd) in [2,3,4,5,6] then week:=week+1;
week
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.