Khnc Fox Tips Tricks[1]
Transcript of Khnc Fox Tips Tricks[1]
-
7/28/2019 Khnc Fox Tips Tricks[1]
1/43
Marc F. BernardPlatinum ConsultantSAP NetWeaver RIG Americas
SAP Labs, LLC
Thursday, March 10, 2005
SAP NetWeaver Know-How Network Conference Call
Tips & Tricks forFOX Formulas in BW-BPS
-
7/28/2019 Khnc Fox Tips Tricks[1]
2/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /2
Learning Objectives
Determine the correct design for a planning function
Understand how BPS formulas work
Design formulas the right way
Resolve performance problems or avoid them in the first place
-
7/28/2019 Khnc Fox Tips Tricks[1]
3/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /3
How to Design a Planning Function
The following holds for EVERY planning function:
Define the business scenario for the planning function.
Determine the proper level i.e. the level of aggregation in the
InfoCube that is needed for the business scenario. You have toidentify the proper characteristics and key figures.
Write down some sample records in that level.
Write down how the data records should look like afterexecuting the planning function.
Identify the fields in the data records that are changed or usedfor calculation by the planning function. These are the fields
to be changed in the planning function.
Identify the type of planning function (predefined type, formula(FOX), exit) and configure the planning function.
-
7/28/2019 Khnc Fox Tips Tricks[1]
4/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /4
Example for a Planning Function
Business scenario: You want to copy the amount for eachproduct from the current year to the next year.
Level: We use an InfoCube with 0PRODUCT and 0FISCYEAR as
characteristics and 0AMOUNT as key figure.
2002010000210020100001
0AMOUNT0FISCYEAR0PRODUCT
20020110002
20020100002
1002011000110020100001
0AMOUNT0FISCYEAR0PRODUCT
Field(s) to be changed
Before
After
-
7/28/2019 Khnc Fox Tips Tricks[1]
5/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /5
Planning Functions Subsets: Example
Sample data
records:
20020110002
20020100002
10020110001
10020100001
0AMOUNT0FISCYEAR0PRODUCT
Empty
Year
Product
All
Fields for grouping
2 calls,records (1+3) and (2+4)
Product
1 call,all records
All
2 calls,
records (1+2) and (3+4)
Year
4 calls,one record per call
Empty
Number of calls/recordsFields to be changed
-
7/28/2019 Khnc Fox Tips Tricks[1]
6/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /6
Reference Data
Flowchart for Planning Functions
Start of Execution
End of Execution
Data Selection
Execute Function
Build Subsets from Selected Data
Initialize Function (Add Subsets)
FOREACH Subset.
ENDFOR.
Phase 1
Phase 2
-
7/28/2019 Khnc Fox Tips Tricks[1]
7/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /7
Types of Generic Functions
Copy Copy
Copy to Several Target Objects
Delete Delete Based on Values
Delete Invalid Combinations
Revaluation
Repost Repost Based on Values
Repost Characteristic Relationships
Distribute By Key
By Key from Sender to Receiver By Reference Data
By Reference Data from Sender to
Receiver
Currency Translation
Unit of Measure Conversion
Forecast
Formulas (FOX)
Exit Function
BW-BPS provides the following generic planning functions:
-
7/28/2019 Khnc Fox Tips Tricks[1]
8/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /8
Complexity vs. Functionality
PredefinedFunctions
Formulas (FOX) Exits
Complexity, Functionality
-
7/28/2019 Khnc Fox Tips Tricks[1]
9/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /9
When to Use Formulas (FOX)
There is no predefined planning function that does the job.
The task cannot be done in one standard planning function butseveral planning functions/sequence are needed.
The customizing of a standard planning function becomes tocomplicated.
Performance:
Most of the time it is faster to have one fox formula doing thejob than a number of planning functions.
-
7/28/2019 Khnc Fox Tips Tricks[1]
10/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /10
How to Implement Formulas (FOX)
-
7/28/2019 Khnc Fox Tips Tricks[1]
11/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /11
FOX Formulas Definition
Definition:
Formulas define how transaction data is to be processed in orderto generate plan data. Formula functions enable you to use
extended mathematical functions to calculate plan data.
In addition to different calculation functions, which you can usefor value assignment in formulas, there is also the possibility tomodel complex flow structures with the formula language FOX
(FOrmula eXtensions).
-
7/28/2019 Khnc Fox Tips Tricks[1]
12/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /12
FOX Formulas Basics
How to specify a record in a formula:
You use { }-operands to access transaction data. Syntax:{field to be changed 1, field to be changed 2, , field t.b.c. n}
If "key figure name" is a field to be changed, then each key figure in
a record can be addressed individually. If "key figure name" is not in the fields to be changed, then all key
figures of the data record will be changed according to the formula.
Use F4-help for entering { }-operands.
How to read and write data:
To read data, put the { }-operand on the right hand side of equations.
To write data, put the { }-operand on the left hand side of equations.
Do not forget the "." at the end of each statement.
-
7/28/2019 Khnc Fox Tips Tricks[1]
13/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /13
FOX Formulas Help (F1 and F4)
-
7/28/2019 Khnc Fox Tips Tricks[1]
14/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /14
FOX Formulas Basic Examples
Example 1:
Calculate sales amount from sales quantity and a fixed price
Field(s) to be changed: Key figure name
Formula: {0AMOUNT} = {0QUANTITY} * 100. Note: If key figure is the only field to be changed, you do not need
the { } brackets: 0AMOUNT = 0QUANTITY * 100.
Example 2:
For each product copy data from the current year (2005) to thenext year (2006)
Field(s) to be changed: Fiscal Year Formula: {2006} = {2005}.
Note: You do not have to care about the product because of thesubsets ("automatic FOREACH")!
-
7/28/2019 Khnc Fox Tips Tricks[1]
15/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /15
Some Elements of FOX Formulas
* Comments start in the first column of a row andonly have a length of a row
Loop Constructs
DO ENDDO.
Needs explicit EXIT Condition
FOREACH ENDFOR.
Ending, if last selected characteristic value or key figureis reached
Conditional Statements
IF ELSEIF ENDIF. ELSEIF if you want to integrate
several alternatives
IF ELSE ENDIF. ELSE if no condition mentioned before works
Using Time Characteristics
TMLV( Characteristic,
Offset )
offset: to add for example one year
you would select 1 as offset
Using Planning Area Variables:
DATA LocalVariable TYPE InfoObject.
LocalVariable = VARV('name of global variable').
Error and Information Messages
MESSAGE Tnnn(class)
T = E (Error) or I (Information) nnn = Number of message in message class class = Customer defined message class
Working with Local Variables:
DATA varname TYPE typename.
typename I (Integer) or F (Floating Point) or name of an InfoObject
All elements are
explained indocumentation
-
7/28/2019 Khnc Fox Tips Tricks[1]
16/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /16
FOX Formulas Foreach Statement
You have to define a local variable:
DATA year TYPE 0fiscyear.
Use this variable in the Foreach Statement:
FOREACH year.{year} = {2004} * 1.05.
ENDFOR.
The values for YEAR are taken from the records in the selection ofthe planning package.
Assume we have data records for year 2005 and 2006.
First loop: YEAR is replaced with 2005, system calculates
{2005} = {2004} * 1.05.Second loop: YEAR is replaced with 2006, system calculates
{2006} = {2004} * 1.05.
-
7/28/2019 Khnc Fox Tips Tricks[1]
17/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /17
FOX Formulas Variables
Example of formula using one planning area variable and three localvariables:
DATA ZCURPER TYPE 0FISCPER.
DATA ZFCPER TYPE 0FISCPER.
DATA COUNTER TYPE I.
* Copy current period to the next 5 forecast periods
ZCURPER = VARV('PERCUR').
DO.
COUNTER = COUNTER + 1.
ZFCPER = TMVL(ZCURPER,COUNTER).
{0COPASLQTY,ZFCPER,RFC} = {0COPASLQTY,ZCURPER,ACT}.
IF COUNTER = 5.
EXIT.
ENDIF.
ENDDO.
-
7/28/2019 Khnc Fox Tips Tricks[1]
18/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /18
FOX Formulas How to test
Create small packages (e.g. 2 records).
Create layouts for all packages: Put the fields to be changed inthe key columns, the key figures in the data columns, and the
remaining characteristics in the header. The layout looks like adata record.
Execute the planning function using the trace ("execute withtrace").
Send messages with the relevant values.
Have a look at the details in the system messages.
-
7/28/2019 Khnc Fox Tips Tricks[1]
19/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /19
Execute a Planning Function
-
7/28/2019 Khnc Fox Tips Tricks[1]
20/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /20
Execute a Planning Function with Trace
-
7/28/2019 Khnc Fox Tips Tricks[1]
21/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /21
FOX Formulas How to Optimize
The idea is to reduce the complexity of the formula, withoutchanging the result of the calculation.
Less FOX coding leads to less ABAP coding that the system has
to process and therefore faster execution times.
There are two main targets for optimization:
1. Formula Operands
2. Foreach Loops
-
7/28/2019 Khnc Fox Tips Tricks[1]
22/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /22
Real-world Example
Fields to be changed:
{ Key figure, Cost Element, Version, Planning Item, Planning Area }
Parameter group:
* Cash Discount = (Base Revenue - Price Adjustment) * 2%
DATA C TYPE 0COSTELMNT.
DATA V TYPE 0VERSION.
DATA P TYPE ZPLANITEM.
FOREACH C,V,P.
{0AMOUNT,0000415001,V,CASHDISC,PA000004} =
( {0AMOUNT,0000400001,V,CUSTSALES,PA000004} -
{0AMOUNT,0000410001,V,PRICADJDIS,PA000004} +
{0AMOUNT,0000405002,V,PRICADJBKT,PA000004} ) * 0.02.
ENDFOR.
-
7/28/2019 Khnc Fox Tips Tricks[1]
23/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /23
Optimization 1: "Fields to be changed"
Find all characteristics that are restricted to only one value in theformula. This means that the characteristic is NOT being changedand therefore should be removed from the "fields to be changed".
-
7/28/2019 Khnc Fox Tips Tricks[1]
24/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /24
Optimization 1: Example
Optimization 1: "Fields to be changed" that are not changed
Remove from "Fields to be changed": Key figure = 0AMOUNT
Remove from "Fields to be changed": Version = V
Remove from "Fields to be changed": Planning Area = 'PA000004'
Fields to be changed:
{ Cost Element, Planning Item }
Parameter group:
* Cash Discount = (Base Revenue - Price Adjustment) * 2%
DATA C TYPE 0COSTELMNT.
DATA P TYPE ZPLANITEM.
FOREACH C,P.
{0000415001,CASHDISC} =
( {0000400001,CUSTSALES} -
{0000410001,PRICADJDIS} +
{0000405002,PRICADJBKT} ) * 0.02.
ENDFOR.
-
7/28/2019 Khnc Fox Tips Tricks[1]
25/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /25
Optimization 2: "Formula selection"
Determine the selections i.e. the characteristic combinations thatare changed by the formula and compare it against the planninglevel.
There can be several cases: Perfect match (best case)
Level selection is equal to the formula selection. The system willread only data that is actually being changed.
Level selection is bigger (worst case)
The system will read more data than required by the function. Level selection is smaller (error)
The formula potentially generates records that are not part of thepackage, which could lead to an error message.
-
7/28/2019 Khnc Fox Tips Tricks[1]
26/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /26
Optimization 2: Example
Optimization 2 A: "Formula selections (excl. reference data)" that are different thanlevel
Restrict level to formula selection: Key figure
Level:
= 0AMOUNT, ZPRICEADJ, ZUNCOLLAR
Formula:
= 0AMOUNT
Restrict level to formula selection: Cost Element
Level:
No restriction
Formula:
= '0000415001'
Optimization 2 B: "Formula selections (incl. reference data)" that are different thanlevel
Optimization works same way as 2A but tries to combine two database selections.
Level contains more keyfigures than necessary
Level is not restricted on CostElement but formula changes
only one Cost Element
-
7/28/2019 Khnc Fox Tips Tricks[1]
27/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /27
Optimization 3: "Conditions"
Optimization 3 A:
If the condition is equal to the level restriction, then the conditioncan be removed.
Optimization 3 B:
If there's only one condition it can be merged with the level
selection.
-
7/28/2019 Khnc Fox Tips Tricks[1]
28/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /28
Optimization 4: "Formula operands"
It is not necessary to write to the same result operand severaltimes. It's also not necessary to read the same reference operandseveral times. Instead, the operands should be stored temporarilyusing local variables (DATA).
Optimization 4 A:
Based on result data onlyOptimization 4 B:
Based on reference data only
-
7/28/2019 Khnc Fox Tips Tricks[1]
29/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /29
Optimization 4: Example
Fields to be changed:
{ Material, Planning Item }
Parameter group (BEFORE):
DATA M TYPE 0MATERIAL.
{#,TOTALSALES} = 0.
FOREACH M.
{#,TOTALSALES} = {#,TOTALSALES} + {M,CUSTSALES}.
ENDFOR.
Parameter group (AFTER):
DATA M TYPE 0MATERIAL.
DATA TOTAL TYPE F.
TOTAL = 0.
FOREACH M.
TOTAL = TOTAL + {M,CUSTSALES}.
ENDFOR.
{#,TOTALSALES} = TOTAL.
If there are 1.000 materials,then TOTALSALES has to be
read and updated 1.000 times
There's only one update ofTOTALSALES independed of
the number of materials
-
7/28/2019 Khnc Fox Tips Tricks[1]
30/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /30
Optimization 5: "Operands in Foreach Loops"
If an operand is used in an foreach loop but does not depend onany foreach variable, then the operand is being processed toooften and therefore should be moved before (reference data) orafter (result data) the foreach loop.
Optimization 5 A:
Based on result data onlyOptimization 5 B:
Based on reference data only
-
7/28/2019 Khnc Fox Tips Tricks[1]
31/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /31
Optimization 5: Example
Fields to be changed:
{ Cost Element, Planning Item }
Parameter group (BEFORE):
* Cash Discount = (Base Revenue - Price Adjustment) * 2%
DATA C TYPE 0COSTELMNT.
DATA P TYPE ZPLANITEM.
FOREACH C,P.
{0000415001,CASHDISC} =
( {0000400001,CUSTSALES} -
{0000410001,PRICADJDIS} +
{0000405002,PRICADJBKT} ) * 0.02.
ENDFOR.
Parameter group (AFTER):
{0000415001,CASHDISC} =
( {0000400001,CUSTSALES} -
{0000410001,PRICADJDIS} +
{0000405002,PRICADJBKT} ) * 0.02.
CASHDISC is updated insidethe loop although it does not
depend on C or P.
The other operands are read
inside the loop although theydon't depend on C or P.
Ultimately the completeForeach loop can be
removed.
-
7/28/2019 Khnc Fox Tips Tricks[1]
32/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /32
Optimization 6: "Formulas on Multi Planning Areas"
If the formulas is using data of only one planning area but isdefined on a multi area, then the complete formula should bemoved to the single basis planning area.
-
7/28/2019 Khnc Fox Tips Tricks[1]
33/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /33
Optimization 7: "Nested Loops"
Use nested FOREACH statements only if necessary.
Use FOREACH VAR1,VAR2, ..., VARn wherever it is possible.
Example: Records
2011
2010
0FISCYEAR
20
10
0AMOUNT
0002
0001
0PRODUCT
2011
2010
0FISCYEAR
200002
100001
0AMOUNT0PRODUCT
2011
2010
20112010
0FISCYEAR
20
0
010
0AMOUNT
0002
0002
00010001
0PRODUCT
FOREACH product.
FOREACH year.
...
ENDFOR.
ENDFOR.
FOREACH product, year.
ENDFOR.
-
7/28/2019 Khnc Fox Tips Tricks[1]
34/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /34
Optimization 8: "If Statements"
If Statements versus Conditions - when to use which: When the logic depends on variables with ranges, several values or
hierarchy nodes (defined on the planning area), then use conditions.
When using if-statements you only have one parameter group and
thus only one formula. Therefore the formula is easier to understand.
9 C
-
7/28/2019 Khnc Fox Tips Tricks[1]
35/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /35
Tip 9: "Naming Conventions"
Use naming conventions for local FOX variables, e.g.: CHA_ for characteristics,
KYF_ for key figure values,
VAR_ for global (BPS) variables,
ATR_... for attributes,
INT_ for integer numbers,
DAT_... for dates,
TIM_... for times.
Add comments to the formula.
Ti 9 E l
-
7/28/2019 Khnc Fox Tips Tricks[1]
36/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /36
Tip 9: Example
Fields to be changed:
{ Key figure name, Posting period, Version }
* Posting period and total amount
DATA CHA_0FISCPER3 TYPE 0FISCPER3.
DATA KYF_TOTAL TYPE F.
* BPS version (via variable)
DATA VAR_ZBPSVER TYPE 0VERSION.
* Actual period (attribute of BPS version)
DATA ATR_ZACTPER TYPE 0FISCPER3.
* Get actual period from BPS version (via variable)
VAR_ZBPSVER = VARV( 'ZBPSVER' ).
ATR_ZACTPER = ATRV( 'ZACTPER', VAR_ZBPSVER ).
* Get total to be distributed greater than actual period
KYF_TOTAL = 0.
FOREACH CHA_0FISCPER3.
IF CHA_0FISCPER3 > ATR_ZACTPER.
KYF_TOTAL = KYF_TOTAL + {0AMOUNT,CHA_0FISCPER3,VAR_ZBPSVER}.
ENDIF.
ENDFOR.
Ti 10 "R f D t "
-
7/28/2019 Khnc Fox Tips Tricks[1]
37/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /37
Tip 10: "Reference Data"
If you set a local variable for a characteristic using TMVL, ATRV,or ATRVT and use this variable in a reference data operand (rightside of formula), then the system ignores any restrictions for thischaracteristic when reading the reference data form the database.
This can lead to major performance problems! Check the BPSstatistics for very long database selection for reference data.
Check that SAP note 729362 is implemented (or BW 3.5 SP 2).
However, there's no easy workaround.
Tip 10: Example
-
7/28/2019 Khnc Fox Tips Tricks[1]
38/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /38
Tip 10: Example
Fields to be changed:
{ Company, Version }
* Company
DATA CHA_0COMPANY TYPE 0COMPANY.
* Source version (attribute of company)
DATA ATR_SVERSION TYPE SVERSION.
* Target version (variable)
DATA VAR_TVERSION TYPE 0VERSION.
* Get target version from variable
VAR_TVERSION = VARV( 'TVERSION' ).
* Copy data from source to target version
FOREACH CHA_0COMPANY.
ATR_SVERSION = ATRV( 'SVERSION', CHA_0COMPANY ).
{CHA_0COMPANY,VAR_TVERSION} = {CHA_0COMPANY,ATR_SVERSION} .
ENDFOR.
Business Case: Copy datainto a target version and each
company potentially has a
different source version.
Source version is set via attribute
(ATRV) and then used in operandto read reference data. System will read ALL
versions since it can't knowwhich ones would be required!
Tip 11: "Endless Loops"
-
7/28/2019 Khnc Fox Tips Tricks[1]
39/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /39
Tip 11: Endless Loops
Save the formula before testing! Otherwise you loose your work ifthe program goes into an endless loop and you have to stop theprogram.
To stop a session after having programmed an "endless loop" (forexample a do loop without exit statement), select "Stop Session"from the SAPGUI system menu or go to transaction SM50 andchoose Program/Mode Program Cancel.
Tip 12: "Performance Analysis"
-
7/28/2019 Khnc Fox Tips Tricks[1]
40/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /40
Tip 12: Performance Analysis
Always do a performance analysis before you start optimizing.
Use various statistics to determine the cause of performance
issues: BPS Statistics (transaction BPS_STAT0)
BW Statistics (table RSDDSTAT)
SQL Trace (transaction ST05)
ABAP Performance Trace (transaction SE30)
Workload Statistic (transaction STAD)
Key Learnings
-
7/28/2019 Khnc Fox Tips Tricks[1]
41/43
SAP AG 2005, Tips & Tricks for FOX Formulas in BW-BPS, Marc F. Bernard /41
Key Learnings
Follow the steps on "how to design a planning function"
Use subsets to your advantage
Remember the tips for implementing and optimizing FOX
formulas
Always determine the root cause first before starting tooptimize
-
7/28/2019 Khnc Fox Tips Tricks[1]
42/43
-
7/28/2019 Khnc Fox Tips Tricks[1]
43/43