Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Complex Formula Post Reply Post New Topic
Author Message
bmhardy
Newbie
Newbie
Avatar

Joined: 10 Jun 2011
Online Status: Offline
Posts: 3
Quote bmhardy Replybullet Topic: Complex Formula
     Posted: 14 Jun 2012 at 4:46am
Hi All,

My overall goal for my formula is to calculate profit margin between contract start and end dates for working days only. What I have to work with from the database is the start date, the end date and the margin rate.

The first problem is that there is a 6 month sliding scale for the margin amount.

Up to and including 6 Months = Rate 1
6 months to 12 months = Rate 2
12 months to 18 months = Rate 3
18 months to 24 months = Rate 4
24 months onward = Rate 5

The second problem is where the contract ends. Mostly they tend to end on the 6 monthly marker points but I need to take into account the fact it may end before, such as 2 weeks before.

I've included my basic formula that calculates the working days between two dates so I was hoping someone would help me expand on this to calculate the rest.

_________________________________________________________

WhileReadingRecords;
Local NumberVar Weeks;
Local NumberVar Days;

Weeks:= (Truncate ({rsp.EndDate} - dayofWeek({rsp.EndDate}) + 1
- ({rsp.StartDate} - dayofWeek({rsp.StartDate}) + 1)) /7 ) * 5;

Days := DayOfWeek({rsp.EndDate}) - DayOfWeek({rsp.EndDate}) + 1 +
(if DayOfWeek({rsp.StartDate}) = 1 then -1 else 0) +
(if DayOfWeek({rsp.EndDate}) = 7 then -1 else 0);   

Weeks + Days

________________________________________________________
...sorry I'm a technotard!

Regards

Ben
IP IP Logged
rkrowland
Senior Member
Senior Member
Avatar

Joined: 20 Dec 2011
Location: England
Online Status: Offline
Posts: 259
Quote rkrowland Replybullet Posted: 14 Jun 2012 at 5:32am
I'm unsure what exactly you want to do, but this should get you what I think you need.
 
numbervar i:= 14; //number of days to add onto enddate in case of early contract finish
datevar startdate:= {rsp.StartDate};
datevar enddate:= {rsp.EndDate};
numbervar monthdiff:= datediff("m",startdate,dateadd("d",i,enddate));
 
numbervar rate:=
if monthdiff >= 24 then Rate5?? else
if monthdiff >= 18 then Rate4?? else
if monthdiff >= 12 then Rate3?? else
if monthdiff >= 6 then Rate2?? else
Rate1??;
 
numbervar weekdays:= Datediff("d", startdate, enddate) -
Datediff("ww", startdate, enddate, crSaturday)-
Datediff("ww", startdate, enddate, crSunday);
 
rate*weekdays //I'm not sure what exactly you want to do here.
 
I'm not sure where your rate information is coming from but I'm sure you will. Also, you may want to change the >= to just > in rate variable.
 
Regards,
Ryan.


Edited by rkrowland - 14 Jun 2012 at 5:40am
IP IP Logged
bmhardy
Newbie
Newbie
Avatar

Joined: 10 Jun 2011
Online Status: Offline
Posts: 3
Quote bmhardy Replybullet Posted: 14 Jun 2012 at 5:50am
Thanks Ryan. I'll have a look and see if I can get this to work.
...sorry I'm a technotard!

Regards

Ben
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum