Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Subtract using a formula field Post Reply Post New Topic
Author Message
dallasf
Newbie
Newbie
Avatar

Joined: 11 Apr 2013
Online Status: Offline
Posts: 26
Quote dallasf Replybullet Topic: Subtract using a formula field
     Posted: 17 Mar 2014 at 7:16pm
Hi All,

I have to do the difference of two fields, which is quite easy in itself. However, one of those fields is a formula.

The reason for the formula is NULL values are entered by the software if nothing is entered into that field. I altered the report to add a formula field as below.


if isnull({RptHRPOSN_PositionDifference.Position_Budget}) then 0.00 else {RptHRPOSN_PositionDifference.Position_Budget}

This formula allows for the 0.00 value to appear if NULL is in the table.

Now I want to do the difference of this field and another which is ({RptHRPOSN_PositionDifference.Wage_Allocated}).

However, creating the difference of the two gives me the error "A formula cannot refer to itself, either directly or indirectly"

Thanks for your help.
IP IP Logged
kevlray
Admin Group
Admin Group
Avatar

Joined: 29 Oct 2009
Online Status: Offline
Posts: 1587
Quote kevlray Replybullet Posted: 18 Mar 2014 at 5:46am
You could do this (probably).  One formula that looks like this.

{RptHRPOSN_PositionDifference.Wage_Allocated}- (if isnull({RptHRPOSN_PositionDifference.Position_Budget}) then 0.00 else {RptHRPOSN_PositionDifference.Position_Budget})

Of course I may have the order wrong for the subtraction.  The error you were getting refers to using a the name of the formula in the formula you are working in (i.e., formula name test;  then using that name in the formula : {@test}-2).

I hope this makes sense.
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 18 Mar 2014 at 7:05am

you can also just set the formula field to 'use default values for nulls' then subtract them without using anther formula field

 
{RptHRPOSN_PositionDifference.Wage_Allocated}-{RptHRPOSN_PositionDifference.Position_Budget}
 
IP IP Logged
dallasf
Newbie
Newbie
Avatar

Joined: 11 Apr 2013
Online Status: Offline
Posts: 26
Quote dallasf Replybullet Posted: 18 Mar 2014 at 11:54am
Thanks Kevlray, that worked thanks.
DBlank, thanks for the reply, I'm not sure how I would go about doing what you said, could you direct me? I'm interested to know the alternate way too.
IP IP Logged
kevlray
Admin Group
Admin Group
Avatar

Joined: 29 Oct 2009
Online Status: Offline
Posts: 1587
Quote kevlray Replybullet Posted: 19 Mar 2014 at 4:52am
Supposedly, if the default value for Nulls is zero for numeric fields, then DBlank's idea should work.  I cannot remember if I have tried that anytime recently. 
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 19 Mar 2014 at 6:51am
depending on which version you are using, inside the formula editor their is the option in a pick list to set how to handle Nulls.
If you do notuse defualts for a null and do not explicitly sate in the formula what to do with the null it will not evaulate on the null and return nothing for that formula result for that row.
By using the default values for null records it can drastically reduce you efforts. Each default will bebased on the data type. as kevlray indiactes if it is numeric it would use a 0.
Using that feature can save you loads of time an energy by not having to write in every variation for handling where nulls might be in every field you are using in that formula.
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