Tuesday, March 20, 2012

Transformations in BI


Introduction

Routines are used to define complex transformation rules. In most of the cases data won’t be coming directly in desired form before sending to the target. In some cases output needs to be derived on some incoming data. In such cases we need to go for writing of routines at the transformation level.
There are four types of Routines available

Start Routine
Characteristic or Field Routine
End Routine
Expert Routine

Depending on the requirement either one  or combination of  the above routine types are used. With Expert Routine , you can achieve complicated scenarios and as good as writing a complex Program. This will not associate with any other routine types like Start routine or End routine.
Start Routine:
The start routine is run at the start of the transformation. When you want to control the data like dropping the certain Document types or want to do certain Validation etc for the entire data before start of the Transformation this would be place, Also you can execute any statements from which collect the data required for further process like filling the Characteristic value at row level during Transformation.
Example for this could be:

 METHOD start_routine.
*=== Segments ===

    FIELD-SYMBOLS:
          TYPE _ty_s_SC_1.

    DATA:
      MONITOR_REC     TYPE rstmonitor.

*$*$ begin of routine - insert your code only below this line        *-*
... "insert your code here
*--  fill table "MONITOR" with values of structure "MONITOR_REC"
*-   to make monitor entries
... "to cancel the update process
*    raise exception type CX_RSROUT_ABORT.
delete SOURCE_PACKAGE where ( abgru = '94' ) or ( auart = 'ZDI1'
) or ( auart = 'ZDP1' ) or ( auart = 'ZEM1' )  or  ( auart =
'ZEPL' )
or ( auart = 'ZIRP' ) or ( auart = 'ZLOT' ) or ( auart = 'ZMA1'
) or ( auart = 'ZOS1' )
or ( auart = 'ZPRI' ) or ( auart = 'ZPRS' ) or ( auart = 'ZRA1'
) or ( auart = 'ZRE1' )
or ( auart = 'ZRP1' ) or ( auart = 'ZRS1' ) or ( auart = 'ZRW1'
) or ( auart = 'ZSA1' )
or ( auart = 'ZSQT' ) or ( auart = 'ZWE1' ).
* Get Vendor from Mat Plant not from Material Attributes for Vendor validation
    REFRESH:it_mmat_plant.
    SELECT mat_plant
           plant
           vendor
           FROM /bi0/mmat_plant INTO
           TABLE it_mmat_plant
           FOR ALL ENTRIES IN SOURCE_PACKAGE
           WHERE mat_plant = SOURCE_PACKAGE-matnr
           AND plant = SOURCE_PACKAGE-werks.


*$*$ end of routine - insert your code only before this line         *-*
  ENDMETHOD.                    "start_routine
---------------------------------------------------------------------------------


 
 If you look at the code above , Certain types of Order Types are eliminated from DATA PACKAGE which is inform of STRUCTURE called SOURCE-FILEDS.
Also you can see the data is pooled temporarily from Data Base as work area for further reading for filling Vendor at row level which happens as Characteristic Routine.

Characteristic / Key Figure Routine


The routine is useful whenever you want to fill the column either by FORMULA/ROUTINE/AS MASTER DATA attribute from available source fields.  You can choose one of the option at any point of time.
Type Formula useful whenever you want do some arithmetic calculations, getting system parameters etc without writing code.  Also useful for manipulating key figure values like currency conversion.

Example: The column is filled with first 10 digits of the source value

 








 Type Routine useful whenever there is some kind of validation or reading pooled data from Start Routine or complex scenarios like reading multiple set of data before finalizing the Characteristic value.
Example
Here  Fiscal Quarter is arrived based on the Creation data of the Document.
 
DATA: wf_year TYPE /bi0/oifiscyear,
          wf_buper TYPE poper.

    CALL FUNCTION 'DATE_TO_PERIOD_CONVERT'
      EXPORTING
        i_date               =  SOURCE_FIELDS-erdat
*   I_MONMIT             = 00
        i_periv              =  SOURCE_FIELDS-periv
     IMPORTING
       e_buper              = wf_buper
       e_gjahr              = wf_year
     EXCEPTIONS
       input_false          = 1
       t009_notfound        = 2
       t009b_notfound       = 3
       OTHERS               = 4
              .
    IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
* get Fisc quarter based on Wf_buper.
    DATA: fisqtr(2) TYPE n.

    fisqtr =  wf_buper.

    CASE fisqtr.

      WHEN '01' OR '02' OR '03'.

        RESULT = '1'.

      WHEN '04' OR '05' OR '06'.

        RESULT = '2'.

      WHEN '07' OR '08' OR '09'.

        RESULT = '3'.

      WHEN '10' OR '11' OR '12'.

        RESULT = '4'.

    ENDCASE.

******* Another Example
Here the Vendor is filled  whenever there is no Vendor associated with  Source Data from the pooled data collected at START ROUTINE.

  IF  SOURCE_FIELDS-zz_lifnr NE space.
      RESULT =  SOURCE_FIELDS-zz_lifnr.

    ELSE.
      CLEAR wa_mmat_plant.
      READ TABLE it_mmat_plant INTO wa_mmat_plant WITH KEY
                        mat_plant = SOURCE_FIELDS-matnr
                        plant = SOURCE_FIELDS-werks.

      RESULT =  wa_mmat_plant-vendor.


    ENDIF.
 
Type read Master Data  is useful if the characteristic in question is an attribute of existing Info object from the source structure. Examples for this could be  like filling Product Hierarchy/Material Type from Material Type Info object at Transactional Level.
You can see here the Customer is being filled from Attribute of BPARTNER.
 
Type Constant means you are passing constant Value all the time for entire data being inserted into Target.
 
End Routine
The End Routine is final step before the data being inserted to the Target. Here is you can do reevaluation ,summarizing the data or reformatting entire Data package as a single step.


Example;
Here the data is dropped where the FISCAL PERIOD  IS XX  which is derived at Characteristic Routine .
Also the  +/- Sign is moved to Front like 100- instead  – 100.
  DELETE RESULT_PACKAGE
                      WHERE /bic/zfiscper EQ 'XXXX.XXX'
                      OR /bic/zamt_bpc EQ 0.

    LOOP AT RESULT_PACKAGE ASSIGNING .

      CALL METHOD zcl_ace_base_class=>cloi_put_sign_in_front
        CHANGING
          value = -/bic/zamt_bpc.

    ENDLOOP.



No comments:

Post a Comment