# Building Advanced Formula Data Actions  Objectives
After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

• Build advanced formula data actions

## Advanced Formula Data Actions Introduction

Planning requires various types of calculations. To perform these calculations, you can use advanced data actions (formulas).

Here are some key points regarding advanced formulas:

• Use Cases:

• Advanced formulas provide a mechanism for custom calculation logic
• Driver based formulas: Rev = Price x Qty
• Depreciation calculation
• Populate opening balances for headcounts
• Mark-ups | Mark-downs
• Schedule or run on an ad-hoc basis
• Maintenance:

• The script editor provides context help and code completion
• Validation and trace mode in the script editor
• Features:

• The visual editor provides a graphical wizard to define calculation logic
• The statements are represented by boxes, which can be further configured with special dialogs
• The graphical representations give users a clear logic flow  Note
Advanced data actions are used to perform calculations that are stored in the database. If there's a need to perform calculations that aren't stored in the database, there are several options:
• Data table formulas
• Story-based calculated measures
• Model-based calculated measures

### Create Calculations with the Visual Editor

Use the visual tool to drag and drop graphic elements in a flow diagram to create advanced formulas. The general process flow for creating advanced formulas is:

1. Specify the context
2. Define and use a variable if needed
3. Set the configuration options: Calendar, Signflip, Unbooked
4. Formula options:
• Calculation
• Repeat
• Condition
• Delete • Advanced formula actions allow you to design formulas for calculations to apply to the source data. The results are written to defined scopes in the planning model.
• Unlike dimension formulas, which perform the calculation in real-time "on the fly", the advanced formulas data action stores the result of the calculation in the SAP Analytics Cloud database.
• And just as with dimension formulas, parameters can be used in the advanced formula data action.

### Recent Enhancements for Advanced Data Actions

• Support AND/OR for IF condition: Complex and detailed condition expressions are possible with shorter scripts which means better execution performance time as well as better readability.
• Keyword "BREAK" that stops a loop if a specific condition is fulfilled: Support an immediate stop of FOREACH loop with IF condition or others. For example, stopping the depreciation loop with date dimension is possible if the residual book value reaches zero value or iteration number reaches and the same as the useful life. You don't need to loop for an entire date scope resulting in better execution performance.

• Load pre-aggregated value as calculation scope: Minimize the data set size of calculation scope for non-calculation related dimensions.
• Popup dialog-box that shows the calculation scope: Estimate the calculation scope at design time and optimize the script for better execution performance.

## Visual Editor Compared to the Script Editor

The visual editor can be used for simpler calculations. It generates the script automatically.

The script editor is used for more complex programming.

You can hover on a dimension in the data line to see what members are included in the scope of the Calculation. ### Ways to Design Advanced Formulas

There are two different ways to design advanced formulas – using the visual tool and writing scripts.

You can build your own logic in the visual editor and then view the generated code in the script editor. ### Summary: Visual Editor and Script Editor

Visual EditorScript Editor
Target business usersTarget professional modeling users, such as IT specialists
No coding skills requiredRequires basic coding knowledge
Created by dragging and dropping graphic elementsCreated by writing scripts
Can be switched to scriptCan be switched back to visual editor

## Context Membersets

### Use Membersets to Restrict the Data Region

Membersets are used to restrict the data region for the calculation.

Typical use cases:

• MEMBERSET [d/ORGANIZATION].[p/REGION] ="ASIA": Set the scope of the Organization dimension's Region property to ASIA.
• MEMBERSET [d/FLOW] = ("F_TEMP", "F_NONE"): Set the scope of the Flow dimension to the F_TEMP and F_NONE members.
• MEMBERSET [d/CURRENCY] = [d/ORGANIZATION].[p/CURRENCY]: Set the Currency dimension's scope to the value of the Organization dimension's currency property.
• CONFIG.HIERARCHY = [d/ENTITY].[h/H1] MEMBERSET [d/ENTITY] = BASEMEMBER([d/ENTITY], "World"): Return all base members of the member "World" in hierarchy H1 and set these base members as the member scope of the Entity dimension.
• MEMBERSET [d/DATE] ="202201" TO "202212": Set the scope of the Date dimension from 202201 to 202212.

Memberset is used to determine the context, that is the Point of View or the Scope. In the preceding example, see the explanations for the script:

• `MEMBERSET [d/Date] = (BASEMEMBER([d/Date].[h/YQM], %YearMonth%))`

`BASEMEMBER` refers to leaf members for calendar year in this example.

The time hierarchy is set to Calendar Year, therefore YQM is the hierarchy ID.

% denotes variables: for example `%YearMonth%` refers to the prompt/variable for time.

Result: If you select 2021, the system will include all months of 2021 in the calculation.

• `MEMBERSET [d/Cost_Center] = (BASEMEMBER([d/Cost_Center].[h/H1], "All_Cost_Centers"))`

The H1 hierarchy for the cost center dimension has been selected.

`All_Cost_Centers` is the id of the top node/parent.

• Result, if the user selects hierarchy node Canada, only Canadian cost centers will be included in the calculation.

The following script will only run for PC1 & PC2 (that is, only PC1 & PC2 are scoped). In the preceding example, January data is being copied to February with an uplift:

`DATA([d/Date] = %TargetMonthYr%) = RESULTLOOKUP([d/Date]=PREVIOUS(1)) * %UpLift%`

• In this example, `%TargetMonthYr%` is the receiver or target that is dynamically selected by the planner.
• `RESULTLOOKUP([d/Date]=PREVIOUS(1))` dynamically reads the previous month (or period) as compared to the `%TargetMonthYr%` selection.
• `%UpLift%` is the variable for the uplift provided by the planner. Example: 1.25.

### Specify the Context of the Calculation

Set proper filters that apply to and govern all transformations and calculations in the advanced formulas action.

## Use the Visual Editor to Create an Advanced Formula

You are a member of the planning team at ABC Corporation and you are responsible for creating and configuring data actions.

Before Jen can run the data action to perform the calculation, you must use the visual editor to create a data action that calculates labor. Since labor rates are in the HR model, you will use the link command to look-up the labor rates and calculate labor.

#### What skills will you develop in this practice exercise?

In this practice exercise, you will:

• Create the parameters (prompts)
• Create the advanced formula action for labor
• Run the data action from the story to calculate labor ## Variables

Variables in advanced formulas can be used to store data temporarily while the script is running. This is the traditional concept referred to as a Local Variable. One of the benefits is that the placeholder can be used multiple times without including the entire command line every time. The result is a more concise set of code that runs faster.

### Configuration Options

The set configuration options defines basic settings that apply to all the calculations in the advanced formulas actions:

In the following example of advanced logic virtual variables, you can see that:

• SumOfSales is the temporary account for total sales revenue.
• Since the total sales is over 1000(2700), the system calculates other income of 2700 x .1 for each product. ## Configuration Options

Configuration Options
Fiscal Year: This configuration governs the time hierarchy used in the advanced formulas. This setting affects the result of time/date commands, such as FIRST and LAST. It controls which period is selected.
Account Sign Flip: If this option is turned on, all calculations consider the sign value (debit or credit) of each account according to the account type (LEQ, AST, INC, and EXP).
Unbooked Data: Unbooked data refers to cells with empty values. If turned on, all empty cells in the source data to be copied will be treated as cells with zeroes.

### Advanced Formula - Time Hierarchy

Time can be interpreted as YQM: Year | Quarter | Month or as FYQP: Fiscal Year | Quarter | Period. This configuration governs the time hierarchy of the script and is set to mirror the time setting in the underlying model. For example, if the model has Fiscal Time enabled, then the advanced formula will have Fiscal Year enabled as well.

### Advanced Formula - Sign Flip

In advanced formulas, all data is calculated in absolute value by default, regardless of the account type. This is important when working with the Account dimension, where certain accounts will have positive values while others will have negative values. If you want to calculate with the existing sign-flipping rules of Account dimensions, then enter `CONFIG.FLIPPING_SIGN_ACCORDING_ACCTYPE=ON` at the beginning of your script.

In the following example, the memberset command is selecting data for revenue and cost and then writing the result to retained earnings. If the sign flip setting is ON, the correct value (40) is written to retained earnings. ### Advanced Formula - Unbooked Data

In the following example, January data for profit centers PC1/2/3 is being copied to February. By using the Unbooked setting, the existing 500 value for PC4 can either be retained or set to zero (to match the source). ## Syntax and Auto Complete

### Auto Complete

As you type, a hint list shows all available functions that match the text you've typed. A short help text description is available for each function. You can use Ctrl+Space to choose from a list of values that are valid for that location in the formula, or type [d/ for a list of valid dimensions and properties to use in the formula.

There are also formula templates for common calculations. If you select a formula from the hint list, a template is automatically entered in the formula entry bar.

Examples of how to use auto complete:

• Type M to access the command that start with the letter M.
• Type [d/ to access the model dimensions.
• Press Ctrl+Space to access members and parameters. ### Script Syntax

• [ ]: Dimensions, properties, hierarchies are enclosed in square brackets
• d: Dimension. Example: [d/Cost_Center]
• p: Property. Example: [d/Account].[p/ElimTarget]
• h: Hierarchy. Example: [d/Cost_Center].[h/H1], "All_Cost_Centers"))
• %: Variables. Example: %YearMonth%
• #: Unassigned. Example: since labor hours are not currency related, currency is unassigned:`RESULTLOOKUP([d/Account] = "LHours", [d/Currency] = "#")`
• " ": Double quotes are required when specifying dimension members. Note
In an advanced formulas step, you can repeat a group of statements a specified number of times, using the FOR keyword. Employing a BREAK statement inside a FOR loop will allow you to stop the loop when the given condition is met, offering more flexibility.

For more syntax options, to the reference guide:

## Formula Options

### Formula Options

When you create an advanced formula, you have 5 options.

1. Calculation will create an equation such as `Target = Expression`
• A calculation is divided into two parts: Target and Source:
• Target is the data range to which you plan to write data.
• Source is the data involved in the calculation. 2. Delete will prompt for a filter upon which to identify which cost center to delete, for example. 3. Repeat (loop) will prompt for a dimension to loop over, and then you can add a calculation, for example. 4. Comment creates an input field for explanations. 5. Condition will prompt for a filter upon which to create the if statement, and then you can add a calculation, for example. In the previous examples, you can see how the system starts to create formulas depending on which option you pick. Each one generates code that you can view in the script dialog. In the following example, you can see an example of a Calculate Labor formula in the Visual Editor. ### Editing Options

There are four editing options, when you hover over the formula:

1. Duplicate
2. Annotate
3. Comment out
4. Delete

### Using Scripts to Write Advanced Formulas

When using script to write advanced formulas, keep the following in mind:

1. Each line of instruction inherits the calculation results of previous lines of instructions.
2. Advanced formulas perform all calculations only with leaf-level members that do not have any account formulas defined.
3. In a `RESULTLOOKUP` function, the function considers the most detailed values in the data set, even if the formula does not specify them. For example, consider the following record set:
AccountProductEntityDateValue
QuantitySparkling WaterRegionDec.2018210 (aggregate of all countries)
QuantitySparkling WaterGermanyDec.201850
QuantitySparkling WaterFranceDec.201870
QuantitySparkling WaterUKDec.201890

You write a formula to add 20 to the Quantity Values for December 2018 and write the new values to January 2019:

`MEMBERSET[d/Product]="Sparkling Water" MEMBERSET[d/Account]="Quantity" DATA([d/Date]="Jan.2019")=RESULTLOOKUP([d/Date]="Dec.2018")+20`

The final result will add 20 to each detailed Entity value, not to the aggregated Region value:

AccountProductEntityDateValue
QuantitySparkling WaterRegionJan.2019270 (aggregate of all countries)
QuantitySparkling WaterGermanyJan.201970
QuantitySparkling WaterFranceJan.201990
QuantitySparkling WaterUKJan.2019110
4. A null value is treated as a non-existent value, while 0 is treated as an actual number. If the `RESULTLOOKUP` function returns a null value, advanced formulas do nothing.
5. Advanced formulas basically work with the fact record-set of the model. If you do not specify other dimension information and members via the `MEMBERSET` definition, the advanced formulas will take all members from the dimensions in the model. Note
In an advanced formulas step, you can repeat a group of statements a specified number of times, using the FOR keyword. Employing a BREAK statement inside a FOR loop will allow you to stop the loop when the given condition is met, offering more flexibility.

## Use the Visual and Script Editors to Create the Advanced Formula Action

### Scenario: Calculating Labor and Benefit with an Advanced Data Action

As part of the 2025 Forecast Income Statement for ABC Corporation, Jen must calculate the labor and benefits.

You are a member of the planning team at ABC Corporation and you are responsible for creating and configuring data actions.

Before Jen can run the data action, you must create and test an advanced formula data action using the visual editor as well as the script editor. The benefits rate will be entered when Jen runs the data action.

#### What skills will you develop in this practice exercise?

In this practice exercise, you will:

• Create the advanced formula to calculate benefits
• Run the data action from the story 