Sub topic 4
Analytical annotations and data categories
This chapter assumes you can read basic ABAP CDS view entities, including associations, and that you know the Virtual Data Model layers from the previous chapters. Here you learn how annotations turn an ordinary CDS view into an analytical building block.
Why annotations matter
A CDS view entity is a plain SQL-like definition. The analytics engine cannot tell from the SQL alone whether a field is a measure to sum or an attribute to group by. Annotations carry that meaning. The most important one is @Analytics.dataCategory, which declares the role of the whole view.
The data categories
The @Analytics.dataCategory annotation accepts these values:
| Value | Role | Typical content |
|---|---|---|
| `#CUBE` | Fact data with measures | Sales items, journal entries |
| `#DIMENSION` | Master data that describes facts | Customer, product, company code |
| `#FACT` | Fact data with measures, but without dimension links | Simple transactional views |
| `#AGGREGATIONLEVEL` | Planning scenarios | Not covered in this tutorial |
Queries are built on top of cubes. Dimensions are reached from cubes through associations. A view without a data category is treated as a normal CDS view and cannot serve as an analytical source.
Annotating a dimension
A dimension has a key, optional text and attributes. The following view is the one used throughout this tutorial.
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Customer'
@Analytics.dataCategory: #DIMENSION
define view entity ZI_CustomerDim
as select from zcustomer
{
key customer_id as CustomerId,
@Semantics.text: true
customer_name as CustomerName,
country as Country
}Three annotations do the work here:
@Analytics.dataCategory: #DIMENSIONmarks the view as master data for analysis.@Semantics.text: truetells consumers thatCustomerNameis the description of the key. Front ends can then show the name next to, or instead of, the ID.@EndUserText.labelprovides the title that users see.
The table zcustomer is hypothetical. Replace it with a table from your own system.
Annotating a cube
A cube holds the facts. It connects to dimensions and defines measures.
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Sales item cube'
@Analytics.dataCategory: #CUBE
define view entity ZI_SalesItemCube
as select from zsales_item
association [0..1] to ZI_CustomerDim as _Customer
on $projection.CustomerId = _Customer.CustomerId
{
key item_uuid as ItemUuid,
@ObjectModel.foreignKey.association: '_Customer'
customer_id as CustomerId,
@Semantics.currencyCode: true
currency as Currency,
@DefaultAggregation: #SUM
@Semantics.amount.currencyCode: 'Currency'
net_amount as NetAmount,
_Customer
}Each annotation has a distinct job:
@ObjectModel.foreignKey.associationnames the association that leads fromCustomerIdto the dimension. The engine uses it to join the dimension and to offer its attributes in queries.@Semantics.currencyCode: trueidentifies the currency field.@Semantics.amount.currencyCodelinks the amount to that currency field by name. The value is a string that must match the field alias exactly.@DefaultAggregation: #SUMstates how the measure is aggregated when rows are grouped.
The association _Customer is also exposed in the select list. Without that, consumers cannot navigate to the dimension.
Common errors
- Missing foreign key annotation. The cube activates, but the dimension's fields do not appear in the query. Check
@ObjectModel.foreignKey.association. - Wrong currency reference. If the string in
@Semantics.amount.currencyCodedoes not match the alias, activation fails or amounts are not shown with their currency. The alias isCurrency, notcurrency, so match the case you used in the select list. - Text annotation on the wrong field.
@Semantics.textbelongs on the description field, not on the key. - Annotating a view that is not a view entity. Use
define view entity. The olderdefine viewwith@AbapCatalogannotations is outdated and is not used here.
Summary of roles
| Annotation | Placed on | Effect |
|---|---|---|
| `@Analytics.dataCategory` | View | Declares cube or dimension |
| `@Semantics.text` | Field | Marks the description of a key |
| `@ObjectModel.foreignKey.association` | Field | Links a cube field to a dimension |
| `@Semantics.currencyCode` | Field | Marks the currency field |
| `@Semantics.amount.currencyCode` | Field | Ties an amount to its currency |
| `@DefaultAggregation` | Field | Sets the aggregation of a measure |
Exercise
A cube activates without errors, but the customer name is missing when you build a query on it. Name two annotation-related causes to check.
Show answer
First, the field CustomerId may lack @ObjectModel.foreignKey.association: '_Customer', so the engine does not join the dimension. Second, the association _Customer may not be exposed in the cube's select list, or CustomerName in the dimension may lack @Semantics.text: true.
Exercise
Which data category do you assign to a view that lists products and their descriptions, and which annotation marks the description field?
Show answer
Use @Analytics.dataCategory: #DIMENSION, and put @Semantics.text: true on the description field.