Skip to Content

Custom Sub total and totals text using Dynamic internal tables in ALV


Dynamic internal tables come into picture in ABAP when the numbers of fields or columns are not known. The concept of internal table applies to the dynamic internal tables too, but just that the rows or columns are dynamic and not static.

Challenges faced in the requirement are:

a)    Display custom subtotal/total texts

b)    Perform difference of the subtotal values to display the difference of 2 subtotal amounts when dynamic internal tables are used.

c)    Color the sub total rows


Here, we shall see an example of an ALV display where columns are static and dynamic in nature.

(Limitation: Maximum of 99 columns can be displayed in an ALV).

ALV output to be built should be as seen in the below screen shot. Assume the G/L accounts to be displayed in the output are maintained in a custom table. Hence, based on the number of G/L accounts in the custom table the columns of the ALV are built.

  • Sub totals texts and Total/Difference texts have to be displayed by calculating the amounts from the line items.



  • Firstly, build an internal table with all the required data fetched from the database tables.
  • First column values would be either’ Paid’ or ‘Not paid’ (depending on the requirement).
  • Build the field catalog for the static and dynamic columns. For dynamic columns, give a unique field name for referencing during subtotal and total amount calculation.
  • Build the field catalog with color if required for the subtotal and total rows.
  • Once the field catalog is built, the dynamic table can be created with reference to the above built field catalog (gt_fldcat).

  CALL METHOD cl_alv_table_create=>create_dynamic_table
      it_fieldcatalog          = gt_fldcat
      ep_table                  = gt_dtable
      generate_subpool_dir_full =
OTHERS                    = 2.

  • Using the above call to method ‘ CREATE_DYNAMIC_TABLE’ of class ‘ CL_ALV_TABLE_CREATE’ gt_dtable can be created. Declaration of gt_dtable is shown below.


               DATA : gt_dtable    TYPE REF TO data.

  • Assign the reference of gt_dtable to a field symbol created for the dynamic internal table.


ASSIGN gt_dtable->* TO <gt_dtable>.

NOTE: The property of gt_dtable is assigned to <gt_dtable> which is the dynamic internal table.

·        To create a work area for the dynamic internal table use the below syntax.

The structure of <gt_dtable> is copied to gt_newline and the property of gt_newline is assigned to <fs_dtable> which will act as the work area for <gt_dtable>.

DATA : gt_newline  TYPE REF TO data.

            CREATE DATA gt_newline LIKE LINE OF <gt_dtable>.
        ASSIGN gt_newline->* TO <fs_dtable>.

·        To fill the dynamic internal table use ASSIGN COMPONENT syntax and pass the data to a field symbol as shown below.

     LOOP AT gt_data INTO f_data. “Table containing the data for display

     ASSIGN COMPONENT <field name> OF STRUCTURE <fs_dtable> TO <w_field>.

     IF sy-subrc IS INTITAL.

       <w_field> = f_data-<field name>.




Calculating sum/difference between different sub-total fields:

·        Subtotal amounts have to be calculated based on the values in the BLANK column (Paid or Not Paid) with the help of the fieldnames in field catalog.

NOTE: The subtotal values can be obtained from ALV functionality, but as the difference of the subtotals is required we are following this approach. Also, to display user specific subtotal texts this approach is used.

·        Store the amounts for each column in a new internal table (this will be useful for calculating the difference for each column).

NOTE: Total functionality is available in ALV but not the difference of subtotal values. Hence, here the sub total values are being used for getting the difference amount.

·        Fetch the index from the dynamic internal table for BLANK column values as ‘Paid’ and ‘Not Paid’ to insert the sub total values and subtotal texts.

·        ‘Net Difference’ text and the respective amount can be inserted at the end of the dynamic internal as it has to be the last line in the display.

NOTE: A new work area has to be created with the similar structure of gt_fldcat in order to hold the subtotal rows of ‘Paid’ and ‘Not Paid’ and the ‘Net difference’ row.

·        Call Function Module ‘ REUSE_ALV_GRID_DISPLAY_LVC’ for the desired output.

Sample code for display is given below.

·      1)  Create dynamic internal table:

DATA : gt_fldcat TYPE lvc_t_fcat,

gt_newline   TYPE REF TO data.


    <fs_dtable> TYPE ANY.

*–Create dynamic internal table and assign to FS
CLEAR : gt_dtable, gt_newline.
CALL METHOD cl_alv_table_create=>create_dynamic_table
      it_fieldcatalog           = gt_fldcat
      ep_table                  = gt_dtable
      generate_subpool_dir_full =
OTHERS                    = 2.
IF sy-subrc <> 0.                                         “#EC NEEDED

  ASSIGN gt_dtable->* TO <gt_dtable>.

*–Create dynamic work area and assign to FS
CREATE DATA gt_newline LIKE LINE OF <gt_dtable>.
ASSIGN gt_newline->* TO <fs_dtable>.

2) Fill dynamic internal table <gt_dtable> using ASSIGN COMPONENT syntax.

3) Calculate the index for ‘Paid’ and ‘Not Paid’.

*–Calculating index to display sub totals row


     IF sy-subrc IS INITIAL.

          IF <w_fld> EQ ‘Paid’.

            w_ind = w_ind + 1.

          ELSEIF <w_fld> EQ ‘Not Paid’.

            w_lin = w_lin + 1.





4) Fetch the subtotal values into a work area and insert them into the dynamic internal table as shown below.

Column_name – Name of the column where the sub total text has to be displayed.

     ASSIGN COMPONENT ‘<Column_name>’ OF STRUCTURE <fs_tab> TO <w_fld>.

     IF sy-subrc IS INITIAL.

       <w_fld> = ‘Paid’.

     Color the subtotal row of the dynamic ALV.

IF sy-subrc IS INITIAL.
        <w_fld> =


    w_ind = w_ind +
INSERT <fs_tab> INTO <gt_dtable> INDEX w_ind.

      5) Display data using the FM below.

      i_callback_program       = w_repid
      i_callback_pf_status_set =
      i_callback_user_command  =
      is_layout_lvc            = gf_lay
      it_fieldcat_lvc          = gt_fldcat
      t_outtab                 = <gt_dtable>.

You must be Logged on to comment or reply to a post.