Comparing Last Year Period to This Year in Crystal Reports
September 18, 2008 Leave a comment
>This was one of the toughest things I had to figure out in Crystal because there are very few examples out there on the internet on how to have columns in a report that compare the previous year values to this year.
The challenge:
My CFO wanted a report that showed waste receipts (we are a landfill) that had several columns that looked something like this:
Last Year to date
This year to date
Variance in tons
Variance in %
Previous Year Last 30 Days
This Year Last 30 Days
Variance in Tons
Variance in Percent
Total Receipts Last Year
Forecast Receipts for this year
So I thought how in the world do you do this because crystal only has one detail section and in my head I couldn’t figure out how you would compare two years values with only one query. Here is what I found.
Crystal has built in functions to calculate what period a date is in. Which is great but the logic on how to apply it was escaping me. What I ended up doing is a detail section that looks like this:
I only needed a couple of fields from the database record, the date of the Transaction [TimeOut] the Transaction number [Ticket] and the Tons Received [Tons]
I had to hide the Tons field because we are going to use this for calculations but since a total on tons would be a total for all query results there is no need to use it. Then for each of the period fields I created formula fields using the build in period functions of crystal for example Last Year to Date Tons [LYTD_Tons] is
If {Ticket.TimeOut} in LastYearYTD Then
{@tons}
Else
0
So now I have a tons field that will only display a value when the date of the transaction is in last year to date. Otherwise this value will be a zero. By applying this same logic to the other period fields I can create summary fields that sum all of the period based fields. I placed this summary in the group for each customer. So what I get is a row on the report that shows Customer Name, LYTD, PYMTD, MTD, LY Total. Now that I have these fields I can create additional summaries that calucate the variance from last year.
The end result looks something like this:
In my humble opinion this is truly a work of art. If you have questions or would like more detail please add comments