I can't figure out why this if statement won't work. I have a DateTime field DATEFROM and a String parameter (it HAS to be String) periodEnd. I would like to calculate percentages depending if these two dates have 1, 2, 3 or more years difference. When I use this formula I get either "100%" or "-" and never the other two options. It's like CR calculates the first IF and if it's true then: "100%" but if it's false, it never checks for the rest of the Else Ifs and goes dirreclty to Else
StringVar percentage:=""; If (cDate({?periodEnd})-{TABLE.DATEFROM})<=1 Then percentage:="100%" Else If (cDate({?periodEnd})-{TABLE.DATEFROM})<=2 Then percentage:="66%" Else If (cDate({?periodEnd})-{TABLE.DATEFROM})<=3 Then percentage:="33%" Else percentage:="-" Any idea?
11 Answer
a) I assume you already made sure your periodend format matches with what cdate interprets?
If you just subtract them, Crystal by default returns the number of days between.
Two ways you can do year:
datediff("yyyy",cDate({?periodEnd},{TABLE.DATEFROM}) or
year(cDate({?periodEnd})-year({TABLE.DATEFROM}) b) You've also no doubt accounted for when your {TABLE.DATEFROM} is greater than cDate({?periodEnd} and you have a negative result?
Not sure if the following is the behavior you would want, but I'm throwing it in for your information
ABS(datediff("yyyy",cDate({?periodEnd},{TABLE.DATEFROM})) **make sure you check the help file for the datediff codes ("yyyy" etc) as they are not quite instinctive and can trip you up when you think you're using the obvious one
2