Sub topic 15
Embedded analytics and CDS views
This chapter assumes you know the module landscape from earlier chapters, can read basic SQL (joins, GROUP BY), and have seen the Universal Journal (ACDOCA) and the sales tables VBAK and VBAP. It does not assume you have written ABAP or CDS before.
What "embedded" means
Embedded analytics is reporting that runs inside the S/4HANA system, directly on the live transactional tables. There is no extract job and no copy of the data in a separate warehouse. A report on open sales orders reads the same VBAK and VBAP rows that order entry just wrote.
This works for two reasons:
- SAP HANA is an in-memory, column-oriented database. It can aggregate millions of rows at query time.
- S/4HANA removed most stored totals and index tables. Finance, for example, keeps line items in ACDOCA and calculates balances when you ask for them.
The building block is the CDS view. CDS stands for Core Data Services. A CDS view is a named, reusable definition of a query that lives in the ABAP development environment. The database runs it each time something selects from it.
The anatomy of a CDS view
Current S/4HANA releases use the CDS view entity. You define the view with define view entity, give it a source, and list the fields to expose. The example below joins sales order headers and items, as in the Topic Spine query, and adds the annotations that analytics tools need.
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Order item net value'
define view entity ZI_OrderItemValue
as select from vbak
inner join vbap on vbap.vbeln = vbak.vbeln
{
key vbak.vbeln as SalesOrder,
key vbap.posnr as SalesOrderItem,
vbak.kunnr as SoldToParty,
vbak.vkorg as SalesOrganization,
vbap.matnr as Material,
@Semantics.currencyCode: true
vbak.waerk as TransactionCurrency,
@Semantics.amount.currencyCode: 'TransactionCurrency'
vbap.netwr as NetAmount
}Some points to notice:
- The
keyfields declare the unique identity of a row. Here a row is one order item, so the key is order number plus item number. - The aliases (
SalesOrder,NetAmount) replace the old abbreviated column names. A view's job is partly to give technical tables readable names. @Semantics.amount.currencyCodeties the amount to its currency field. Without it, a tool cannot know thatNetAmountis money and cannot convert or format it.- The view has no
@Clienthandling to write. The framework restricts it to the current client automatically.
You activate the view in ABAP Development Tools (ADT) in Eclipse. Activation creates a database view that you can then select from like a table.
REPORT zdemo_cds_totals.
SELECT SoldToParty,
TransactionCurrency,
SUM( NetAmount ) AS Total
FROM zi_orderitemvalue
WHERE SalesOrganization = '1010'
GROUP BY SoldToParty, TransactionCurrency
INTO TABLE @DATA(totals).
cl_demo_output=>display( totals ).The SUM and GROUP BY run inside the database. The report receives only the grouped result, which is the "code pushdown" idea behind HANA. Grouping by currency matters: adding amounts in different currencies produces a meaningless number.
The virtual data model and its layers
SAP delivers thousands of CDS views and organizes them as the Virtual Data Model (VDM). The VDM layers views so that each layer has one job.
| Layer | Naming prefix | Purpose | Example use |
|---|---|---|---|
| Basic (interface) | `I_` | Exposes one business entity with clean names, texts, and associations | `I_JournalEntryItem` over ACDOCA |
| Composite | `I_` | Combines basic views, adds calculations | Sales order items with customer and material |
| Consumption | `C_` | Shaped for one app, report, or API | A Fiori app's data source |
| Extension and custom | Customer namespace | Your own views, such as `ZI_...` | The view above |
The layering has a practical effect. If the table structure changes between releases, SAP adjusts the basic view and everything above it keeps working. This stability is why released CDS views, not raw tables, are the supported way to read S/4HANA data.
Associations, not just joins
A CDS view can declare an association, which is a join that runs only when a field from the target is actually used. Basic views use associations to expose relationships without paying for them on every query.
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Order with customer'
define view entity ZI_OrderWithCustomer
as select from vbak
association [0..1] to kna1 as _Customer
on _Customer.kunnr = vbak.kunnr
{
key vbak.vbeln as SalesOrder,
vbak.kunnr as SoldToParty,
_Customer
}Exposing _Customer lets a consumer write a path expression such as _Customer.name1. The database then joins KNA1 only for queries that touch it. A hard-coded inner join, by contrast, is always executed, and an inner join can also drop rows that have no match.
Analytical views and queries
Analytics needs more than a list of rows. It needs to know which fields are measures (numbers to add up) and which are dimensions (things to group by). CDS expresses this with the @Analytics.dataCategory annotation:
#CUBEmarks a view of facts with measures and links to dimensions, for example journal entry items.#DIMENSIONmarks master-data views such as customer or G/L account, which carry texts and hierarchies.#FACTmarks a plain fact view used as a source.
On top of a cube you define an analytical query, either with @Analytics.query: true in a consumption view or through the Custom Analytical Queries Fiori app, which generates the CDS view for you. The query fixes which characteristics appear in rows and columns, which filters are available, and how totals are calculated.
Consumers of these queries include:
- Fiori analytical apps and tiles that show KPIs.
- The Query Browser app, which lets a business user pick any released query and view the results.
- Analytical list pages, which combine a chart, filters, and a table.
- External tools such as SAP Analytics Cloud, which connect live to the same queries.
For Finance, I_JournalEntryItem reads the Universal Journal. Because FI and CO postings share one table, a single query can show an amount by G/L account and cost center together. In the older design this took a reconciliation between separate FI and CO tables.
Where embedded analytics stops
Embedded analytics suits operational reporting: what is open, late, or posted today. It has limits:
- Heavy queries use the same HANA resources as transactions. A badly written view can slow order entry.
- It sees only data in this system. Cross-system reporting, long history, or data from non-SAP sources usually belongs in SAP Datasphere or SAP BW/4HANA.
- Query results reflect the live state. If you need a frozen snapshot for a closed period, you must store it.
Authorizations and common errors
CDS views do not inherit transaction-level authorization checks automatically. Access control is its own object, a Data Control Language (DCL) role that restricts rows by authorization object, such as company code or sales organization. The example views above use #NOT_REQUIRED only to stay short.
Typical problems:
- Double counting from fan-out. Joining a header amount to its items repeats the header value once per item. Keep header-level measures out of item-level views, or aggregate first.
- Summing across currencies or units. Always group by the currency or unit field, or convert with
currency_conversion. - Reading tables directly in cloud scenarios. In Cloud Public Edition and in clean-core development, only released views and APIs are allowed. Select from
I_...views instead of VBAK and VBAP. - Aggregating in ABAP loops. Fetching all rows and adding them in a loop discards the pushdown. Use
SUMandGROUP BYin theSELECT. - Forgetting
keyfields. A view whose keys do not identify a row uniquely gives wrong results in analytical tools.
How this ties back to the rest of the system
The CDS layer sits between the shared data (ACDOCA, sales documents, material and business partner tables) and everything that displays it. Fiori apps read consumption views. APIs expose them as OData services. Custom extensions add fields and views on top without modifying SAP code, which is the subject of the next chapter.
Exercise
A colleague writes a CDS view joining VBAK (header, with a net value field) to VBAP (items), exposes the header net value as a measure, and reports total revenue per customer. The total comes out far too high. What is the likely cause, and how would you fix it?
Show answer
The header net value repeats on every item row of the join. Summing it per customer adds the same header amount once per item, which is fan-out double counting. Fix it by summing the item-level net value (VBAP-NETWR) instead, or by aggregating the items to header level before joining. Also group by currency, so amounts in different currencies are not added together.