An expression that does division, such as calculating profit margin, is trickier than it looks. The problem is how do you calculate a group or end-of-report value?

For example, you can create a formula to calculate the profit margin of an item using something like "(SaleAmount - Cost)/SaleAmount * 100." That expression works correctly for detail items but not for groups. Suppose an invoice has one item sold for $2.00 that you paid $1.00 for (a 50% margin) and another item for $20.00 that you paid $15.00 for (a 25% margin). The total sale amount for the invoice is $22.00 and the total cost of the items is $16.00 for an overall margin of 27%. For a group footer for that invoice, you clearly can't sum the margins (25 + 50 = 75%), nor can you average them ((25 + 50)/2 = 37.5%). What you really need the report to do is to total the sales amounts, total the cost amounts, and then calculate the margin at the group level from those totals.

Fortunately, Stonefield Query has a way to do that: formulas with group level recalculation. See the help topic for “Creating Your Own Formulas” for a discussion of how to use that feature.

If you’re doing the calculations manually in the Advanced Report Designer rather than with formulas, you need to calculate the margin with variables. In the Advanced Report Designer, choose Variables from the Report menu, click Add, enter a name such as GroupPrice, and use these settings:

*Value to store*: the name of the field containing the price, such as EXTPRICE *Initial value*: 0 *Reset value*: choose which group to restart the calculation on *Calculation type*: Sum

This tells Stonefield Query to sum up the price field at the group level. Create another variable named TotalPrice with the same settings but Reset Value should be Report so it sums the price field for the whole report.

Create two more variables, GroupCost and TotalCost, that are similar but use the name of the cost field instead.

Finally, use these variables in fields in the Group Footer band and Summary band to display the overall margin. For example, for the one in the Group Footer band, use:

iif(GroupPrice = 0, 0, (GroupPrice - GroupCost) / GroupPrice * 100)

That way, the calculation is done the same way you'd do it by hand: total up the price, total up the cost, and do the calculation on the totals.

## 0 comments:

Post a Comment