Crystal reports formula using variables - crystal-reports

I need a formula like this:
if {#TarSale_TimeWise}= 0 or isnull({#TarSale_TimeWise}) then '-'
else
totext(round(({#ActSale_TimeWise}-{#TarSale_TimeWise})/{#TarSale_TimeWise}*100,1),1)
Here the formula for {#TarSale_TimeWise} is
round({CatTimeWise.tarSale},1)
Similarly the formula for {#ActSale_TimeWise} is
round({CatTimeWise.ActSale},1)
I need to take rounding as 1 for both the fields and then I need to do the calculation for var sale with values round as 1 for both the formulas.
Now instead of creating this below 2 formulas and using those in 3rd formula I want to create only one formula using variables.
How can I modify above formulas as 1 formula?

Create only one formula and write below code:
Local Numbervar a1:=round({CatTimeWise.tarSale},1) ;
Local NumberVar a2:=round({CatTimeWise.ActSale},1) ;
if a1= 0 or isnull(a1) then '-'
else
totext(round((a2-a1)/a1*100,1),1)

Try:
If Isnull({CatTimeWise.tarSale}) Or {CatTimeWise.tarSale}=0 Then
'-'
Else
ToText( Round( (({CatTimeWise.ActSale} - {CatTimeWise.tarSale}) / {CatTimeWise.tarSale}*100), 1), 1 )

Related

Average data which comes out of a Crystal Report formula

I have a formula in my Group Header which looks like the following:
if count({Removals.rpid},{Removals.r-ixservice}) <> 0 then
( sum ({#SIT - Count},{Removals.r-ixservice}) / count({Removals.rpid},{Removals.r-ixservice}) * 100)
else 0;
This works perfectly in that it spits out a % number based on the fields above. However, in the Report Footer I would like to average all the data generated by the above formula and spit it out so that if in the report this formula had been on 3 lines saying 90-80-70, then in the footer it would say 70.
I tried using Average({#Formulaname})but Crystal just says "This field cannot be summarized".
Thanks.
Its already a summarized formula so you can't summarize it again..suggested option would be use arrays and summarize.
Try this
Use the formula in group header and supress
Shared Numbervar array store;
if count({Removals.rpid},{Removals.r-ixservice}) <> 0 then
store:=store+( sum ({#SIT - Count},{Removals.r-ixservice}) / count({Removals.rpid},{Removals.r-ixservice}) * 100)
else store:=store+0;
1
Now in report footer
Shared Numbervar Array store;
Local Numbervar i;
Local numbervar final;
For i:=1 to Ubound(store) do
(
final:=final+store[i];
);
Final/Ubound(store);

Crystal Reports - Disable default rounding/Show all decimals

If I create a Formula Field A containing only 1/1000 and then drag it out into the report it will show "0,00" due to default number of decimals is set to 1.00 and default rounding is set to 0.01 for a field of with the type Number.
No problems.
When decimals and rounding are adjusted it will show 0,001.
When I create Formula Field B that only contains {#A} and increase decimals and rounding it will show as "0,001000000000".
But if I use A to create a text, totext({#A}) + ' test', the result will be "0,00 test" and I can not figure out a way to show the correct "0,001 test" without adding totext({#A},3). I don't want to use rounding because the number of decimals in A varies and I don't want to show any zeroes at the end.
I did modify this code (could be used in conditional formatting of decimals for a number field):
If CurrentFieldValue = Int(CurrentFieldValue) Then 0
Else If CurrentFieldValue * 10 = Int(CurrentFieldValue * 10) Then 1
Else If CurrentFieldValue * 100 = Int(CurrentFieldValue * 100) Then 2
Else If CurrentFieldValue * 1000 = Int(CurrentFieldValue * 1000) Then 3
Else If CurrentFieldValue * 10000 = Int(CurrentFieldValue * 10000) Then 4
Else DefaultAttribute
to
If {#A}= Int{#A} Then totext({#A},0)
Else If {#A}* 10 = Int{#A}* 10) Then totext({#A},1)
...
It works but I was hoping I wouldn't need all that extra code, I already have performance issues in my Crystal Reports and I don't want the risk of adding to that.
I just want to use the actual value in {#A} no matter the number of decimals.
I guess this could be caused by both default rounding settings as default number of decimals
And now I realise this might be caused by default number of decimals, not rounding.
If I change A to (1/1000)*6 it will show up as 0,01 unformatted, B will show "0,00600000 test". So it is a rounding problem.
Leave the output of your desired formula as a number.
Right click on the formula and select format field.
In the number tab select Custom Style and click customize.
For rounding option select 0.0000000001
Click the X-2 next to the Decimals option and enter this formula
local numbervar a;
local numbervar b;
for a := 1 to 10 do
(
if mid(strreverse(totext(CurrentFieldValue,10)),a,1) <> "0" then
(b := a;
a := 10);
);
11- b
I have found that the default rounding is defined by your system regional settings. If you increase the number of decimal places from 2 (usual) to say 8, all your problems will be resolved.

Sumifs in Crystal

I'm working on a database that basically looks like this (in its simplest form):
{Phase} {Code} {Qty}
Example:
{Qty} for {Phase}="R" and {Code}="Nat" = 0
{Qty} for {Phase}="F" and {Code}="Nat" = 5
{Qty} for {Phase}="R" and {Code}="Int" = 10
{Qty} for {Phase}="F" and {Code}="Int" = 15
I am trying to get a result to show me the Qty for phase "R" and Code "Nat" (where R is <> 0) otherwise give me the qty for phase "F". So for the above example I would get an answer = 5 for Nat (because qty for phase R is 0) and an answer of 10 where the code is Int (because qty for phase R <> 0)
I have used three formula fields to do this:
1: if ({PHASE}="F" and {CODE}="NAT") then {QTY} else 0
2: if ({PHASE}="R" and {Code}="NAT") then {QTY} else 0
3: if {2} = 0 then {1} else {2}
Formula fields 1 & 2 come up with the correct amounts. However formula field {3} returns both phases. For example Code "Int" Phase "R" shows as qty = 25 instead of qty = 10.
How do I get around this?
You need to group by {table.code} because this is not a one-row calculation, but needs to be a calculation over each code for 2+ phases (meaning 2+ rows of data).
Create a formula with two variables that will store the values of each phase, F and R. This formula needs to go in the Details section of the report.
whileprintingrecords;
numbervar Fqty;
numbervar Rqty;
if {table.phase}="F" then Fqty:={table.qty}
else Rqty:={table.qty};
Now, in the group footer, you can reference both quantity values via the variables.
whileprintingrecords;
numbervar Fqty;
numbervar Rqty;
if Fqty=0 then Rqty else Fqty
And you're done. Don't forget to reset the two variables in the group header so you don't carry the quantity over between different codes.

crystal formula to compare a string field and a number field

I'm trying to compare two fields that contain up to 2 digit field.
One formula field is a {string} the other to compare to is a {number} field.
But i"m running to a problem when the string field shows for example "08" and the number field shows "8" then it will show 1 that there is a differences but actually there is no difference. If the string field shows 14 and the number field shows 14 that works perfectly but anything between 1-9 will show a difference when actually there is no difference.
This is what I tried so far.
If {number.field} = 0
Then StringVar AdjustValue:= " "
Else StringVar AdjustValue:= totext ({number.field},0,"")
;
if {#stringfield} = StringVar AdjustValue then 0 else 1
Thanks in advance for the help.
Instead, convert from string to numeric so you don't have to worry about leading zeros.
if tonumber({#stringfield})={number.field} then 0 else 1
And just a quick side note: You are re-declaring the AdjustValue variable three times. There's no need to refer to it via "StringVar" after an initial variable declaration within the same formula.
EDIT: Since you're having problems with the top formula, you could also try the alternative of just padding your AdjustValue variable to two spaces:
stringvar AdjustValue;
If {number.field} = 0
Then AdjustValue:= " "
Else AdjustValue:= totext ({number.field},"00") //add padding
;
if {#stringfield} = StringVar AdjustValue then 0 else 1
Keep it simple with a formula field that will return a Boolean value:
// {#compare}
ToNumber({#string})={number.field}
Reference the {#compare} where ever you need it.

Crystal report dynamic formula name

Here is what I have. I have 15 unique formulas named Week1, Week2, ... Week15. I would like to be able to take a parameter name, MYCount, and use that to loop through the records and sum them. So if MyCount is equal to 3, the loop would sum Week1 + Week2 + Week3. I know how to create a loop, but I cannot figure out how to build the formula name dynamically. Here is what I have so far: (I am using Crystal Xi)
Whileprintingrecords;
local NumberVar i := {?MyCount}
For i := 1 To (MyCount-1) Do (
i = {#Week & "i"} + i
);
x
I think you may be over complicating. Why not just do:
Local numbervar x := 0;
If param > 0 then
X := x + week1;
If param > 1 then
X := x + week;
And so on...
X;
I don't exactly what you are trying to do, but you may want to consider another approach.
Create a formula that will segment a field based on a date field's week number:
//{#amount}
// adjust firstDayOfWeek and firstDayOfYear parameters to match your organization's
// definition of a week
If DatePart("ww", {table.dateField})<={?Week} Then
{table.amount}
Insert a summarized field on the formula field.
If you are using your #Weekn formula fields so that you can summarise by date across the page, then have you considered using Crystal's Crosstab functionality instead?