02-15-2019 3:59 AM
Hi abap expert.
How to transpose data from rows to column in abap sap. My requirement is like from picture below
I want to transpose from table 1 to table 2. But my requirement, in table 2 header must be dynamic. So can fetch all data in field Type in table 1.
Please help me guys
Thank you
02-15-2019 5:44 AM
If I take your question literally, your requirement picture shows transposing via Excel, so you may use OLE statements in ABAP (like CALL METHOD ... OF ..., GET PROPERTY ... OF ...) and you may select the source range dynamically (two variables for the number of rows and columns) with the Excel OLE method "cells" of the "worksheet" object, plus the "select" and "copy" methods, finally the "paste" method with "transpose" option.
02-15-2019 6:26 AM
Hi Sandra Rossi,
No, that image just as an example.. My requirement is How to transpose like in the picture but in ALV grid.
02-15-2019 7:01 AM
02-15-2019 7:01 AM
Read up on dynamic programming. Search for dynamic ALV questions/answers and blogs. Find out what RTTS is, and use that. This has already been covered widely. Googling transpose alv site:sap.com produces at least 10 hits.
02-18-2019 2:31 AM
02-15-2019 9:07 AM
Hi,
you can check this report as an example:
https://github.com/juancarlosrodriguezf/myPublicSAP/blob/master/ztranspose.prog.abap
*&---------------------------------------------------------------------*
*& Report ztranspose
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
report ztranspose.
class lcl_appl definition create private.
public section.
class-methods create
returning
value(ro_appl) type ref to lcl_appl.
methods start_of_selection.
protected section.
private section.
class-data go_appl type ref to lcl_appl.
endclass.
start-of-selection.
lcl_appl=>create( )->start_of_selection( ).
class lcl_appl implementation.
method create.
if go_appl is not bound.
go_appl = new lcl_appl( ).
endif.
ro_appl = go_appl.
endmethod.
method start_of_selection.
data lt_data_table_transpose type ref to data.
field-symbols <lv_data_table> type any.
field-symbols <lt_data_table_transpose> type table.
field-symbols <lv_data_table_transpose> type any.
select * from t000 into table @data(lt_data_table).
cl_salv_table=>factory(
importing
r_salv_table = data(lo_alv_table) " Basis Class Simple ALV Tables
changing
t_table = lt_data_table
).
lo_alv_table->display( ).
data(lt_components_transpose)
= value cl_abap_structdescr=>component_table(
(
name = 'Attributes'
type = cast #( cl_abap_datadescr=>describe_by_name( p_name = 'CHAR20' ) )
)
).
lt_components_transpose
= value cl_abap_structdescr=>component_table(
base lt_components_transpose
for i = 1 then i + 1 while i <= lines( lt_data_table )
(
name = |Field{ lt_data_table[ i ]-mandt }|
type = cast cl_abap_datadescr( cl_abap_datadescr=>describe_by_name( p_name = 'CHAR20' ) )
)
).
data(lo_tabledescr) = cl_abap_tabledescr=>create(
p_line_type = cast #( cl_abap_structdescr=>create( p_components = lt_components_transpose ) )
).
create data lt_data_table_transpose type handle lo_tabledescr.
assign lt_data_table_transpose->* to <lt_data_table_transpose>.
loop at cast cl_abap_structdescr( cast cl_abap_tabledescr( cl_abap_tabledescr=>describe_by_data( p_data = lt_data_table ) )->get_table_line_type( ) )->get_components( )
assigning field-symbol(<ls_components>).
append initial line to <lt_data_table_transpose> assigning field-symbol(<ls_data_table_transpose>).
assign component 'Attributes' of structure <ls_data_table_transpose> to <lv_data_table_transpose>.
<lv_data_table_transpose> = <ls_components>-name.
endloop.
loop at lt_data_table assigning field-symbol(<ls_data_table>).
loop at cast cl_abap_structdescr( cast cl_abap_tabledescr( cl_abap_tabledescr=>describe_by_data( p_data = lt_data_table ) )->get_table_line_type( ) )->get_components( )
assigning <ls_components>.
loop at <lt_data_table_transpose> assigning <ls_data_table_transpose>.
assign component 'Attributes' of structure <ls_data_table_transpose> to <lv_data_table_transpose>.
if <lv_data_table_transpose> = <ls_components>-name.
assign component <ls_components>-name of structure <ls_data_table> to <lv_data_table>.
assign component |Field{ <ls_data_table>-mandt }| of structure <ls_data_table_transpose> to <lv_data_table_transpose>.
<lv_data_table_transpose> = <lv_data_table>.
exit.
endif.
endloop.
endloop.
endloop.
cl_salv_table=>factory(
importing
r_salv_table = data(lo_alv_table_transpose) " Basis Class Simple ALV Tables
changing
t_table = <lt_data_table_transpose>
).
lo_alv_table_transpose->display( ).
endmethod.
endclass.
02-18-2019 2:28 AM
Thanks for ur response, but i'm still confuse because i'm new in abap. So can you give me an sample code that more easier please.
Thanks
02-18-2019 7:07 AM
Looks to me that's as easy as it gets.
Run it through in debug if you can't figure out how it works.
02-18-2019 12:59 PM
Too many loops I guess, I have "simplified" :
DATA(lo_tabledescr) = cl_abap_tabledescr=>create( p_line_type = CAST #( cl_abap_structdescr=>create(
VALUE cl_abap_structdescr=>component_table(
LET alv_cell = VALUE text255( )
rtti_alv_cell = CAST cl_abap_datadescr( cl_abap_datadescr=>describe_by_data( alv_cell ) ) IN
(
name = 'COL_NAME'
type = rtti_alv_cell
)
( lines OF VALUE #( FOR i = 1 WHILE i <= lines( lt_data_table )
(
name = |ROW_{ i }|
type = rtti_alv_cell
) ) ) ) ) ) ).
CREATE DATA lt_data_table_transpose TYPE HANDLE lo_tabledescr.
ASSIGN lt_data_table_transpose->* TO <lt_data_table_transpose>.
LOOP AT CAST cl_abap_structdescr( CAST cl_abap_tabledescr( cl_abap_typedescr=>describe_by_data( p_data =
lt_data_table ) )->get_table_line_type( ) )->get_components( ) ASSIGNING FIELD-SYMBOL(<ls_components>).
DATA(source_comp_num) = sy-tabix.
APPEND INITIAL LINE TO <lt_data_table_transpose> ASSIGNING FIELD-SYMBOL(<ls_data_table_transpose>).
DATA(target_comp_num) = 1.
ASSIGN COMPONENT target_comp_num OF STRUCTURE <ls_data_table_transpose> TO <lv_data_table_transpose>.
<lv_data_table_transpose> = <ls_components>-name.
LOOP AT lt_data_table ASSIGNING FIELD-SYMBOL(<ls_data_table>).
ASSIGN COMPONENT source_comp_num OF STRUCTURE <ls_data_table> TO <lv_data_table>.
ADD 1 TO target_comp_num.
ASSIGN COMPONENT target_comp_num OF STRUCTURE <ls_data_table_transpose> TO <lv_data_table_transpose>.
<lv_data_table_transpose> = <lv_data_table>.
ENDLOOP.
ENDLOOP.
cl_salv_table=>factory(
IMPORTING
r_salv_table = DATA(lo_alv_table_transpose) " Basis Class Simple ALV Tables
CHANGING
t_table = <lt_data_table_transpose>
).
lo_alv_table_transpose->get_columns( )->set_optimize( ).
lo_alv_table_transpose->display( ).