Using a Simple Condition to Create a Basic Calculated Field


Now that you have created a basic calculation, you can take it a step further by using a condition to determine a calculation. In some cases, you might want to perform a calculation or output a value only if a certain condition exists. For example, in the SAP query you have been working with in this chapter, you could create a calculated field to specify that the discount field should have the 100 discount only if the passenger is listed as flying in first class. There are several real-world applications for this type of conditional calculation, including the following:

  • HR/payroll You could forecast how much it would cost to increase all associates' salaries by three percent within a department to assist in salary or budgeting projections.

  • Finance For fixed-asset management, you could determine which purchase requisitions are not assigned to an account.

  • Logistics You could calculate invoice payments based on the delivery dates and down payments.

For this example, you will continue working with the exercise query you created earlier in this chapter. To create a calculated field that is based on a condition, you follow these steps:

1.

Navigate to the main screen of the SAP Query tool by using transaction code SQ01, select the query you want to work with (for example, DLS_QUERY_09), and click the Change button.

2.

Navigate to the Select Fields screen by clicking the Next Screen button on the Application toolbar twice. If the short names are not already displayed, select Edit, Short Names, Switch On/Off to turn them on. Enter a short name for the Flight Class field (CLASS) in the Flight Booking Field group so that you can easily refer to it in your calculation.

3.

Position your cursor in the Flight Class field, which you will base your calculated field on.

4.

Select Edit, Local Field, Create. Because your cursor is in the Flight Class field, a custom field is added to its field group (Flight Booking Table).

5.

When the Field Definition dialog box appears, input a short name for your newly created local field (FDISCOUNT) and input a Field description (First Class Airfare Discount), which will be the heading for the column in the report (see Figure 9.5).

Figure 9.5. For this example, you input a short name and a field description, and you indicate for attributes that you want the field to be in the same format as the TIXCOST field.


Caution

The calculated field you created in the first example for airfare discount had the short description DISCOUNT. The calculated field you are now creating for this example is for first-class airfare discount, with the short name FDISCOUNT. Be sure not to get the two confused.

6.

Define the attributes for the field (refer to Table 9.2) by indicating that the field has the same attributes as the TIXCOST field. (Both are currency fields.)

7.

If it is not already selected, select the option button next to the Calculation Formula box at the bottom of the Field Definition dialog box and then enter your basic mathematical formula into the designated box to the right of it (refer to Figure 9.5). In this case, enter TIXCOST-100. Because this formula is based on a condition, also input CLASS = 'F' into the Condition line under the formula. This translates to an output in the new First Class Airline Discount field of the value of the Airfare field minus 100 only if the flight class is listed as F. Press Enter to proceed.

8.

To add the newly created field to your report output, confirm that the field is selected on the Select Fields screen by ensuring that the small box next to the First Class Airline Discount field at the bottom of the Flight Demo Table field group is selected.

9.

Navigate to the Basic List Line Structure screen by clicking the Basic List button on the Application toolbar. You see your newly created field listed there, under the name you gave it in step 5. Add the new local field to your report output on the Basic List screen of the SAP query report by indicating a line and sequence number. (I listed mine as Line 1 and Sequence 7.) Click the Save button.

10.

Execute your report by pressing F8. You are presented with the report's selection screen. Press F8 again to see your updated SAP query, which should now contain your newly added calculated field (see Figure 9.6).

Figure 9.6. The report now shows the newly added column that contains the value of the Airfare field minus 100 only if the flight is first class.


Defining Conditions for a Local Field

Conditions are special formulas that always evaluate to true (that is, the condition is satisfied) or false (that is, the condition is not satisfied). A condition consists of a single comparison, although it is possible to link comparisons by using logical operators. A comparison consists of two operands linked by a valid operator, as shown in Table 9.4.

Valid operands are used in the SAP Query tool just as they are used in basic math. The processing sequence conforms to the usual rulesthat is, negations (NOT operations) take priority over AND operations, which in turn take priority over OR operations. However, you can override this sequence by using parentheses. For more information on SAP calculation conditions, please refer to the SAP online help at http://help.sap.com and then select SAP Library, Cross Application Components, CA - Drilldown Reporting, How to Process Report Lists, Defining Display Conditions.

Table 9.4. Valid Operators for Linking Operands and Comparisons

Operator

Description

Operators for Linking Operands

 

=

Equal to

<>,><

Not equal to

<

Less than

<=,=<

Less than or equal to

>

Greater than

>=,=>

Greater than or equal to

Operators for Linking Comparisons

 

NOT

Negation of the result of a subsequent comparison

AND

AND link of two comparisons

OR

OR link of two comparisons





SAP Query Reporting
SAP Query Reporting
ISBN: 0672329026
EAN: 2147483647
Year: 2006
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net