Crystal Reports JD Edwards EnterpriseOne and World Calculating Percentages Using Crystal Reports Table of contents INTRODUCTION... 2 GENERAL OVERVIEW... 2 CALCULATING A PERCENTAGE WITHOUT USING A SUMMARY FIELD... 2 CALCULATING A PERCENTAGE WITH A SUMMARY FIELD... 2 EXAMPLES... 4 SUMMARY... 11 Preferred Strategies, LLC 1
Introduction This document explores the techniques involved in calculating percentages using Crystal Reports and provides both a general overview as well as a detailed example of the steps used to build formulas that calculate percentages in Crystal Reports. General Overview A percentage may be described as a proportion where one number is compared with another number. Many people are familiar with the steps involved in calculating a percentage: divide the part by the whole where the part is the numerator and the whole is the denominator. The same concept applies in the calculation using Crystal Reports, the challenge is identifying the correct fields to use in the percentage calculation formulas. Database fields and formulas can be used to calculate a percentage in Crystal Reports, and many times percentage calculations include or are solely based on a subtotals or summaries of those fields. Calculating a percentage requires identifying which fields to use, and then creating a new formula that returns the desired percentage. It should be mentioned that difficulty often arises when new users attempt to identify which fields to use for their percentage calculation. This is especially true in the case when using summary fields. Calculating a Percentage WITHOUT Using a Summary Field When calculating percentages using database or formula fields and displayed in the DETAILS section of the report canvas, a new formula must be created. Using Crystal Syntax, a formula may be created as follows: {Part} % {Whole} Alternatively, here is another way of expressing the same formula: ({Part}/{Whole})*100 (multiply by 100 for display purposes) The first line of both formulas (if {Whole} = 0 then 0) is included as a test to determine if the denominator is zero. In the case that the denominator is a zero value, the whole formula returns a zero value. If that logic is not included, and the denominator is zero, the formula will produce an error when the report is refreshed. For purposes of this document we will use the first formula. Calculating a Percentage WITH a Summary Field Calculating a percentage using straight database or formula fields is fairly straightforward. The next step we will take involves calculating a percentage using summary or subtotal fields. Summary fields are produced when data in the details section of a report is summarized at a group level. For example: A Subtotal in Group Footer #1 returns the sum of all records that fall within Group Level #1. A Count Summary in Group Footer #2 counts all records in the details section for that particular group and returns that number in Group Footer #2. 2 Preferred Strategies, LLC
Any time a summary field is created, Crystal Reports creates a summary field in the Formula Editor of the Field explorer. A screenshot that displays summary fields found in the Field Tree in the Formula Editor is shown below. Notice how the summary fields are preceded by a Σ which indicates that the field is in fact a summary field. Also notice that the group level that the summary field resides in precedes the summary field, as well as the field used to group. In the sample above, Group #1 indicates the group level and @II Company indicates the field used to group. Understanding how to identify these summary fields is important because they will be used in the formula that you use to calculate your percentage. When you double-click on the summary to add the summary into your formula text, Crystal Reports displays the summary calculation slightly different than how it is displayed in the Field Tree. Summary Function Field or Formula being summarized Field or Formula used to create group Calculating a percentage using a summary field requires identifying the correct fields based on which field or formula is being summarized and which group level the summary field resides. Calculating Percentages using Crystal Reports 3
Lets look at an example to calculate an Object Account subtotal as a percentage of the Company subtotal, both based on a fictional amount called {Field}: if Sum ({Field}, {@II - Company}) = 0 then 0 else Sum ({Field}, {@II - Object Account}) % Sum ({Field}, {@II - Company}) In this formula, Sum ({Field}, {@II - Company}) is the whole or sum at the Company group level. Alternately, Sum ({Field}, {@II - Object Account}) is the part or sum at the Object Account group level. We will now go through an example to clarify the process of creating a percentage in a real report. Examples Lets say that you are building an income statement by company for your organization. You have created several groups on the report to display one line per object account and further group all object accounts based on whether they are classified as revenue, cost of materials, fixed expenses, etc. The report must include a column that calculates the Actual Amount Period Balance for each summary/group level as a percentage of total revenue for the company Looking at a pre-built income statement, we have created the groups and subtotals for Actual Amount. Starting with the lowest level summary displayed on the report, we are going to determine how much a particular object account subtotal contributes to a company as a percentage of all revenue contributed. The screenshot below displays part of the income statement without the desired percentages. We would like to insert a formula into the areas enclosed by the circle that determines the Actual Amount subtotal as a percentage of company revenue. In this report the formula will be placed in Group Footer #5 of the report. 1. Identify the part or numerator Identifying the numerator is fairly easy. In this example the subtotal of Period Actual Amount in the Group Footer #5 is the part, which is the same field displayed in the Group Footer #5 under the Actual column on the report. The name of the formula that the summary field has been created from in this report is 4 Preferred Strategies, LLC
{IV CY?Period Actual Amount}. In the formula field explorer, the summary field for Group #5: 2. Identify the whole or denominator In order to calculate a percentage based on total company revenue, you must first create a formula that isolates revenue accounts and insert a subtotal for that formula into the Company group footer. The subtotal in the Company group footer returns total company revenue and will now be used as the whole in the percentage calculation. In this report, the formula to isolate revenue: {IV CY? Period Actual Amount Revenue} if {@VIIII - *Criteria - Revenue Amounts} then {@IV - CY?Period Actual Amount}*-1 else 0 When the subtotal is inserted into the Company group footer, it creates the following summary field in the formula field explorer: 3. Create the percentage formula Start by creating a new formula in the field explorer and then giving that formula a name that identifies it as a percentage. At this point you will open the formula editor and look for the summary fields that you will use in the formula. We ve already identified the numerator or Part and the denominator or Whole {Part} % {Whole} Calculating Percentages using Crystal Reports 5
Next we will build the actual formula to express the percentage. The actual formula in this case looks like this: if Sum ({@IV - CY?Period Actual Amount - Revenue}, {@II - Company}) = 0 then 0 else Sum ({@IV - CY?Period Actual Amount}, {@II - Object Account}) % Sum ({@IV - CY?Period Actual Amount - Revenue}, {@II - Company}) Remember that this formula is specifically used to express the object account balance as a percentage of total revenue. Therefore the formula will be inserted into the report at group level #5. New formulas must be created in order to express percentages for other amounts, or summary fields in other groups. 4. Insert the percentage formula onto the report and format Save and close the formula then insert the formula into group footer #5 of the report. Preview the report to view the results. We can see that the percentage field is in the report and that it is calculating correctly. In order to display the field with the % sign the field must be formatted. You can format the field to display the % sign by highlighting the field (clicking on the field once) and then clicking the % in the toolbar. A screenshot showing the properly formatted percentage field is displayed below: 6 Preferred Strategies, LLC
Looking at the example above we see that the new percentage is displayed in Group Footer #5 in the Actual % column. When calculating percentages, you must create separate percentage formulas for each group level and report column combination. For example, the percentages displayed under % column for Actual, Budget and Variance columns in the Group Footer #5 are separate formulas. Similarly, in order to display a percentage in the same column but a different group, you would have to create a new formula that again calculates percentage but uses the other group s summary fields. Lets review the process again as we build a new formula that determines a similar percentage in the same column but for a summary in another group footer. In the sample above we also need to calculate the subtotal in Group Footer #4 as a percentage of total revenue. This formula will determine Total Revenue and Total Cost of Materials subtotals as a percentage of total Company revenue. Calculating Percentages using Crystal Reports 7
1. Identify the part or numerator In this example the subtotal of Period Actual Amount in the Group Footer #4 is the part, which is the same field displayed in the Group Footer #4 under the Actual column on the report. Revenue and cost of materials subtotals are created in the Group #4 based on the {@II.a - *Row Flag 1} formula. The name of the formula that the summary field has been created from in this report is {IV CY?Period Actual Amount}. In the formula field explorer, the summary field for Group #4: 2. Identify the whole or denominator Since the percentage formula is in the same column as the previous formula we created, the whole or denominator is the same. It is the total company revenue in the Company Group Footer. 3. Create the percentage formula Create the percentage formula using the identified numerator or Part and the denominator or Whole. {Part} % {Whole} Build the actual formula to express the percentage. The actual formula in this case looks like this: if Sum ({@IV - CY?Period Actual Amount - Revenue}, {@II - Company}) = 0 then 0 else Sum ({@IV - CY?Period Actual Amount}, {@II.a - *Row Flag 1}) % Sum ({@IV - CY?Period Actual Amount - Revenue}, {@II - Company}) Remember that this formula is specifically used to express the revenue and cost of materials balances as a percentage of total revenue. Revenue and cost of materials subtotals are created in the Group #4 based on the {@II.a - *Row Flag 1} formula. Therefore the formula will be inserted into the report at group level #4. 8 Preferred Strategies, LLC
4. Insert the percentage formula onto the report and format Save and close the formula then insert the formula into group footer #4 of the report. Preview the report to view the results. One final percentage that we will calculate in this exercise is the gross profit as a percentage of total revenue. In the sample above we also need to calculate the subtotal in Group Footer #3 as a percentage of total revenue. This formula will determine Gross Profit subtotal as a percentage of total Company revenue. 1. Identify the part or numerator In this example the subtotal of Period Actual Amount in the Group Footer #3 is the part, which is the same field displayed in the Group Footer #3 under the Actual column on the report. Revenue and cost of materials subtotals are created in the Group #3 based on the {@II.a - *Row Flag 2} formula. Calculating Percentages using Crystal Reports 9
In the formula field explorer, the summary field for Group #3: 2. Identify the whole or denominator Since the percentage formula is in the same column as the previous formula we created, the whole or denominator is the same. It is the total company revenue in the Company Group Footer. 3. Create the percentage formula Create the percentage formula using the identified numerator or Part and the denominator or Whole. {Part} % {Whole} Build the actual formula to express the percentage. The actual formula in this case looks like this: if Sum ({@IV - CY?Period Actual Amount - Revenue}, {@II - Company}) = 0 then 0 else Sum ({@IV - CY?Period Actual Amount}, {@II.a - *Row Flag 2}) % Sum ({@IV - CY?Period Actual Amount - Revenue}, {@II - Company}) Remember that this formula is specifically used to express the gross profit balance as a percentage of total revenue. The gross profit subtotal is created in the Group #3 based on the {@II.a - *Row Flag 2} formula. Therefore the formula will be inserted into the report at group level #3. 4. Insert the percentage formula onto the report and format Save and close the formula then insert the formula into group footer #3 of the report. Preview the report to view the results. 10 Preferred Strategies, LLC
Summary Here is a final summary of the important points to remember when building these formulas and inserting them into the report: Identify the section of the report where you would like to insert your formula that calculates the percentage. Identify the specific fields that will be used to determine the percentage. Create the formula in the formula editor. The formula should appear like this: {Part} % {Whole} Insert the formula into the report by dragging it onto the report canvas. Format the field to display the field as a percentage. Refresh the report and view the results. Calculating Percentages using Crystal Reports 11