Crystal Reports formula to grouping field - crystal-reports

I am trying to create a formula in Crystal Reports which would return the grouping field depending on what group the formula is placed. For example, if the formula inserted in the group row 'Month', the formula should return ‘command.month’. If the formula inserted in the group row 'Year', the formula should return ‘command.year’. Can anyone help with this, please?
Thank you.

If your date field format is MM/DD/YYYY then you can use below condition in Formula field. and use this formula field in grouping.
if {YourGroupFieldValue }='month' then
left({DateField}',2)
else if {YourGroupFieldValue }='Day' then
mid ({DateField}',2,2)
else
right({DateField}',4)
It should work because i have used it.

Consider you have two groups Month and Year. Add one formula in month group. In that formula create a shared variable as shown below.
Shared numbervar num:=0;
Create another formula in Year group. In that formula create a same shared variable as shown below.
Shared numbervar num:= 1;
Now create third formula, in that formula add below declaration.
Shared numbervar num;
if num = 0 then {your logic}
else if num =1 then {your logic};
Put your third formula in both groups and run the report. Same formula will show different values in different groups. I hope I got you this time... :)

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.

Sum a calculated constant on Crystal Reports - CR does not provide "SUM" Option and Manual "Sum" Generates error

I have a crystal report which requires to report on components (RUB_0) that are included on payslips, specifically basic salaries and pension contributions.
Certain employees could have more than one payslip (BUL_0) in a month and as such only the latest (max) payslip (BUL_0) per employee has to be reported since we are working with month to date figures.
My data looks as follows:
BUL_0 DAT_0 RUB_0 AMT_0
1700000018 2017-01-31 00:00:00.000 SALACT_MTD 20000.000000000000
1700000019 2017-01-31 00:00:00.000 SALACT_MTD 40000.000000000000
I have built the following formula which reports correctly for the latest (max) payslip in the detail section of the report.
IF ({HISTOPAYE.RUB_0} = "SALACT_MTD") AND {HISTOPAYE.TYP_0} = 6 AND {HISTOPAYE.BUL_0} = Maximum({HISTOPAYE.BUL_0}, {HISTOPAYE.EMP_0})
THEN {HISTOPAYE.AMT_0}
ELSE 0
Which displays on my report as per the following image which is correct (on detail level)
Current Crystal Output
updated image below:
Updated Crystal Output
When I try to add a summary for one of these formula fields, the "SUM" option is not available, nor is the actual report field. When trying to sum the value manually, crystal returns "This field cannot be summarized" warning.
I have found that the reason for this warning is because my formula above, is creating a constant which cannot be summed. I have tried casting the fields to different data types and have tried setting multiple formula's to reset the value as per the below article, however - it did not resolve my particular issue.
Crystal Report Sum of a Group Summary
I need to include a summary to the group footer of the formula written. Any assistance will be appreciated.
Try this:
Create a formula Initialize and place in report header
Shared Numbervar counter;
counter:=0;
Change your existing formula like below:
Shared Numbervar counter;
Local Numbervar display;
IF ({HISTOPAYE.RUB_0} = "SALACT_MTD") AND {HISTOPAYE.TYP_0} = 6 AND {HISTOPAYE.BUL_0} = Maximum({HISTOPAYE.BUL_0}, {HISTOPAYE.EMP_0})
THEN display:={HISTOPAYE.AMT_0}
ELSE 0;
counter:=counter+display;
display
Now create below formula in group footer
Shared Numbervar counter;
counter; //This will give the final output sum of all values

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

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

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.

crystal report use Maximum as Sum condition problem

I met a problem with sum function:
the data have a datetime column and I want to get sum of who's datetime is max. And also there's anoher group condition.
So I do like:
Add formula maxDatetime: Maximum({datetime}, {groupcondition})
Add new formula for sum ValueToSum: if({datetime} = {#maxDatetime}) then value else 0
Sum the value, add new formula totalValue: Sum({#valueToSum}, {groupcondition})
The result is the sum can't be processed, it says: 'valueToSum can't be sumed'.
I think the causor may be using a maximum value as a condition in step 2. Because when use 1 = 1 to replace the condition, there's no problem.
Can anyone give some advice?
We usually solve such kind of problems by creating additional queries and linking them to main data. Because Crystal Reports does only two passes over data - record reading and aggregate calculation, it can't easily aggregate over already aggregated values.
Another possibility is to accumulate your 'totalvalue' into variable. Something like next may work (needs tweaking probably):
WhilePrintingRecords();
NumberVar totalvalue;
If ({datetime} = Maximum({datetime}, {groupcondition}))
then totalvalue:=totalvalue+value
else 0
This formula (which displays totalvalue) needs to placed into group footer:
WhilePrintingRecords();
NumberVar totalvalue
Somewhere in group header you need another one to reset totalvalue:
WhilePrintingRecords();
NumberVar totalvalue:=0