Calculated Fields in an ABAP Query

Despite the prevalence of using BusinessObjects and BI Universes as a reporting tool for retrieving data from SAP, there are still times when there is a requirement to create ABAP queries directly in SAP.  I recently had a unique requirement to create an ABAP query for material master data where Sales Org, Storage Bin, and Storage Location needed to have fixed values based upon certain logic.  After searching the internet and piecing the solution together I decided to make a post to show how simple this task is.

There are many guides showing how to create ABAP queries and this post will not attempt to reinvent the wheel.  This post will show you how to create a calculated field based upon an existing InfoSet.

Log into SAP and enter t-code SQ02 to navigate to the Infoset: Initial Screen.  Select the existing InfoSet and click the Change button.

2015-10-12_12-32-01

The screen changes to the Change InfoSet screen.  Click the Extras button which brings up the Extras section on the right side of the screen.

2015-10-12_12-36-28

Once the Extras section is available click the Create button (in the screenshots above and below I have already created the three new fields).

2015-10-12_12-39-57

Enter a name for the new field and select the Additional field radio button and click continue or press Enter.

2015-10-12_12-42-34

A new window will appear.  Enter a long description in the Long text field and the name of the column in the Header field.  Enter the appropriate field Type, Length, and Output Length, and provide the ECC technical field name which the custom field will be similar to (the LIKE Reference is not required).  Click the continue button or press Enter to create the new field.

2015-10-12_12-45-37

Select the new field you want to provide logic for and click the CodingForAddition button.

2015-10-12_12-52-31

The screen changes and enables you to enter ABAP code to program logic for deriving the value of the new field.  It will be helpful to have some basic working knowledge of ABAP, however it isn’t very difficult to program basic conditional logic using ABAP’s IF statements.

The code below declares a variable sales_org which is similar to MVKE-VKORG.  The code then determines if the plant value for the given record begins with ‘US’.  If the Plant value does begin with ‘US’ then the sales_org variable will be assigned the value ‘0210’.  If the Plant value does not begin with ‘US’ then the sales_org variable will be assigned the same value as the “real” Sales Org value from column MVKE-VKORG.  We then pass the sales_org variable’s value to the SALESORG field we created.

2015-10-12_12-54-22

Likewise, if you just want to assign a default value you do not need to use an IF statement.  In this case you can simply assign a value directly to the new field you created.

2015-10-12_13-02-30

The new fields now appear in the Additional fields group in the Data fields section on the left side of the screen, however they are not available in the InfoSet just yet.  Select the Field Group on the right side of the screen which should receive the additional field.  After the Field Group has been selected right click the additional field on the left side of the screen and select Add field to field group (in the screenshot below the field was already added, so now the option has been changed from add to delete).  The new field will now be available in the Field Group.

2015-10-12_13-25-35

Leave a comment