Writing a Key Figure Calculation

Objective

After completing this lesson, you will be able to write a key figure calculation

Key Figure Calculation

Once you have created a key figure, you can add calculations to it. Note the following modeling requirements:

  • Any key figures that an end user is able to query from the UI must have a calculation at REQUEST level.

  • A calculation at REQUEST level either must be an aggregation, or must have inputs from REQUEST level only.

  • You can define calculations that aggregate the key figure data from a lower base planning level using an operator, such as SUM, MIN, or MAX.

  • You can also define calculations across key figures, for example, KF1 plus KF2.

  • A key figure referenced in a calculation must be specified as an input key figure for the calculation.

  • Calculation inputs can be marked as stored or calculated. The calculation chain for every key figure must result in a stored key figure. For example, KF1@PL1 = KF2@PL1 plus KF3@PL1, key figure 2 (KF2) is a stored input key figure, and key figure 3 (KF3) is a calculated input key figure. The calculated chain for key figure 3 (KF3) must finish with a stored key figure (such as KF3@PL1 = SUM(KF4@PL2), where KF4@PL2 is a stored key figure.

Any key figure not marked as stored forces the system to locate a calculation to determine the values of the key figure for the calculation. In this way, calculations are linked together.

A complete list of requirements is listed in the Model Configuration Guide >> Modeling Requirements (Checks and Errors).

Commonly Used Functions and Expressions

Examples of Standard Functions

SyntaxExample
IF(intarg , arg2, arg3)

KF1@PL = IF("KF2@PL" >"KF3@PL", 1, 0)

JF(intarg, arg2, arg3) → same syntax as above, but different SQL semantic

KF1@PL = JF("KF2@PL" >"KF3@PL", 1, 0)

ISNULL(arg1)

MARKETINGFORECASTQTY@PERPRODCUST = IF(ISNULL("MARKETINGFORECASTQTY@PERPRODCUST"),

"SALESFORECASTQTY@PERPRODCUST",

"MARKETINGFORECASTQTY@PERPRODCUST")

ABS(arg)

IF(ABS("SUPPLYREV@PERPRODFML" -

"CONSENSUSDEMANDREV@PERPRODFML")/"CONSENSUSDEMANDREV@PERPRODFML" > 0.2,1,0)

ROUND(double, int)

KF1@PERPRODLOCSRC =ROUND("KF@PERPRODLOCSRC",0)

EXP(arg1)

KF@MTHPRODLOC =EXP("SKF@MTHPRODLOC")

LOG(arg1)

KF@MTHPRODLOC =LOG("SKF@MTHPRODLOC")

SQRT(arg1)

SQRT@MTHPRODLOC =IF("SKF1@MTHPRODLOC" = 1,

SQRT("SQUAREDEMAND@MTHPRODLOC"), - 1)

arg1**arg2

KF@MTHPRODLOC ="SKF01@MTHPRODLOC" **"SKF02@MTHPRODLOC"

These functions are very flexible as they can be nested in other calculations.

We will use a few of these functions in the exercises throughout this course.

Note

Cumulative multiplication (commonly used for calculating the compound interest, inflationary effect, etc.) can also be modeled in SAP IBP.

A multiplication function is not available directly, but you can calculate a series of value changes defined in percentages across time periods by using the existing SUM(), LOG() and EXP() functions.

Calculation Chain

As previously mentioned, the request level is at the top of the key figure model, and at the bottom are datasources - stored inputs into the calculations.

The calculations defined in the Configuration app are translated into a calculation graph, which consists of different types of nodes and arcs.

The nodes of the calculation graph can have a colored background:

  • No background: calculated node
  • Green background: data source node (stored key figure values)
  • Purple background: calculation in focus

The arcs of the calculation graph have, for example, one of the following colors, depending on the type of the node that the arc arrow points to:

  • Light blue: Aggregation
  • Medium blue: Simplified key figure calculations

    Note

    Later in the course, we will be working with some of these calculations.

  • Orange: Inner join

The Key Figure Calculations app allows for the visualization of calculation graphs, root attributes, filter blocks, and where-used graphs.

Calculation chains can get quite complex, depending on the requirements. If the chain grows to 15-20 levels deep, there will likely be impacts on performance (including performance in the Excel UI), and it will be very challenging to find and correct errors.

It is a good practice to check the calculation chain and make decisions about breaking the calculations or attempting simplification.

Simulate Key Figure Calculations App

You can use the Simulate Key Figure Calculations app to simulate and validate changes made to the calculation definitions of key figures without activating the planning area first.

The use case can be for working in a bigger team, where you do not need to wait for other configurators to activate the model if you want to check the performance of the configuration element you were working on.

Note

Currently, the app works with templates in SAP IBP, add-in for Microsoft Excel.

Using the app, you can create and run simulations to check the correctness of each calculation step in the graph, from the level of stored input to calculations at REQUEST level, including interim calculation steps. The simulation details will render in table form and can be exported to Microsoft Excel.

To run a simulation, choose Create, select the planning area, the planning view template or favorite, the worksheet, and the key figure for which you want to run a simulation. You can simulate your user or another user. If you simulate another (business) user, planning view templates and favorites are filtered based on the permission filters of the business user when running the simulation. You can also give a name to the simulation.

To make the simulation more specific, you can define the following settings as well:

  • The time period from which you want to read stored input
  • Planning filter for attributes
  • Scope of the simulation: you can run it for the complete graph or selected nodes only
  • Number of days for which you want to store the simulation in the system

Filter Blocks

SAP Integrated Business Planning for Supply Chain usually works with huge data volumes. It is not uncommon, for example, to see master data types that have several million data records. To manage performance, queries are typically filtered.

You can use filters in the SAP Integrated Business Planning, add-in for Microsoft Excel (attribute-based or value-based filters) or in the Planning Filters app.

When filters are used, all attributes are filtered as early in the calculation chain as possible, ideally on the level of stored key figures.

Some modeling techniques prevent filtering on the level of stored key figures by imposing filter blocks for certain attributes in certain calculations. A filter block is required for these calculations so that they provide correct results at time attribute transformation, at master data transformation and at cross-period calculations.

As explained in the previous section, filter blocks are displayed in the calculation graphs, allowing you to get a better understanding of how to filter more effectively and how to improve the performance of queries.

Select a planning area and a key figure, then choose Go to display the calculation graph.

Choose the Filter Blocks → Show All Attributes tab to display attributes where filter blocks are raised, as well as attributes where filtering is possible.

To learn about the details and causes of the filter blocks, select an attribute for which a filter block exists or display the node info.

Overview how filter block can be visualized in the SAP IBP Fiori UI.

How to Write a Key Figure Calculation

Write a Key Figure Calculation

Business Example

You are a member of the project team managing the implementation of SAP Integrated Business Planning for Supply Chain solution. You want to set up the business model objects in the SAP Integrated Business Planning system. Based on your blueprint, you now set up your key figures. Once you have created a key figure, you can add calculations to it at request level where the calculation uses the SUM operator from the lower base planning level.

When the data values for the exercise include ##, replace ## with the group number that the instructor has provided to you.

In this exercise, you write a key figure calculation that is required for one or more subsequent exercises in the course.

This exercise requires you to use the key figure that you created as a key figure in a previous exercise.

Steps

  1. Use the Inputs dialog box to check the definitions used in the calculation for the ACTUALSQTY key figure.

    1. Choose Planning Areas Model Configuration tile.

    2. Choose the T## Planning area and go to Key Figures tab.

    3. Select the Actuals Qty key figure.

      Result

      The ACTUALSQTY screen appears.
    4. Select Edit.

    5. In the Calculation Definitions screen area, check that the calculation displays as the following default:

      ACTUALSQTY@REQUEST = SUM("ACTUALSQTY@MTHPRODCUST")

      Note

      If there is no defined calculation, choose Add Calculation Definition, and enter the calculation shown.
    6. Choose Inputs.

      Result

      The Inputs dialog box appears.
    7. Select Stored in the Value Used for the ACTUALSQTY@MTHPRODCUST key figure, and choose OK.

    8. Choose Save.

  2. Create a request level calculation for T##UNITPRICE so that it calculates a weighted average.

    1. Choose the T##UNITPRICE key figure.

      Result

      The T##UNITPRICE screen appears.
    2. Select Edit.

    3. Choose Edit in the Calculation Definitions.

      Result

      A new Calculation Definitions screen area appears with the following details: T##UNITPRICE@Request equals SUM("T##UNITPRICE@MTHPRODCUST").
    4. In the equation area, enter the following calculation: IF(ISNULL("CONSENSUSDEMANDQTY@REQUEST") OR "CONSENSUSDEMANDQTY@REQUEST" = 0 , 0, "CONSENSUSDEMANDREV@REQUEST"/"CONSENSUSDEMANDQTY@REQUEST")

      Result

      The calculation checks if CONSENSUSDEMANDQTY is 0 or empty. If it is, then zero is assigned as the value. Otherwise, it calculates the weighted average.
    5. Choose Validate.

      The calculation expression has no errors.

    6. Using the Inputs tab, ensure that the CONSENSUSDEMANDQTY key figure appears with the Calculated in the Value Used box.

    7. Choose OK.

    8. Ensure that in the General SettingsT##UNITPRICE is marked as Stored and Calculated.

    9. Choose Save.

  3. Be aware that CONSENSUSDEMANDREV is a calculated key figure and is not stored, and define the calculation at the base planning level and the request level. Define the calculations for CONSENSUSDEMANDREV so that it is calculated at the base planning level as unit price * Qty and the aggregate level calculation is the sum of the base planning level.

    1. Choose the CONSENSUSDEMANDREV key figure.

      Result

      The CONSENSUSDEMANDREV screen appears.
    2. In the Calculation Definitions screen area, check that the calculation displays as the following default: CONSENSUSDEMANDREV@REQUEST = SUM("CONSENSUSDEMANDREV@MTHPRODCUST")

    3. Choose Edit and then Edit in the Calculation Definitions

    4. Choose Inputs.

      Result

      The Direct Inputs dialog box appears.
    5. Ensure that you do not select the Stored Value, confirm the key figure details CONSENSUSDEMANDREV@MTHPRODCUST, and choose OK.

    6. Choose Add Calculation Definition.

      Result

      A new Calculation Definition screen area appears with the following details: CONSENSUSDEMANDREV@.
    7. Select MTHPRODCUST.

    8. In the equation box, enter the following calculation: "T##UNITPRICE@MTHPRODCUST" * "CONSENSUSDEMANDQTY@MTHPRODCUST"

    9. Choose Validate .

    10. Select Inputs.

    11. Ensure that the T##UNITPRICE@MTHPRODCUST is selected as stored and that CONSENSUSDEMANDQTY@MTHPRODCUST is not selected as stored.

    12. Choose Save.

      Result

      Remain on the Configuration screen.

Log in to track your progress & complete quizzes