Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Add dynamic columns based on sum of the rows in alv

Hi,

My requirement is I have total amount field and I need to add the dynamic columns for year wise amount fields say (2011,2012,2013 etc).Now, I have to sum the year wise amounts until the sum equals to total amount. When the year wise sum equals to total amount I need to stop to add the dynamic columns.

For ex : output is : Amount 2011 2012 2013 2014 so on

200 100 50 25 25

I had gone through the blogs but couldnt get the correct solution. Anyone can help please ?

3 REPLIES 3

rosenberg_eitan
Active Contributor

Hi,

Is it really dynamic ?

How many years are you expecting more then fifty ?

You can create a fix size ALV and just show the active columns.

To demonstrate see included program:

Regards.

y-r-eitan-test-06-03.txt

bertrand_delvallee
Active Participant
0 Kudos

Hello,

It seems more like an algorithm problem.

I guess your "Total amount" column is an objective and you want to do a projection, depending on capacities produced every year, to determine how many years you need to achieve this objective?

Once you have determined how many years you need you will be able to add columns (or display them like said by Eitan Rosenberg).

In pseudo code it should be like :

DATA data_table structured like :
total_amount type amount,
year_1  type amount,
year_2 type amount,
...
year_10 type amount,
.

CLEAR max_columns_needed.
LOOP AT data_table.
  CLEAR l_objective_acheived.
  l_nb_year_needed = 1.
  WHILE l_objective_acheived < data_table-total_amount
    AND l_nb_column_needed <= 10.
* CONDITION 1 : reach the objective
    CONCATENATE 'DATA_TABLE-YEAR_' l_nb_year_needed into l_column_name.
    ASSIGN (l_column_name) to <year_column>.
    add <year_column> to l_objective_acheived.
* CONDITION 2 : reachable objective
    IF max_columns_needed < l_nb_column_needed.
       l_nb_column_needed = l_nb_column_needed.
    ENDIF.
    ADD 1 TO l_nb_column_needed.
  ENDWHILE.
* HERE : l_nb_column_needed contains how many years are needed to reach* total_amount of the current line,
* OPTIONAL : with 2 additional fields in DATA_TABLE you can save 
* l_nb_column_needed and l_objective_acheived for each line (for debug * or display)
ENDLOOP.

* HERE : max_column_needed contains the number of "year column" 
* you have to display (from  1 to 10).

naimesh_patel
Active Contributor
0 Kudos

Or you can create the dynamic Internal table http://zevolving.com/2008/09/dynamic-internal-table-creation/ based on the number of inputs. I assume that you have range on selection screen and user can enter say 2001 to 2016 and you want to have 16 columns.

Once you create the dynamic internal table, you can use the ASSIGN statement to populate the data for each column.

Regards.