2018 Jun 01 12:39 PM
Hello experts,
I am new to SAP ABAP. Could you please help me with the below issue.
I have an internal table with non-unique values as shown below.

I need to calculate the minimum price and percentage change for each material and populate the next 2 columns, which should look as below.

I tried implementing this with a select query, but it did not work. Is there any other way to modify an internal table to accommodate the changes.
Thanks in advance.
Thank you,
Abhishek
Hello experts,
I am new to SAP ABAP. Could you please help me with the below issue.
I have an internal table with non-unique values as shown below.

I need to calculate the minimum price and percentage change for each material and populate the next 2 columns, which should look as below.

I tried implementing this with a select query, but it did not work. Is there any other way to modify an internal table to accommodate the changes.
Thanks in advance.
Thank you,
Abhishek
2018 Jun 01 1:35 PM
2018 Jun 01 1:36 PM
Hi,
first of all you need to calculate the minimum for each material in your itab. To do this you need to create a temp itab which you will collect each material with the minimum price. Sample code below
types: begin of t_min,
matnr type matnr,
min type netpr,
end of t_min.
data: lt_min type table of t_min.
data: ls-min type t_min.
data: w_index type i.
loop at itab into ls_itab. "itab is you internal table.
read table lt_min into ls_min with key matnr = ls_itab-matnr.
w_index = sy-tabix
if sy-subrc <> 0. " first time
ls_min-matnr = ls_itab-matnr.
ls_min-min = ls_min-netpr.
append ls_min to lt_min.
else.
if ls_itab-netpr < ls_min-netpr.
ls_min-netpr = ls_itab-netpr.
modify lt_min from ls_min index w_index.
endif.
endif.
endloop.
After you have calculate the min for all materials just loop once more to update you original table
loop at itab into ls_itab.
w_index = sy-tabix.
read table lt_min into ls_min with key matnr = ls_itab-matnr.
ls_itab-min_price = ls_min-min.
*Now calculate persentage
ls_itab-perc = (( ls_itab-netpr - ls_itab-min_price) * 100 ) / ls_itab-min_price.
modify itab from ls_itab index w_index.
endloop.
I hope this helps.
S.Socratous
2018 Jun 01 5:13 PM
It would be simpler and more effective with LOOP AT ASSIGNING instead of MODIFY.
It would be contemporary with expressions.
It would be more readable if you formatted your code example.
2018 Jun 01 2:36 PM
Hi,
Use control level statements like this reference : https://wiki.scn.sap.com/wiki/display/ABAP/Control+Level+Statements+in+ABAP
1. Sort itab comparing matnr netpr (descending)
2. Loop itab.
at end of matnr.
--> this line minimum of netpr
endat.
endloop.
2018 Jun 01 5:14 PM
| User | Count |
|---|---|
| 3 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |