cancel
Showing results for 
Search instead for 
Did you mean: 

FOX FOREACH loop problem

Former Member
0 Kudos

I am having strange problems with the FOX FOREACH routine.

The code is below and as you can see very straigtfoward.

For each iteration it actually executing the whole script rather than just the code between FOREACH... ENDFOR!

Code below. Any assistance appreciated.

Alex

DATA CURRPER TYPE 0FISCPER.

DATA FISCPER TYPE 0FISCPER.

DATA SUMPLAN TYPE F.

DATA SUMACTUAL TYPE F.

  • Periods read from the variables with technical name PERIOD

CURRPER = VARV( 'ZPERIOD' ).

MESSAGE I001(CURRENTPERIOD) WITH CURRPER.

  • Total Planned Amount

FOREACH FISCPER.

SUMPLAN = SUMPLAN + { FISCPER, BAS, 020, C565 }.

ENDFOR.

  • Total Actuals (***change to date by variable)

FOREACH FISCPER.

  • IF FISCPER > CURRPER.

SUMACTUAL = SUMACTUAL + { FISCPER, 000, 010, C565

}.

  • ENDIF.

ENDFOR.

{ FISCPER, NW, 020, C565 } = SUMACTUAL.

ENDIF.

ENDFOR.

Message was edited by: Alex Munroe

Accepted Solutions (1)

Accepted Solutions (1)

former_member93896
Active Contributor
0 Kudos

Alex,

please check the formula you posted here. It is syntactically not correct since there's an ENDIF and ENDFOR at the end (IF and FOREACH are missing).

In any case the (complete) formula will be executed several times depending on the number for characteristic combinations that are generated from the transaction data. Execute the function with trace and you can see all the combinations.

Regards

Marc

SAP NetWeaver RIG

Former Member
0 Kudos

Marc - thanks for the prompt response.

Leaving aside the typo's I find that the trace does not identify the cause. I have included a very example below.

What I want is for all the Key Figure 2 values to be assigned the sum of the values for Key Figure 1. Instead what I find is that values for each fiscal period are being copied from KF1 to KF2 for the same fiscal period. It's as if the code is running the whole program by

fiscal period rather than just each FOREACH loop.

Data example:

original data:

KF1 KF2

========

10 0

10 0

10 0

current data result:

KF1 KF2

========

10 10

10 10

10 10

expected result:

KF1 KF2

========

10 30

10 30

10 30

Any thoughts on causes?

regards.

Alex

======

DATA FISCPER TYPE 0FISCPER.

DATA SUM TYPE F.

FOREACH FISCPER.

  • KF 1

SUM = { FISCPER,BAS,020,C600 } + SUM.

ENDFOR.

FOREACH FISCPER.

  • KF 2

{ FISCPER,NW,020,C600 } = SUM.

ENDFOR.

Message was edited by: Alex Munroe

Former Member
0 Kudos

Hi,

As Marc mentioned, depending on the charectaristics combination, the formula will be executed. Each execution of the formula is processing one record (or one combination set of charecteristics), and doesn't have access to other KF values for a different characteristics combination. This is expected (though not what you want).

cheers,

Former Member
0 Kudos

I have basically taken the SAP delivered example below and have modified it for my own requirements. In the code below a key figure represented by a combination of characteristics { 1, FISCPER }is being summed for re-use elsewhere in the code which is exactly what I want to do.

I can't see what needs to be done for the program to work as it intuitively should.

=========

Rolling Planning (SAP EXAMPLE):

Characterisics to be changed are version and fiscal year/period. The actual data of the current period is copied into the plan version. The difference is distributed over the remaining periods. The current period is determined from a variable.

DATA CURRPER TYPE FISCPER.

DATA FISCPER TYPE FISCPER.

DATA SUM TYPE F.

DATA DELTA TYPE F.

  • Periods read from the variables with the technical name PERIOD

CURRPER = VARV( 'PERIOD' ).

  • Get total on weights

FOREACH FISCPER.

IF FISCPER > CURRPER.

SUM = SUM + { 1, FISCPER }.

ENDIF.

ENDFOR.

  • Determine delta between plan value and actual value

DELTA = {1, CURRPER} - {0,CURRPER}.

  • Set plan value to actual values

{1,ACTPER} = {0, ACTPER}.

  • Distribute delta weighted

FOREACH FISCPER.

IF FISCPER > CURRPER.

{1,FISCPER} = {1,FISCPER} + DELTA * {1,FISCPER} / SUM.

ENDIF.

ENDFOR.

Former Member
0 Kudos

I tested same (similar) code and it seems to work fine. It loops through the records on each FISCPER for the FOREACH loop as you would expect. (I am on SEM4, BW 3.5).

From the code mentioned, it seems to be doing everything right. And the test data, I assume, is there (alongwith the variable values), in which case it should have worked ok.

cheers,

former_member93896
Active Contributor
0 Kudos

Alex,

in the example you gave key figure is NOT a field to tbe changed. Therefore the logic is applied to all key figures.

FOREACH FISCPER.
* KF 1
SUM = { KF1, FISCPER,BAS,020,C600 } + SUM.
ENDFOR.

FOREACH FISCPER.
* KF 2
{ KF2, FISCPER,NW,020,C600 } = SUM.
ENDFOR.

Regards

Marc

SAP NetWeaver RIG

Former Member
0 Kudos

Thanks Ajay for that. However no joy on my side.

Former Member
0 Kudos

Marc,

I realise that Key figure is not a field to be changed and hence the logic is applied to all key figures. However the transactional data required is contained/modelled within a single Key Figure (0AMOUNT) with the values being differentiated by the value types and versions. In this instance the fields to be changed are:

{Fiscal year/period,Version,Value type,Project}

However I still am perplexed as to why the whole FOX seems to be repeated using each record a dataset (so the whole formula seems to be repeated rather than just the code within the FOREACH formula) rather procedurally executing the code and completing each FOREACH loop in sequence.

Appreciate the assistance

cheers,

Alex

Former Member
0 Kudos

Is there any other characteristic not included in the 'fields to be changed' which have unique value for each record?

Former Member
0 Kudos

No. The records are key'ed with current change fields.

Alex

Answers (0)