05-29-2013 11:03 AM
Hi everyone,
I need may be a formula or an ABAP routine to calculate below.
The idea is to sum 'Amount' and put into a new column period wise. For example: Period2 100 + PeriodSum 100 = PeriodSum 200.
Then for Period3 amount -10 + PeriodSum 200 = 190......and so on
So it shoud take current Period Amount + Previous PeriodSum
FY/period Amount PeriodSum
2012001 100 100
2012002 100 200
2012003 -10 190
Thanks for your input
Ahmad
05-29-2013 1:25 PM
add one more piece of code to my earlier one:
In the read statement i am taking the period sum just above the current row. Suppose if it is the first row that you are accessing, then there will not be any earlier row. So adjust the code like this :
data : newsum type i. (declare based on your data element otherwise).
clear newsum.
loop at itab into wa.
read table itab into waitab index sy-tabix - 1.
if sy-subrc ne o.
newsum = wa-amount.
wa-periodsum = newsum.
else.
newsum = wa-amount + waitab-periodsum.
endif.
endloop.
after all your calculations, modift the internal table setting the transport values.
05-29-2013 12:25 PM
HI Ahmed,
please add the below code in your loop where u are populating the data in your Final ITAB.
in your loop.
initialize sum = 0 befor loop.
sum = sum + wa_itab-amount.
wa_itab-periodsum = sum.
append wa_itab to ITAB.
Hope this helps.
Thanks & Regards,
Azhar
05-29-2013 12:28 PM
Ahmad,
That's something that I would think I would only do on output, and a fixed output at that. Say for instance, that you wrote this as an ALV. If the user reorder the data, then the column is wrong. But perhaps you have a different intention. I believe that something could be done like that in Report painter / Report Writer (not that I'd ever willingly use such a thing).
Could you give us an detailed explanation of the requirement that causes you to ask this?
Neal
05-29-2013 12:59 PM
Hi,
Since you are having all the data in a single internal table, do the following:
from the above scenario, i believe that you are entering the amount field manually.
data : newsum type i. (declare based on your data element otherwise).
clear newsum.
loop at itab into wa.
read table itab into waitab index sy-tabix - 1.
newsum = wa-amount + waitab-periodsum.
endloop.
now move the field newsum to which ever field you want.
Hope this helps..
05-29-2013 1:25 PM
add one more piece of code to my earlier one:
In the read statement i am taking the period sum just above the current row. Suppose if it is the first row that you are accessing, then there will not be any earlier row. So adjust the code like this :
data : newsum type i. (declare based on your data element otherwise).
clear newsum.
loop at itab into wa.
read table itab into waitab index sy-tabix - 1.
if sy-subrc ne o.
newsum = wa-amount.
wa-periodsum = newsum.
else.
newsum = wa-amount + waitab-periodsum.
endif.
endloop.
after all your calculations, modift the internal table setting the transport values.
05-29-2013 1:54 PM
If your internal table does not have the field sum, create an internal table eg final_tab with column sum in addition to all other columns in the existing internal table, lets say that is itab.
itab structure - Current internal table
period
amount
final_itab structure. Final internal table
period
amount
sum
wa_itab like line of itab.
wa_final like line of final_itab
data : total type i.
Assuming itab contains all the details.
total = 0 .
loop at itab into wa_itab.
move corresponding wa_itab to wa_final.
total = total + wa_itab-amount.
wa_final-sum = total.
append wa_final into final_itab
clear : wa_final, wa_itab.
endloop.
_________________________________
if your current table is already having structure final_itab.
total = 0.
Loop at final_itab into wa_final.
total = total + wa_final-amount.
wa_final-sum = total.
modify final_itab INDEX sy-tabix FROM wa_final.
clear wa_final.
endloop.
05-29-2013 5:31 PM
Hi Ahmed ,
Please find the below code for your reference , So that you can get you desired result .
TYPES : BEGIN OF st_final ,
period(7) TYPE c ,
amount TYPE i ,
persum TYPE i ,
END OF st_final .
DATA : wa TYPE st_final ,
itab TYPE STANDARD TABLE OF st_final .
DATA : v_sum TYPE i .
SORT itab BY period ASCENDING .
CLEAR wa .
LOOP AT itab INTO wa .
v_sum = v_sum + wa-amount .
wa-persum = v_sum .
MODIFY itab FROM wa INDEX sy-tabix TRANSPORTING persum .
CASE wa-period+4(3) .
WHEN '003' OR '006' OR '009' OR '012' .
CLEAR v_sum .
WHEN OTHERS .
ENDCASE .
CLEAR wa .
ENDLOOP .
Thanks,
Mallikarjun
05-29-2013 6:11 PM
HI Ahmad,
Try the below code.
*&---------------------------------------------------------------------*
*& Report ZSUM_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsum_test.
TYPES : BEGIN OF ty_final ,
period(7) TYPE c ,
amount TYPE i ,
persum TYPE i ,
END OF ty_final .
DATA : wa_tab TYPE ty_final ,
wa_final TYPE ty_final,
wa_tab1 TYPE ty_final,
it_tab TYPE STANDARD TABLE OF ty_final,
it_final TYPE STANDARD TABLE OF ty_final.
DATA:lv_sum TYPE i.
CLEAR:lv_sum,
wa_tab1.
CLEAR wa_tab.
wa_tab-period = '2012001'.
wa_tab-amount = '100'.
APPEND wa_tab TO it_tab.
CLEAR wa_tab.
wa_tab-period = '2012001'.
wa_tab-amount = '200'.
APPEND wa_tab TO it_tab.
CLEAR wa_tab.
wa_tab-period = '2012002'.
wa_tab-amount = '100'.
APPEND wa_tab TO it_tab.
CLEAR wa_tab.
wa_tab-period = '2012003'.
wa_tab-amount = '-10'.
APPEND wa_tab TO it_tab.
SORT it_tab BY period.
LOOP AT it_tab INTO wa_tab.
wa_tab1 = wa_tab.
AT NEW period.
wa_final-period = wa_tab-period.
ENDAT.
wa_final-amount = wa_tab-amount.
lv_sum = lv_sum + wa_tab1-amount.
wa_final-persum = lv_sum.
APPEND wa_final TO it_final.
CLEAR :wa_tab,
wa_final.
ENDLOOP.
please find the output.
let me know if any issue.
Regards,
Gurunath