Friday 21 April 2017

"Next" and "Previous" Functions in Crystal Report

Today  I am going to write about  the "Next" and  "Previous" Functions available with Crystal Reports.  As their names describe the intent of these functions is to provide you a value of your selected field for either  the Next or the Previous record as shown in your report.
For example if I wanted to determine the number of days it has been since a History record has been written against an Account in SalesLogix,  I could use these two functions along with the DateDiff function  to organize our data.  Let me give you an idea of how!
First, create a SalesLogix report with the History table.  Make sure to create two groups, group 1 is for the Account Name - sorted ascending, group 2 is by History Complete Date - sorted ascending by second.  The group level fields are used in the report.  Use your Section Expert to have group1 overlay group2.  Set the fields in the two groups so they would line up into columns if they were next to each other.  Also,  I  suggest suppressing all group footers and the detail section in your report.
Now create a formula field called "Next" and add the following formula:
IF {HISTORY.ACCOUNTNAME} = Next ({HISTORY.ACCOUNTNAME}) then CSTR(Next ({HISTORY.COMPLETEDDATE})) else ""
This formula states if the current records Account Name does not equal the next records Account name then do not show the next records Complete date.   If you do not include the account comparison in the formula then "Next" will display a value for the next record even if it is not from the same account. 
Add this field anywhere in History Group 2.  If you put this field by the complete date you will see that the field displays the date of the next records complete date.  The last record under the group for the Account will be blank.  What I want is this last History record that was added for this Account and none of the others so I will use this blank field to my advantage.  My group 2 suppression formula looks something like this.
{@Next}<>""
This should bring you down to just one record per account.  Since none of the data showing has a "Next" value so remove that field from the report.
Let's go ahead and create another field called "Previous" using the following formula:
IF {HISTORY.ACCOUNTNAME} = Previous ({HISTORY.ACCOUNTNAME}) then CSTR(CDATE(Previous ({HISTORY.COMPLETEDDATE}))) else ""
This field is very similar to the "Next".  Add this field to the report and you will see that you now have the date of the previous History record.  If the value is blank then there is not a previous record in the system
So you now know the Complete date of  the last History record along with the Complete Date of  record prior to the last History record, but how long has it been since the last History record?  Create one final formula field called "Days Since" using this formula:
DateDiff ("d",{HISTORY.COMPLETEDDATE},CurrentDate)
Add this to your group 2 and you will have all the information you need to see what Accounts are in need of some attention.

1 comment:

  1. I am looking for a simple way to choose records that have a start day for the previous Sunday. I have used parameters before but this needs to be automatic so no one has to set the parameter manually. Please help. Thank you.

    ReplyDelete

Control Structures (Crystal Syntax) In Crystal Report : If Expressions (Crystal Syntax) in Crystal Report

Formulas without control structures execute each expression in the formula exactly once when the formula is evaluated. The expressions are...