Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Math Calculation Post Reply Post New Topic
Page  of 2 Next >>
Author Message
psalm19
Groupie
Groupie
Avatar

Joined: 19 Feb 2009
Online Status: Offline
Posts: 48
Quote psalm19 Replybullet Topic: Math Calculation
     Posted: 12 May 2009 at 12:15pm
Running CR v10
 
I'm attempting to calculate Margin % based on two fields in the report. To do so I added the following "Profit" formula:
 
{tblInvoices.TotalNetSell} - {tblInvoices.PostedCost}
 
I then added another formula "Margin" which I'm attempting to use to calculate the margin %.
 
{@Profit} / {tblInvoices.TotalNetSell}
 
When included in the report it complains with the following message: "Division by zero". I found an article which suggested I make adjustment below to the formula but I get the same error. Any ideas?
 
(If {@Profit} = 0 then 1
else {@Profit}) / {tblInvoices.TotalNetSell}
 
 
IP IP Logged
JohnT
Groupie
Groupie
Avatar

Joined: 20 Jan 2008
Online Status: Offline
Posts: 92
Quote JohnT Replybullet Posted: 12 May 2009 at 12:46pm
If you are getting a division by zero error, you need to check the  tblInvoices.TotalNetSell for a zero value.  That is the field causing the error. 
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 12 May 2009 at 12:58pm

you were close, you needed to account for the tblInvoices.TotalNetSell as a possible 0, also you proabably want a 0 rather than a 1 as the then statement as this woul be 0%. You can also use % instead of / to get a percentage:

If {tblInvoices.TotalNetSell} = 0 then 0
else
{@Profit} % {tblInvoices.TotalNetSell}


Edited by DBlank - 12 May 2009 at 12:59pm
IP IP Logged
psalm19
Groupie
Groupie
Avatar

Joined: 19 Feb 2009
Online Status: Offline
Posts: 48
Quote psalm19 Replybullet Posted: 12 May 2009 at 2:17pm
Many thanks to both of you!
 
DBlank your suggestion worked like a champ! Is it possible to include the percent sign "%" in the formula so results will display like so: 51.2% instead of 51.2?
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 12 May 2009 at 2:20pm
If {tblInvoices.TotalNetSell} = 0 then "0 %"
else totext(
{@Profit} % {tblInvoices.TotalNetSell},1) + "%"
 
Change the red number to match the number of decimal places you want to display.


Edited by DBlank - 12 May 2009 at 2:39pm
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 12 May 2009 at 2:38pm
if you do not want to convert it to text ...
right click on the field and select Format Field.
On the Number tab click on the Custom Style for the number format.
Click on the Currency Symbol tab.
Change the Synbol to "%".
Change the position to "-123%".
On the Number tab set your decimal value and any other formating you want.
 
 
IP IP Logged
psalm19
Groupie
Groupie
Avatar

Joined: 19 Feb 2009
Online Status: Offline
Posts: 48
Quote psalm19 Replybullet Posted: 12 May 2009 at 3:24pm

Awesome! Thank you very much!!!

IP IP Logged
CrystalKiwi
Newbie
Newbie
Avatar

Joined: 08 Jun 2009
Location: New Zealand
Online Status: Offline
Posts: 3
Quote CrystalKiwi Replybullet Posted: 08 Jun 2009 at 9:38pm

How do you get it to show a negative margin rather than 0%

Confused
Kiwi
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 09 Jun 2009 at 6:56am
If you are trying to show a percentage the formula will show this if your division field is negative.
From the example above
If {tblInvoices.TotalNetSell} = 0 then 0
else
{@Profit} % {tblInvoices.TotalNetSell}
if the @profit formula resulted in a negative amount the percentage would also appear as a negative amount.
The first part of the formula
"If {tblInvoices.TotalNetSell} = 0 then 0" is only being used to prevent the 'division by 0' error that could occur if the totalnetsell field is 0.
 
If you are having problems get the correct value in your report feel free to post more details for more specific assistance.


Edited by DBlank - 09 Jun 2009 at 6:58am
IP IP Logged
CrystalKiwi
Newbie
Newbie
Avatar

Joined: 08 Jun 2009
Location: New Zealand
Online Status: Offline
Posts: 3
Quote CrystalKiwi Replybullet Posted: 09 Jun 2009 at 2:44pm

Thanks very much for your help

I am still getting 0 as the result when the margin is negative, though?

My formular; If  {SALESLINE.LINEAMOUNT}= 0 then 0
else {@Profit}% {SALESLINE.LINEAMOUNT}

and @Profit ;{SALESLINE.LINEAMOUNT}-{@Line Cost}

and @line cost ;{SALESLINE.COSTPRICE}*{SALESLINE.QTYORDERED} this is giving a negative result
 
 
Kiwi
IP IP Logged
Page  of 2 Next >>
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