Thursday, June 26, 2014

Crystal Reports : Subtotaling Formulas

Let us suppose we have an Invoices report. This report is subtotaling a value called {INVOICES.TOTAL}, which is simply a "raw" dollar amount for each invoice. You could simply use the "Insert Summary" menu item under "Insert" in Crystal. However, this limits you if you ever need to modify the way Invoices are subtotaled.

It would be better to create a formula field called {@frmTotal} and then Summarize that.

The code for {@frmTotal} is simply {INVOICES.TOTAL}. Depending on your back-end database value, you may need to convert it with TONUMBER{INVOICES.TOTAL}.

The reason for this is simple; now that we have control over how an Invoice total is expressed, we can do anything we want, including multiplying each invoice by a "markup", like this:


Which returns each Invoice Total plus three percent of itself. Because you are still Summarizing {@frmTotal}, you need only change this in one place.

This is good practice as any Crystal Reporter knows that report usage has a way of changing. Leaving yourself "wiggle room" is always a good idea.

No comments: