Skip to main content

Chapter 6: Data Transformation and Enrichment

In the previous chapter, we unleashed the power of the HANA database, using an AMDP to fetch all our raw financial data with incredible speed. The data has arrived! But if we look at it closely, it's a bit like a pile of raw, unpolished parts straight from the factory. We have a vendor number but not the name, and confusing codes like 'S' instead of 'Debit'.

This chapter is about the assembly line. It's the crucial step where we take the raw data parts, clean them, combine them, and polish them into the final, valuable product that the user sees in the report. This process is called Data Transformation and Enrichment.

The Assembly Line Analogy

Imagine a car factory. A powerful machine fetches all the raw parts: a chassis, an engine block, doors, and a bag of bolts. These are our "raw data" from the AMDP.

An assembly line then takes these parts and transforms them:

  • Cleaning/Formatting: A bolt might be coded as 'H-17' in the inventory system, but on the assembly line, it's just a bolt. We convert codes into something understandable.
  • Enrichment: We take the plain engine block and add a serial number and a logo. We've enriched it with more information.
  • Combining: We attach the doors to the chassis. We've merged data from different sources.

Our program's assembly line for data does the exact same thing. It takes the raw data from our gt_final table and transforms it into the polished gt_sfinal table, ready for display.

This entire process happens inside the get_amdp_data subroutine, right after the call to the AMDP returns with the raw data.

Step 1: Moving to the Final Structure

Our first step is to move the data from the raw blueprint (ty_final) to our finished product blueprint (ty_sfinal), which we defined in the Report Data Model chapter. The finished structure has extra fields for the enriched data we're about to add.

This happens inside a loop that goes through every single raw record.

File: z_fico_tax_report.abap

* Loop through each raw record from the database
LOOP AT gt_final INTO DATA(gs_final).

" gs_sfinal is our "finished product" structure
CLEAR gs_sfinal.

" Copy all fields with the same name from raw to finished
MOVE-CORRESPONDING gs_final TO gs_sfinal.

" ... enrichment logic will go here ...
ENDLOOP.

For each raw record (gs_final), we prepare a new finished record (gs_sfinal) and use MOVE-CORRESPONDING as a quick way to copy over all the fields that exist in both structures, like BUKRS (Company Code) and BELNR (Document Number).

Step 2: Cleaning and Formatting the Data

One of our raw fields, shkzg, contains 'S' for a debit or 'H' for a credit. This is not user-friendly. Let's transform it into something a human can read.

File: z_fico_tax_report.abap

  " Transform the Debit/Credit indicator code
CASE gs_final-shkzg.
WHEN 'S'.
gs_sfinal-shkzg = 'Debit'.
WHEN 'H'.
gs_sfinal-shkzg = 'Credit'.
ENDCASE.

This simple CASE statement checks the raw value. If it's 'S', it puts the word 'Debit' into our finished record. If it's 'H', it puts 'Credit'. Just like that, our data is cleaner and easier to understand.

Step 3: Enriching with More Information

This is where we add real value. Our raw data contains the vendor number (lifnr), but our AMDP also efficiently fetched a separate list of vendor details (gt_vencus). Now, we can combine them.

We use the key of the current document to look up the matching vendor details.

File: z_fico_tax_report.abap

  " Find the vendor details for this specific document
READ TABLE gt_vencus INTO DATA(ls_ven)
WITH KEY bukrs = gs_final-bukrs
belnr = gs_final-belnr
gjahr = gs_final-gjahr.

" If we found a match, add the vendor's name!
IF sy-subrc = 0.
gs_sfinal-namel = ls_ven-namel. " Add Vendor Name
ENDIF.

This is a classic lookup. The READ TABLE command searches our gt_vencus helper table for an entry that matches the current document. If it finds one (sy-subrc = 0), it copies the vendor's name (namel) into our finished record. We have just enriched our data!

Step 4: Finalizing the Assembly

After cleaning, formatting, and enriching the record, the last step in the loop is to add our newly assembled "finished product" to the final display table.

File: z_fico_tax_report.abap

  " Add the new, polished record to our final table
APPEND gs_sfinal TO gt_sfinal.

ENDLOOP. " <-- The loop continues with the next raw record

This process repeats for every single line of raw data until our gt_sfinal table is full of beautiful, polished data, ready to be shown to the user in the ALV Grid Presentation.

The Transformation Flow

This diagram visualizes our data assembly line in action.

Conclusion

In this chapter, we learned about the crucial final step in data preparation: Data Transformation and Enrichment. We saw that raw data fetched from a database is rarely ready for users. It needs to be processed.

Key takeaways:

  • Transformation is like an assembly line that turns raw parts into a finished product.
  • We can clean data by formatting codes into human-readable text (like 'S' to 'Debit').
  • We can enrich data by looking up and adding related information (like a vendor's name).
  • This entire process happens in ABAP, looping through the raw data after it has been efficiently fetched by the AMDP.

This was the final piece of our puzzle. Congratulations! You've journeyed through the entire architecture of a modern SAP report. Let's quickly recap our journey:

  1. We started where the user does, defining the User Selection Criteria.
  2. We defined the "blueprints" for our data in the Report Data Model.
  3. We saw how the program's "conductor" uses VAT Report Orchestration to manage the entire process.
  4. We dove deep into the high-speed engine room for HANA Database Data Retrieval (AMDP).
  5. In this chapter, we polished the raw data with Data Transformation and Enrichment.
  6. Finally, this polished data is displayed to the user with the ALV Grid Presentation.

You now have a complete, end-to-end understanding of how this powerful VAT report is built. You've seen how each component has a specific job and how they all work together in harmony to deliver a fast, flexible, and user-friendly tool.