Advertisement

01.29.2008 at 12:14AM PST, ID: 23118616
[x]
Attachment Details

Business Hours calculation Excluding Weekends in Crystal XI

Asked by md5auth in Crystal Reports Software

Tags: Business Objects, Crystal Reports XI, XI, Oracle Database

Greetings Everyone,

I have some problem with calculating Business hours in crystal XI. Following is a sample out of data. our business time is from 07:00 hrs to 16:00 hrs and business days are from Sat-Wed, weekends are Thursday and Friday.

so the calculation of hours should exclude Thursday and Friday and any hours before 7:00 AM and after 04:00 PM.

following is the sample output of calculation @Business.Hours.

Date1: Start Date
Date2: End Date

also note that the entries in the Date2 field were intentionally added that way to explain the problem. this problem seems to be when the Date1t and Date2 are on Thursdays and is during non business hours. this gives the wrong result. instead of giving 0 hours it is showing me 9 hours.

ID              Date1                             Date2               @Business.Hours
1        1/3/08  3:00 AM         1/3/08  3:00 AM        9.00
2        1/3/08  3:00 AM         1/3/08  2:59 AM        9.00
3        1/3/08  3:00 PM          1/3/08  3:00 PM         0.00
4        1/3/08  3:00 PM          1/3/08  2:59 PM         0.00
5        1/4/08  3:00 AM         1/4/08  3:00 AM        0.00
6        1/4/08  3:00 AM         1/4/08  2:59 AM        0.00
7        1/4/08  3:00 PM          1/4/08  3:00 PM         0.00
8        1/4/08  3:00 PM          1/4/08  2:59 PM         0.00
9        1/5/08  7:00 AM         1/5/08  2:00 PM         7.00
10       1/5/08  7:00 AM        1/5/08  6:59 AM        0.00
11       1/5/08  7:00 AM        1/10/08  7:00 AM      45.00
12       1/5/08  7:00 AM        1/11/08  11:00 PM      45.00
13       1/10/08  2:00 AM       1/12/08  2:00 AM      0.00
14       1/10/08  2:00 AM       1/12/08  8:00 AM      1.00

Original formula is downloaded from http://support.businessobjects.com/ and modified to suit weekend and business hours.

the modified formula looks like below.

//@Business.Hours.
//CALCULATE THE NUMBER OF BUSINESS HOURS
//BETWEEN FirstDateTime AND LastDateTime
//INCLUDING HOLIDAYS IN THE TOTAL

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//INPUT REQUIRED _ INPUT REQUIRED _ INPUT REQUIRED

//Replace datetime(2001,12,23,11,50,0) with your starting //date
DatetimeVar FirstDateTime:= {Orders.Date1};
//Replace datetime(2001,12,26,10,0,0) with your ending //date
DatetimeVar LastDateTime:= {Orders.Date2};
//Replace Time(09,00,00) with your business starting time
TimeVar BusinessStartTime:= Time(07,00,00);
//Replace Time(17,00,00) with your business ending time
TimeVar BusinessEndTime:= Time(16,00,00);

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//END INPUT REQUIRED _ END INPUT REQUIRED _ END INPUT

//Other variables used in the formula
Numbervar HoursInADay:= (BusinessEndTime - BusinessStartTime)/3600;
Numbervar Days;
Numbervar Weekends;
Numbervar Finaldays;
DateVar StartDate;
DateVar EndDate;
NumberVar halfdays;
NumberVar fulldays;
NumberVar hours;
timevar TrueStartTime;
timevar TrueEndTime;



//BEGIN FORMULA:
//*********************************************************

//FINISH FORMULA IF FirstDateTime OR LastDateTime IS NULL
//*********************************************************
IF FirstDateTime <=Date(0,0,0) or LastDateTime <=Date(0,0,0) then hours:= 0

//ELSE ASSIGN HOURS
//*********************************************************
ELSE
(
//ASSIGN FirstDateTime and LastDateTime
//*********************************************************
//Determine whether FirstDateTime falls within
//Start Time to End Time
if time(FirstDateTime) in BusinessStartTime to BusinessEndTime then
    FirstDateTime:= FirstDateTime
else if time(FirstDateTime) > BusinessEndTime then
    FirstDateTime:= datetime(date(FirstDateTime)+1, BusinessStartTime)
else if time(FirstDateTime) < BusinessStartTime then
    FirstDateTime:= datetime(date(FirstDateTime), BusinessStartTime);

//Determine whether LastDateTime falls within Start Time to End Time
if time(LastDateTime) in BusinessStartTime to BusinessEndTime then
    LastDateTime:= LastDateTime
else if time(LastDateTime) > BusinessEndTime then
    LastDateTime:= datetime(date(LastDateTime), BusinessEndTime)
else if time(LastDateTime) < BusinessStartTime then
    LastDateTime:= datetime(date(LastDateTime)-1, BusinessEndTime);

//ASSIGN STARTDATE and ENDDATE
//*********************************************************
//if the first day falls on a weekend,

If DayOfWeek(FirstDateTime) = 5 Then
    StartDate := date(FirstDateTime) + 2
Else If DayOfWeek(FirstDateTime) = 6 Then
    StartDate := date(FirstDateTime) + 1
Else StartDate:=date(FirstDateTime);

//if the last day falls on a weekend,

If DayOfWeek(LastDateTime) = 5 Then
    EndDate := date(LastDateTime) + 2
Else If DayOfWeek(LastDateTime) = 6 Then
    EndDate := date(LastDateTime) + 1
Else EndDate := date(LastDateTime);

//CALCULATE DAYS AND WEEKENDS
//*********************************************************
//Calculate Days (including First day and Last day)
Days:= (EndDate - StartDate)+1;

//Calculate weekends
if Days >= 7 then
    WeekEnds := (Round((Days/7),0))*2
else if DayOfWeek(StartDate) > DayOfWeek(EndDate) then
    WeekEnds := 2
else WeekEnds := 0;

//CALCULATE FINALDAYS
//*********************************************************
//If the Last Day is on a weekend then FinalDays subtract the weekend days
If DayOfWeek(LastDateTime) = 5 then FinalDays:= FinalDays - 1;
If DayOfWeek(LastDateTime) = 6 then FinalDays:= FinalDays - 2;

//Assign FinalDays to Days minus Weekends
FinalDays:= Days - WeekEnds;

//CALCULATE HOURS
//*********************************************************
//Calculate FirstDateTime and LastDateTime if falling on a weekend
//if the first day falls on a weekend, StartDate is equal //to the following Monday for calculation reasons
If DayOfWeek(FirstDateTime) = 5 Then
    FirstDateTime := datetime(date(FirstDateTime) + 2, BusinessStartTime)
Else If DayOfWeek(FirstDateTime) = 6 Then
    FirstDateTime := datetime(date(FirstDateTime) + 1, BusinessStartTime);

//if the last day falls on a weekend, EndDate is equal to //the following Monday for calculation reasons  
If DayOfWeek(LastDateTime) = 5 Then
    LastDateTime := datetime(date(LastDateTime) + 2,BusinessStartTime)
Else If DayOfWeek(LastDateTime) = 6 Then
    LastDateTime := datetime(date(LastDateTime) + 1, BusinessStartTime);

//If less than 24 hours involved
If FinalDays <= 1 then
(
    //If first day is the same day as last day
    if date(FirstDateTime) = date(LastDateTime) then
    (
        //If First Day starts before business start time, //assign TrueStartTime to business starttime
        if time(FirstDateTime) >= BusinessStartTime then
            TrueStartTime:= time(FirstDateTime)
        else TrueStartTime:= BusinessStartTime;

        //If Last Day ends after business end time, assign //TrueEndTime to business endtime
        if time(LastDateTime) <= BusinessEndTime then
            TrueEndTime:= time(LastDateTime)
        else TrueEndTime:= BusinessEndTime
    )
    //If first day is not the same day as last day
    else
TrueStarttime:= BusinessStartTime;

    if time(LastDateTime) <= BusinessEndTime then
TrueEndTime:= time(LastDateTime)
    else TrueEndTime:= BusinessEndTime;
   
    //Assign hours to the endtime - starttime
//divided by 3600 (seconds in an hour)
    hours:= (TrueEndTime-TrueStartTime)/3600;
)

//Else hours = how many hours on the two half days + how //many hours for the full days
Else
(
halfdays:= ((BusinessEndTime - time(FirstDateTime)) /3600 + (time(LastDateTime) - BusinessStartTime)
/3600);
fulldays:= (FinalDays-2) * HoursInADay;
hours:= halfdays + fulldays;
);
);

//DISPLAY NUMBER OF BUSINESS HOURS IN THE RANGE
//*********************************************************hours;

any help to fix this would be appreciated.





Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
//@Business.Hours.
//CALCULATE THE NUMBER OF BUSINESS HOURS 
//BETWEEN FirstDateTime AND LastDateTime 
//INCLUDING HOLIDAYS IN THE TOTAL
 
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//INPUT REQUIRED _ INPUT REQUIRED _ INPUT REQUIRED 
 
//Replace datetime(2001,12,23,11,50,0) with your starting //date
DatetimeVar FirstDateTime:= {Orders.Date1}; 
//Replace datetime(2001,12,26,10,0,0) with your ending //date
DatetimeVar LastDateTime:= {Orders.Date2}; 
//Replace Time(09,00,00) with your business starting time
TimeVar BusinessStartTime:= Time(07,00,00); 
//Replace Time(17,00,00) with your business ending time
TimeVar BusinessEndTime:= Time(16,00,00); 
 
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//END INPUT REQUIRED _ END INPUT REQUIRED _ END INPUT 
 
//Other variables used in the formula
Numbervar HoursInADay:= (BusinessEndTime - BusinessStartTime)/3600;
Numbervar Days;
Numbervar Weekends;
Numbervar Finaldays;
DateVar StartDate;
DateVar EndDate;
NumberVar halfdays;
NumberVar fulldays;
NumberVar hours;
timevar TrueStartTime;
timevar TrueEndTime;
 
 
 
//BEGIN FORMULA:
//*********************************************************
 
//FINISH FORMULA IF FirstDateTime OR LastDateTime IS NULL
//*********************************************************
IF FirstDateTime <=Date(0,0,0) or LastDateTime <=Date(0,0,0) then hours:= 0
 
//ELSE ASSIGN HOURS
//*********************************************************
ELSE
(
//ASSIGN FirstDateTime and LastDateTime
//*********************************************************
//Determine whether FirstDateTime falls within 
//Start Time to End Time
if time(FirstDateTime) in BusinessStartTime to BusinessEndTime then 
    FirstDateTime:= FirstDateTime
else if time(FirstDateTime) > BusinessEndTime then 
    FirstDateTime:= datetime(date(FirstDateTime)+1, BusinessStartTime)
else if time(FirstDateTime) < BusinessStartTime then 
    FirstDateTime:= datetime(date(FirstDateTime), BusinessStartTime);
 
//Determine whether LastDateTime falls within Start Time to End Time
if time(LastDateTime) in BusinessStartTime to BusinessEndTime then 
    LastDateTime:= LastDateTime
else if time(LastDateTime) > BusinessEndTime then 
    LastDateTime:= datetime(date(LastDateTime), BusinessEndTime)
else if time(LastDateTime) < BusinessStartTime then 
    LastDateTime:= datetime(date(LastDateTime)-1, BusinessEndTime);
 
//ASSIGN STARTDATE and ENDDATE
//*********************************************************
//if the first day falls on a weekend, 
 
If DayOfWeek(FirstDateTime) = 5 Then 
    StartDate := date(FirstDateTime) + 2
Else If DayOfWeek(FirstDateTime) = 6 Then 
    StartDate := date(FirstDateTime) + 1
Else StartDate:=date(FirstDateTime);
 
//if the last day falls on a weekend, 
 
If DayOfWeek(LastDateTime) = 5 Then 
    EndDate := date(LastDateTime) + 2
Else If DayOfWeek(LastDateTime) = 6 Then 
    EndDate := date(LastDateTime) + 1
Else EndDate := date(LastDateTime);
 
//CALCULATE DAYS AND WEEKENDS
//*********************************************************
//Calculate Days (including First day and Last day)
Days:= (EndDate - StartDate)+1;
 
//Calculate weekends 
if Days >= 7 then 
    WeekEnds := (Round((Days/7),0))*2
else if DayOfWeek(StartDate) > DayOfWeek(EndDate) then 
    WeekEnds := 2
else WeekEnds := 0;
 
//CALCULATE FINALDAYS
//*********************************************************
//If the Last Day is on a weekend then FinalDays subtract the weekend days
If DayOfWeek(LastDateTime) = 5 then FinalDays:= FinalDays - 1;
If DayOfWeek(LastDateTime) = 6 then FinalDays:= FinalDays - 2;
 
//Assign FinalDays to Days minus Weekends
FinalDays:= Days - WeekEnds;
 
//CALCULATE HOURS
//*********************************************************
//Calculate FirstDateTime and LastDateTime if falling on a weekend
//if the first day falls on a weekend, StartDate is equal //to the following Monday for calculation reasons
If DayOfWeek(FirstDateTime) = 5 Then 
    FirstDateTime := datetime(date(FirstDateTime) + 2, BusinessStartTime)
Else If DayOfWeek(FirstDateTime) = 6 Then 
    FirstDateTime := datetime(date(FirstDateTime) + 1, BusinessStartTime);
 
//if the last day falls on a weekend, EndDate is equal to //the following Monday for calculation reasons  
If DayOfWeek(LastDateTime) = 5 Then 
    LastDateTime := datetime(date(LastDateTime) + 2,BusinessStartTime)
Else If DayOfWeek(LastDateTime) = 6 Then 
    LastDateTime := datetime(date(LastDateTime) + 1, BusinessStartTime);
 
//If less than 24 hours involved
If FinalDays <= 1 then
(
    //If first day is the same day as last day
    if date(FirstDateTime) = date(LastDateTime) then
    (
        //If First Day starts before business start time, //assign TrueStartTime to business starttime 
        if time(FirstDateTime) >= BusinessStartTime then 
            TrueStartTime:= time(FirstDateTime)
        else TrueStartTime:= BusinessStartTime;
 
        //If Last Day ends after business end time, assign //TrueEndTime to business endtime
        if time(LastDateTime) <= BusinessEndTime then 
            TrueEndTime:= time(LastDateTime)
        else TrueEndTime:= BusinessEndTime
    )
    //If first day is not the same day as last day
    else 
TrueStarttime:= BusinessStartTime;
 
    if time(LastDateTime) <= BusinessEndTime then 
TrueEndTime:= time(LastDateTime)
    else TrueEndTime:= BusinessEndTime;
    
    //Assign hours to the endtime - starttime 
//divided by 3600 (seconds in an hour)
    hours:= (TrueEndTime-TrueStartTime)/3600;
)
 
//Else hours = how many hours on the two half days + how //many hours for the full days
Else 
(
halfdays:= ((BusinessEndTime - time(FirstDateTime)) /3600 + (time(LastDateTime) - BusinessStartTime)
/3600);
fulldays:= (FinalDays-2) * HoursInADay; 
hours:= halfdays + fulldays;
);
);
 
//DISPLAY NUMBER OF BUSINESS HOURS IN THE RANGE
//*********************************************************hours;
[+][-]01.29.2008 at 01:19PM PST, ID: 20772367

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]01.31.2008 at 12:11PM PST, ID: 20790511

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.01.2008 at 04:25AM PST, ID: 20795960

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.03.2008 at 12:36AM PST, ID: 20808197

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.10.2008 at 10:24PM PST, ID: 20864338

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.11.2008 at 08:50PM PST, ID: 20872572

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.23.2008 at 12:48AM PST, ID: 20964200

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.05.2008 at 10:37PM PDT, ID: 21290883

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]04.08.2008 at 09:00PM PDT, ID: 21311709

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Crystal Reports Software
Tags: Business Objects, Crystal Reports XI, XI, Oracle Database
Sign Up Now!
Solution Provided By: mlmcc
Participating Experts: 1
Solution Grade: A
 
 
[+][-]05.26.2008 at 02:03PM PDT, ID: 21648092

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]06.01.2008 at 06:19AM PDT, ID: 21686955

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628