Crystal Reports - Selecting data in the first row to be used in a formula - crystal-reports

I have a very basic report with no groups. The details consists of a number of numeric columns. I want to add a formula field which will use a field from the first row. Example:
(ColumnAFromCurrentRecord - ColumnBFromFirstRecord) / ColumnBFromFirstRecord
Can someone point me in the right direction?

1.
Create a formula named AssignFirstValue and define it like this:
WhilePrintingRecords;
Global StringVar strFirstValue := {MYDBNAME.MYSTRINGFIELD};
Put the AssignFirstValue formula in your Report Header and suppress it.
2.
Create a formula named PrintFirstValue and define it like this:
WhilePrintingRecords;
Global StringVar strFirstValue;
strFirstValue
Put the PrintFirstValue formula wherever you want to display the first value in your report.

you need to extact the column b from first record. try below solution.
Note: This is not a tested solution as I at present I don't have CR.
create below formula Count and place in detail section:
Local NumberVar a;
a:=a+1;
This will number the rows starting from 1 to the last row.
Now create one more formula StoreB:
Share NumberVar b;
if {#Count}=1
then b:=columnb
This will store the value of columnb in first record in variable b
Now use where you want:
EvaluateAfter(#StoreB)
Shared NumberVar b;
(ColumnAFromCurrentRecord - b) / b
Let me know how it goes.

Related

How can I sum formula field in crystal report?

I want to show my Grand Total values by using a specific formula field.
Actually I want to sum formula fiel such as :
sum(#ClosingBalance)
But It gives a message
This Field Can not be summarize
How can I solve this problem ??
I solved my problem,
I explain below how can I solve this,
I need two other function to show grand total in my report footer, where I use formula field as my summarized field.
so, firstly I create a function name #Grouptotal by using below formula
Numbervar x:=x+{#ClosingBalance} And place it in my group footer
Then I create another formula name #GrandTotal and place it in my report footer
Numbervar x:={#Grouptotal}-{#ClosingBalance}
Finally I suppress the #Grouptotal Field , and get the result of #GrandTotal By using Formula Field.
Thanks a lot...
Yes, you can do this using variables.
Step 1: Create a Formula Field Named as Sum1:
Shared numberVar Sum1;
Sum1 := (Your Table Field) + Sum1;
Place this one in Details section and suppress it (Right Click Formula --> Format Field --> Common --> Suppress).
Step 2: Create another formula to display the result as PrintSum1:
Shared numberVar Sum1;
Sum1;
Place this formula in Group Footer and suppress it (Right Click Formula --> Format Field --> Common --> Suppress).
Step 3: Create another formula field:
Shared numberVar GrandTotal;
GrandTotal := GrandTotal + {#Sum1}
This way you can summarize a formula field. Place this one in group footer.
Step 4: Create one last formula as Clear:
Shared numberVar Sum1;
Sum1 := 0;
Place this formula in Group Header and suppress it. This is to clear the Sum1 value for every record or group.
Let me know if you have issues.

Summarizing Shared Variable in Crystal Reports

I have a formula that uses two shared numbers in it. I now need to sum this formula. If I use running total or sum functions, the formula does not show in the pick list.
The formula I need to sum is called "excel formula" and this is the calculation the formula is doing:
({#Shared Total Wt Lbs}*{#Shared Scrap})/100
The "usual way" of creating a sum or running total does not work for a formula that holds a variable .
To get a sum of it you'll need to use a third variable.
Place the following formula in detail-section to add up the values:
WhilePrintingRecords;
NumberVar SumOfExcelFormula;
SumOfExcelFormula := SumOfExcelFormula + {#excel formula}
Use the following formula to show the sum:
WhilePrintingRecords;
NumberVar SumOfExcelFormula;
To reset the sum on every group change, put the following code in a formula-field and place it on the group header:
WhilePrintingRecords;
NumberVar SumOfExcelFormula := 0;

Crystal Report: Summarize multiple date fields in one

please help me..I've been trying to search this for weeks but can't find the answer..
Given: Currently I have these dates field on my crystal.
01/01/2015
02/24/2015
02/27/2015
02/28/2015
02/29/2015
How can I summarize it in one field to be like this,
01/01/2015, 02/24/2015, 02/27-2015 - 02-29-2015
generally, separate different dates with comma and join dates that are in range or consecutive in order.
Thank you very much.
There is no direct way of summarizing the field in crystal report like you mentioned in your question.
However, you could try a work-around.
Create a Group and group by the 'date' field you want to summarize and then-
1) Create a formula with a shared variable to be placed in the header section of the report:
shared stringVar result="";
2) Create another formula with the same shared variable to be placed in detail section which concatenates your result into a comma separated array:
whileprintingrecords;
shared stringVar result;
if(result='')
then result:=CStr ({Date1.Date1_Text})
else
result:=result + "," +CStr ({Date1.Date1_Text});
Here ,{Date1.Date1_Text} is the date field which you want to summarize.
3) Create another formula with the same shared variable to be placed at the report footer:
shared stringVar result;
NumberVar i;
StringVar array dates := Split(result,",");
//Here you could put your logic to convert dates to date range.
//....
result; // returns final result

Concatenate string field in crystal reports

I have below data in my detail section.
Contract No
VTC/2013/0026
VTC/2013/0028
VTC/2013/0030
These data are belong to a one field in a table and there can be more.
I want to display the same details in the report header like below.
VTC/2013/0026, VTC/2013/0028, VTC/2013/0030, ...
How can I do that without using a sub-report?
Follow below steps:
Create a formula Concat and write below code and place in right most part of the report.
Shared Stringvar a;
a:=a+Contract No+", " ;
now create one more formula display and place in report header.
EvaluateAfter(#Concat);
Shared Stringvar a;
a

Increment a number for select group header sections per group

I have a unique question that I am not sure is even possible.
I have a report that has several Group Headers per group. What I would like to do is have select Group Headers (GH1a, GH1p, GH1w...) have an auto incremented number, which I will then turn into an roman numeral using roman(#function). The numbering would be independent on the number of actual groups, only Group Headers. There could be one to 100,000 records (groups) returned to the report but for each record(group) the Group Headers within each group would have select Group Headers numbered the same
EX:
GROUP A
GH1a: I
GH1b:
GH1c: II
GH1d: III
GH1e:
GH1f:
GH1g: IV
GROUP B
GH1a: I
GH1b:
GH1c: II
GH1d: III
GH1e:
GH1f:
GH1g: IV
etc....
Any help would be appreciated. I have tried to use a global variable x in both the report header and each GH that I want incremented using the following code:
in reportheader and/or GH1a //#iCountreset
Global NumberVar iCount:=1;
in each GH that I want incremented //#iCounted
Global Numbervar iCount;
iCount:= iCount+1;
then a second one to romanize it //#RomanCount
roman(#iCounted);
I currently have them hard coded but am trying to combine several rpt files into one where the only difference would be some GH sections would be suppressed and therefore not counted in the numbering.
Thank you in advance.
//{#reset}
WhilePrintingRecords;
Global Numbervar G1:=1;
//{#increment}
WhilePrintingRecords;
Global Numbervar G1:=G1+1;
//{#roman}
Roman({#increment})
** edit **
Ryan's comment was correct. My new approach uses subreports to increment a shared variable, which works.
Steps:
add {#reset} GH1a of 'main' report; suppress
//{#reset}
WhilePrintingRecords;
Shared Numbervar G1:=0;
create a subreport; place it in GH1b; add these formulae to it:
add to Details section; suppress:
//{#increment}
WhilePrintingRecords;
Shared Numbervar G1:=G1+1;
add to Details section:
//{#roman}
Roman({#increment})
You will need to add this subreport to each section that requires a Roman-numeral calculation. To make this process a little less painful, export the subreport ('save subreport as'), then reinsert it.