Now that you have created a basic calculation and a simple conditional calculation, you can try something more dynamic. In some cases, you might want to perform a calculation or output a value based on a value that you may know only at report execution time (that is, a dynamic value). For example, you can use the same SAP query you have worked with throughout this chapter to create a calculated field to specify that the Discount field should subtract 100 or 200 or 500, with the actual numerical amount input at runtime. There are several real-world applications for this type of conditional calculation, including the following:
HR/payroll You could analyze how much it would cost for an employee on the third shift to work on a holiday by calculating the cost of increasing the hourly rate by $1, $2, and so on.
Finance You could monitor the impacts of accounts payable reports if an invoice were paid in 30 days, 60 days, and so on.
Logistics You could calculate the number of days a plant maintenance order has been open as of today, as of next week, and so on.
To create a calculated field that is based on dynamic selection screen input, you follow these steps:
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.
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. You will reuse the local field you created in the first example, Airfare Discount, which has the short name DISCOUNT.
Position your cursor in the Airfare Discount field and select Edit, Local Field, Change to change your existing calculated field rather than create a new one.
When the Field Definition dialog box appears, delete TIXCOST-100 from the Calculation formula box and select the radio button Input on Selection Screen. Doing so includes the Airfare Discount field on the report's selection screen so that you can input a value at report runtime.
Create a calculated field that shows the value of the amount entered in the Airfare field minus the amount entered on the selection screen in the Airfare Discount field. In this case, you can reuse the First Class Airline Discount field that you used in the previous example. To modify this existing field, position your cursor in the First Class Airline Discount field and select Edit, Local Field, Change.
When the Field Definition dialog box appears, modify the formula TIXCOST-100 to say TIXCOST-DISCOUNT and leave the Condition line as is (see Figure 9.7). Press Enter to proceed.
Figure 9.7. Instead of subtracting the initial Airfare (TIXCOST) value, you change it to subtract the amount entered on the selection screen in the Airfare Discount field (DISCOUNT).
Ensure that the First Class Discount field is selected on the Select Fields screen. Then navigate to the Basic List Line Structure screen by clicking the Basic List button on the Application toolbar. Confirm that it is set to be included in the report output by indicating a line and sequence number. (I listed mine as Line 1 and Sequence 8.) Click the Save button.
Execute your report by pressing F8. You are presented with the report's selection screen, which should contain an entry field labeled Airfare Discount (see Figure 9.8).
Figure 9.8. Any field indicated in the Field Definition dialog box as Input on the selection screen is automatically inserted on your report's selection screen.
Input a numeric value in the Airfare Discount field on the selection screen (for example, 250). Press F8 to see your updated SAP query, which now contains your newly added calculated field labeled First Class Airline Discount (see Figure 9.9).
Figure 9.9. The newly added column that contains the value of the Airfare field minus the value input on the selection screen displays only if the flight is first class.
You can return to the selection screen, modify the number inserted into the Airfare Discount field, and reexecute the report to cause a calculation to be performed dynamically each time.