Skip to Content

Add dynamic columns based on sum of the rows in alv


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 ?

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

3 Answers

  • Oct 13, 2016 at 04:46 AM


    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:



    zrgf4.png (99.5 kB)
    Add comment
    10|10000 characters needed characters exceeded

  • Oct 13, 2016 at 03:25 PM


    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.
        ADD 1 TO l_nb_column_needed.
    * 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)
    * HERE : max_column_needed contains the number of "year column" 
    * you have to display (from  1 to 10).
    Add comment
    10|10000 characters needed characters exceeded

  • Nov 02, 2016 at 04:30 PM

    Or you can create the dynamic Internal table 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.


    Add comment
    10|10000 characters needed characters exceeded