Mykyta  Stelmaschenko
Mykyta Stelmaschenko

Reputation: 121

Data didn't show in the first row and start to calculate from second (ABAP,BW)

Can you help me with ABAP code (I make some simple logic for BW).

When data is appended to the table, it calculates the first row, but writes the result in the second, second to third, etc. And on the first row I have zero result.

What can it be? Can you help me, please, maybe I lost something in my code.

Picture with my problem

My code:

      FIELD-SYMBOLS:   <ls_data>       TYPE any,
                 <lv_plant>     TYPE /BI0/OIPLANT.   
      TYPES: begin of    ty_kpi_result,
         plant           type  /BI0/OIPLANT,
         mat_num_01(18)  type  n,
         mat_num_02(18)  type  n,    
    END OF ty_kpi_result.


    DATA: lt_result        TYPE TABLE OF ty_kpi_result,
        ls_result        like LINE OF lt_result,
        lv_count         type i,
        lv_plant_1       type /BI0/OIPLANT.   
    sort ct_data ASCENDING.  
    LOOP AT ct_data ASSIGNING <ls_data>.
    ASSIGN COMPONENT 'PLANT' OF STRUCTURE <ls_data> to <lv_plant>.
     * lv_count             = lv_count + 1.
      ls_result-plant      = <lv_plant>.
    IF lv_plant_1        = <lv_plant>.
       lv_count             = lv_count + 1.
        ls_result-mat_num_01 = lv_count.
    ELSE.
      lv_count = 0.
      lv_count = lv_count + 1.


    APPEND ls_result to lt_result.
    ENDIF.

    lv_plant_1 = <lv_plant>.
    ls_result-mat_num_01 = lv_count.  
 ENDLOOP.

Upvotes: 2

Views: 189

Answers (1)

Mykyta  Stelmaschenko
Mykyta Stelmaschenko

Reputation: 121

Found solution on SAP forum:

FIELD-SYMBOLS: <ls_data>        TYPE any,
                 <lv_plant>     TYPE /bi0/oiplant.
TYPES: BEGIN OF ty_kpi_result,
     plant           TYPE  /bi0/oiplant,
     mat_num_01      TYPE  i,
     mat_num_02      TYPE  i,
END OF ty_kpi_result.


DATA: lt_result        TYPE sorted TABLE OF ty_kpi_result with UNIQUE KEY  plant,
    ls_result        LIKE LINE OF lt_result,
    lv_count         TYPE i,
    lv_plant_1       TYPE /bi0/oiplant.

SORT ct_data BY ('PLANT').
CLEAR ls_result.

LOOP AT ct_data ASSIGNING <ls_data>.

ASSIGN COMPONENT 'PLANT' OF STRUCTURE <ls_data> TO <lv_plant>.

IF  <lv_plant> EQ ls_result-plant.

ADD 1 TO ls_result-mat_num_01.

 ELSE.
IF ls_result-plant IS NOT INITIAL. " first record
  APPEND ls_result TO lt_result.
ENDIF.
CLEAR ls_result.
ls_result-plant = <lv_plant>.
ls_result-mat_num_01 = 1.
 ENDIF.

AT LAST. " last record

APPEND ls_result TO lt_result.

ENDAT.
 ENDLOOP.
  ENDMETHOD.

Upvotes: 2

Related Questions