How To Report Data in Alternate Units of Measure
How To Report Data in Alternate Units of Measure
Overview
1 1.1 1.2 1.3 1.4 2 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 4 5 6 BUSINESS SCENARIO .............................................................................................................2 Key findings....................................................................................................................................... 2 Data Flow Diagram............................................................................................................................ 2 Benefits .............................................................................................................................................. 3 Version History.................................................................................................................................. 3 SOLUTION ................................................................................................................................4 THE STEP-BY-STEP SOLUTION..............................................................................................5 Key Figure for Conversion Factor .................................................................................................... 5 Characteristic Material..................................................................................................................... 6 Characteristic for Alternate Units of Measure ................................................................................. 7 Variable for Alternate Unit of Measure............................................................................................ 9 Formula Variable for Conversion Factor.........................................................................................11 Modification......................................................................................................................................13 User Exit for Variables.....................................................................................................................17 Calculated Key Figures for Conversion ...........................................................................................20 Restricted Key Figure for Input.......................................................................................................22 Create a Query .................................................................................................................................23 PRESENTATION ..................................................................................................................... 24 COMMENTS ............................................................................................................................ 25 APPENDIX............................................................................................................................... 25
1 Business Scenario
This document describes the how to report data in alternate units of measure. Data is stored in InfoCubes in quantity key figures with the base unit of measure of materials. This data needs to be converted dynamically into an alternate unit of measure, which is specified by the end user at query execution. The goal of this design is to enable flexible and user-friendly reporting in any alternate unit of measure. There is no solution available in the Standard SAP BW releases 2.x and 3.x. Therefore a new solution has been developed. This solution is based on a modification of SAP standard coding.
1.1
Key findings
A dynamic unit conversion as with currencies in BEx context menus is not possible. A calculated key figure with a variable based on it can be used to represent all units of measurement for which conversion factors in the material master exist. At the start of a query, the user enters the alternate unit of measure as a text variable. The system then reads the necessary conversion factors and dynamically converts the quantities into the alternate unit of measure. The solution is based on a small modification of standard SAP code.
1.2
1.3
Benefits
Even though the solution might appear to be complex at first, it is very usable and maintainable for the end users. Everything is a one-time setup. Only the conversion factors (InfoObject 0MAT_UNIT) have to be loaded (and maintained on R/3) on a regular basis.
Independent of data model (solution works with any Infocube and query). Low maintenance (one-time setup of calculated key figures which can be reused). Very easy to use (simple input of alternate unit of measure by user). Correct display of unit of measure in query result (important if you want to use result in further calculations). Correct aggregation of converted quantities (totals and subtotals are calculated). Low performance impact (conversion factors are read once from master data table before selection of transaction data). No impact on transaction data (aggregates, InfoSources, update rules, etc.). Can be implemented in SAP BW release 2.0A to 3.5.
1.4
Version History
Initial release of How-to paper. Improved handling of inexistent conversion factors (see section 3.7, include ZXRSRU01). Correction of user exit coding (for example missing ABAP variables). Added reference to SAP note 617048: Unit of measure displayed as ERR in BEx.
November 2004
Changed variable type from text to characteristic value, which allows usage of OLAP cache and also enables the value help for unit of measure. Note: If the old solution with a text variable is already in place, you have to use a different variable name (for example YCAUNIT instead of YTAUNIT). The ABAP coding does not have to be adjusted since the checking routine (I_STEP = 3) will be obsolete. How-to paper is also relevant for SAP BW release 3.5. Added table of contents.
July 2005
2 Solution
The following objects need to be installed for this solution. With the exception of the modification only standard functionality is used: Key figure: Conversion factor (attribute YUOMCNVF of characteristic 0MATERIAL) Characteristics: Material (0MATERIAL), Alternate Units of Measure (0MAT_UNIT) Variable: Alternate Unit of Measure (YTAUNIT) Formula Variable: Conversion factor (YFCONVF) Modification: Read attributes for formula variables (LRRSVF20) User Exit: Variable Enhancement (ZXRSRU01) Calculated Key Figures for Queries
When a query is executed, then following steps are processed: 1. User enters Alternate Unit of Measure on variable selection screen for variable YTAUNIT. 2. Text variable is being validated in user exit ZXRSRU01. If input is OK, the alternate unit of measure is stored in a global program variable. Otherwise an error message is displayed and the user has to correct the entry. 3. The system starts the query program and reads all values for attribute YUOMCNVF of characteristic 0MATERIAL in order to determine the formula variable YFCONVF. 4. This is the point where the modification is processed. The modification will call the user exit ZXRSRU01 once for each value of 0MATERIAL that is selected in the query. This allows determining the conversion factor based on user input of the alternate unit of measure (instead of using the attribute value from the material master data). The actual value of attribute YUOMCNVF is ignored. 5. In the user exit ZXRSRU01 for each material the conversion factor from the base unit of measure to the alternate unit of measure will be determined from the master data table of characteristic 0MAT_UNIT. If a conversion factor cannot be determined (does not exist or division by zero), it is set to zero. The conversion factors are buffered in memory in order to minimize database selections. 6. The system continues processing the query. The transaction data is selected from the InfoCube. Then the system determines the result of each calculated key figure based on the formula variable.
3.2
Characteristic Material
Activate InfoObject 0MATERIAL from business content. Add key figure YUOMCNVF as an attribute to InfoObject 0MATERIAL. Note: This attribute can be left empty (initial value) and does not need to be filled with any conversion factor!
3.3
Activate InfoObject 0MAT_UNIT from business content. Load conversion factors from R/3 into InfoObject 0MAT_UNIT. Note: The numerator and denominator attributes need to be filled in the master data table.
In order to determine the conversion factor from the base unit of measure to an alternate unit of measure the following formula has to be applied.
Conversion Factor =
Numerator (Base UoM) Denominato r (Base UoM) Numerator (Alternate UoM) Denominato r (Alternate UoM)
3.4
Note: The screen shots show the variable wizard of release 3.x. In release 2.x, you have to use transaction RSZV to create variables. Create variable YTAUNIT for characteristic Unit of Measure (0UNIT).
10
3.5
11
HOW TO REPORT DATA IN ALTERNATE UNITS OF MEASURE Pick the attribute that was added to Material in chapter 3.2 (YUOMCNVF).
12
3.6
Modification
13
LIKE TYPE TYPE TYPE TYPE TYPE TYPE TYPE LIKE LIKE
rszglobv-vnam, rsd_s_cob_pro, rsr_s_rkb1d, rro01_s_rkb1f-periv, rrs0_t_var_range, rrs0_s_var_range, rsr_t_rangesid, rsr_s_rangesid, rszglobv-meeht, rszglobv-waers.
* Loop over all characteristic values LOOP AT l_tx_atr ASSIGNING <l_sx_atr>. * Loop over all variables to be replaced by attributes LOOP AT i_th_var_atr ASSIGNING <l_s_var>. * Fill additional information into pseudo variables REFRESH l_t_var_range. InfoObject (i.e. characteristic) CLEAR l_s_var_range. l_s_var_range-sign = 'I'. l_s_var_range-opt = 'EQ'. l_s_var_range-vnam = '1CHANM'. l_s_var_range-iobjnm = i_chanm. l_s_var_range-low = <l_sx_atr>-chavl. APPEND l_s_var_range TO l_t_var_range. Attribute READ TABLE <l_sx_atr>-t_atr ASSIGNING <l_s_atr> WITH KEY attrinm = <l_s_var>-attrinm. IF sy-subrc <> 0. RAISE x_message. ENDIF. CLEAR l_s_var_range. l_s_var_range-sign l_s_var_range-opt l_s_var_range-vnam l_s_var_range-iobjnm l_s_var_range-low APPEND l_s_var_range
14
InfoObject properties CALL FUNCTION 'RSD_COB_PRO_GET_ALWAYS' EXPORTING i_iobjnm = i_chanm IMPORTING e_s_cob_pro = l_s_cob_pro EXCEPTIONS infocube_not_found = 1 error_reading_infocatalog = 2 iobjnm_not_found = 3 illegal_input = 4 OTHERS = 5. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. l_vnam = <l_s_atr>-attrinm.
15
* *
16
3.7
Implement user exit for variables and activate enhancement RSR00001. Note: Be sure to implement the coding into the correct include. Otherwise the solution will not work.
*----------------------------------------------------------------------* ***INCLUDE ZXRSRTOP. *----------------------------------------------------------------------* *** Enhancement: Alternate U.o.M. DATA: g_aunit TYPE /bi0/oimat_unit. *----------------------------------------------------------------------* ***INCLUDE ZXRSRU01. *----------------------------------------------------------------------* *** Enhancement: Alternate U.o.M. DATA: l_s_range TYPE rsr_s_rangesid, l_s_var TYPE rrs0_s_var_range. * l_dummy(72) TYPE c. DATA: l_factor l_factor1 l_factor2 l_msgv1 l_msgv2 l_msgv3 l_material l_base_uom
"delete
* Called after user input IF i_step = 3. * Get variable for alternative unit entered by user * Validate entry and store as global variable READ TABLE i_t_var_range WITH KEY vnam = 'YTAUNIT' INTO l_s_var. IF sy-subrc = 0. g_aunit = l_s_var-low. insert * >>> delete * TRANSLATE l_s_var-low TO UPPER CASE. "#EC TRANSLANG * CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT' * EXPORTING * input = l_s_var-low * language = sy-langu * IMPORTING * output = g_aunit * EXCEPTIONS * unit_not_found = 1 * OTHERS = 2. * IF sy-subrc <> 0. ** Error: Please enter a valid unit of measure * l_msgv1 = 'Unknown unit of measure:'. * l_msgv2 = l_s_var-low. "insert * l_msgv3 = '. Please enter a valid unit of measure.'. * CALL FUNCTION 'RRMS_MESSAGE_HANDLING' * EXPORTING
17
"change
"#EC *
l_factor2.
* return conversion factor for variable IF l_factor2 <> '0.0'. l_factor = l_factor1 / l_factor2. e_meeht = g_aunit. "insert ELSE. * When the UoM conversion is not maintained, instead of showing 0, the * base UoM with a factor of 1 is used. Therefore, the user sees MIXED * VALUES in the total line and knows right away that there is something * wrong. That way its easier to track inexistent conversion factors. * l_factor = 0. "delete l_factor = 1. "insert e_meeht = l_base_uom. "insert ENDIF.
18
*----------------------------------------------------------------------* ***INCLUDE ZXRSRF01. *----------------------------------------------------------------------* *&---------------------------------------------------------------------* *& Form get_factor *&---------------------------------------------------------------------* * Get conversion factor from /bi0/pmat_unit *----------------------------------------------------------------------* FORM get_factor USING i_material TYPE /bi0/oimaterial i_mat_unit TYPE /bi0/oimat_unit e_factor. TYPES: BEGIN OF ys_mat_unit, material TYPE /bi0/oimaterial, mat_unit TYPE /bi0/oimat_unit, numerator TYPE /bi0/oinumerator, denomintr TYPE /bi0/oidenomintr, END OF ys_mat_unit. DATA: l_s_mat_unit TYPE ys_mat_unit. STATICS: s_t_mat_unit TYPE SORTED TABLE OF ys_mat_unit WITH UNIQUE DEFAULT KEY. CLEAR: l_s_mat_unit, e_factor. READ TABLE s_t_mat_unit INTO l_s_mat_unit WITH KEY material = i_material mat_unit = i_mat_unit. IF sy-subrc <> 0. SELECT SINGLE * FROM /bi0/pmat_unit INTO CORRESPONDING FIELDS OF l_s_mat_unit WHERE material = i_material AND mat_unit = i_mat_unit AND objvers = 'A'. IF sy-subrc = 0. INSERT l_s_mat_unit INTO TABLE s_t_mat_unit. ENDIF. ENDIF. IF l_s_mat_unit-denomintr <> 0. e_factor = l_s_mat_unit-numerator / l_s_mat_unit-denomintr. ENDIF. ENDFORM. " get_factor
19
3.8
For each basic key figure that should be shown with the alternate unit of measure, create a matching calculated key figure. Note: The solution works only if you build calculated key figures. You must not use the conversion factor (formula variable) directly in structure formulas.
The generic formula for the conversion is: = NODIM( Quantity ) * 'Conversion Factor to Alternative Unit of Measure'
20
21
3.9
To activate the input of the "Alternative Unit of Measure, create one restricted key figure. Restrict the selection for Unit of Measure to the variable that was created in step 3.4.
22
Set the restricted key figure to "Always Hide. It is used only to reference the input variable for Unit of Measure.
23
4 Presentation
When you execute the query, the user will be prompted for the alternate unit of measure on the variable selection screen.
The unit of measure can be entered or selected using the value help.
The query result will show the quantities converted into the unit of measure entered by the user. If no conversion factor is available (in the master data for 0MAT_UNIT) then the result of the conversion is equal to the original value.
Note: In this example, conversion factors have been maintained only for some materials. Therefore, the some converted values are shown with the original amount and unit of measure.
24
5 Comments
In BW release 3.x you have to delete the OLAP cache for the queries that use the alternative unit of measure, whenever new conversion factors are loaded! Alternatively, you can deactivate the OLAP cache for those queries (transaction RSRT). The "key to success" is to use a calculated key figure. Anything else like a formula in a structure does not work! Also this calculated key figure must be defined based on the basic key figure of the InfoCube and not based on other restricted or calculated key figures. Note: In order to return to the SAP Standard, simply remove the modification from program LRRSVF20. If the units of measure are displayed as ERR in the result set, please implement SAP note 617048 or the corresponding support pack.
6 Appendix
Here are some hints for debugging in case the query is not showing the expected results: 1. 2. 3. Run transaction RSRT and select your query. Edit Technical Information
Check section OLAP-Relevant Data. Calculation before Aggregation and Attributes in Calculated Key Figures both need to be Y. Otherwise the key figure(s) are not setup correctly. Note: The red/yellow lights are OK and can be ignored.
25
4. 5. 6.
Go to
Scroll down to "FORM SELECT_0 ..." In this form routine you should find the following lines: PERFORM VARCHAVL_INIT in program (...) USING '0MATERIAL' L_T_0MATERIAL. LOOP AT I_T_DATA INTO G_S_DATA. If the PERFORM VARCHAVL_INIT does not exist, the query definition is not correct. There is no calculated key figure in this query using the formula variable, which is replaced by the attribute. Check the query definition. If the PERFORM VARCHAVL_INIT exists, put a break-point on that line and run the query (via RSRT). After you entered the query variable on the popup, the system will stop on this line and if you step through the code it will reach the point of the modification. In addition you might want to put another break-point into the user exit for the variable replacement.
7.
8.
26