Sub topic 4
Analytical annotations and data categories
This chapter explains how annotations turn a plain CDS view entity into an analytical object. It assumes you can read a CDS view entity with associations, that you know the interface, composite and consumption layers of the Virtual Data Model, and that you have basic SQL skills. Modeling the cube and dimension in full comes in the next chapter. Here the focus is on what each annotation means, where it goes, and which errors it prevents.
Why annotations carry the analytical meaning
A CDS view entity describes a SELECT. By itself it does not say which fields are measures, which are descriptive attributes, or how values should be added up. The database only sees columns.
Annotations are metadata attached to the view or to its fields. They begin with @. The analytical engine reads them at runtime to decide how to treat each field. The same SELECT with different annotations can be a dimension, a cube or a query.
Three groups of annotations matter in this chapter:
- View-level annotations that set the data category, such as
@Analytics.dataCategory. - Field-level semantic annotations, such as
@Semantics.amount.currencyCodeand@Semantics.text. - Aggregation and relationship annotations, such as
@DefaultAggregationand@ObjectModel.foreignKey.association.
Data categories
The annotation @Analytics.dataCategory tells the engine what role the view plays. It sits above the define view entity line. These are the values you will meet most often.
| Value | Role | Typical content |
|---|---|---|
| `#DIMENSION` | Master data with keys, texts and attributes | Customer, product, country |
| `#CUBE` | Transactional data with measures and links to dimensions | Sales items, journal entries |
| `#FACT` | Transactional data with measures that is not a full cube | Fact data without dimension associations |
| `#AGGREGATIONLEVEL` | A fixed grouping level used by planning scenarios | Rarely used in reporting |
In reporting work you will almost always choose #DIMENSION or #CUBE. Query views do not use a data category. They use @Analytics.query: true, which is covered later in this chapter.
A dimension
The first example is a dimension view entity over a hypothetical custom table zcustomer.
@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
}Read the annotations in order:
@AccessControl.authorizationCheck: #NOT_REQUIREDstates that no access control is attached. Master data such as a customer list is often open, but check your own security rules before copying this.@EndUserText.labelgives the view a readable name that front ends can show.@Analytics.dataCategory: #DIMENSIONmarks the view as master data for analysis.@Semantics.text: truemarksCustomerNameas the text for the key. A report can then show "Contoso GmbH" instead of "100023".
A dimension needs a key. The key identifies one member, such as one customer.
A cube
The second example is a cube over a hypothetical table zsales_item.
@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
}Here @Analytics.dataCategory: #CUBE tells the engine that the view holds facts. The engine treats numeric fields with an aggregation as measures. It treats fields linked through foreign key associations as dimension references. The line _Customer in the field list exposes the association so that consumers can follow it.
Field-level semantic annotations
Semantic annotations describe what a field means. The engine uses them to behave correctly, not only to display values.
Text annotations
@Semantics.text: true marks a text field. A text field is paired with a key field so that front ends can show the description next to, or instead of, the key. In the dimension above, CustomerName is the text for CustomerId.
Texts that depend on a language need a language field and a separate text view. That pattern is part of the dimension work in the next chapter.
Currency and unit annotations
Amounts are meaningless without a currency. Quantities are meaningless without a unit. Two annotations connect them:
@Semantics.currencyCode: truemarks the field that holds the currency key.@Semantics.amount.currencyCode: 'Currency'marks an amount field and names the currency field it belongs to. The name is the alias used in the view, written as a string.
For quantities, the equivalent pair is @Semantics.unitOfMeasure: true and @Semantics.quantity.unitOfMeasure: '<unit field alias>'.
The pairing is what stops the engine from adding 100 US dollars to 100 Japanese yen. When rows have different currencies, the engine keeps them apart or converts them when conversion is defined. The next chapter, on measures, covers conversion.
Aggregation
@DefaultAggregation sets how a measure is combined when rows are grouped. Common values are:
#SUMadds the values.#MINand#MAXreturn the smallest or largest value.#AVGreturns the average, available in some releases and view contexts.#COUNTcounts rows. It is typically used on a helper field.#NONEturns off aggregation for a field.
Think of the SQL equivalent. A query with customer on the rows and NetAmount as a measure behaves like this statement:
SELECT customer_id, currency, SUM(net_amount) AS net_amount
FROM zsales_item
GROUP BY customer_id, currency;The engine builds this kind of statement for you. The group-by columns come from the fields placed on the axes, and the aggregate function comes from @DefaultAggregation. Currency is grouped alongside, because the amount depends on it.
Choosing an aggregation
Choose the aggregation by asking what the number means when you merge two rows.
- A net amount can be added, so use
#SUM. - A unit price cannot be added meaningfully. Use
#NONEor#AVG, or avoid exposing it as a measure. - A date should never be summed. Leave it without an aggregation so it behaves as an attribute.
Foreign key associations
@ObjectModel.foreignKey.association: '_Customer' appears on the field CustomerId in the cube. It tells the engine that this field is the foreign key to the dimension reachable through _Customer. With this link the engine can:
- Offer the customer's attributes and text when a user builds a report on the cube.
- Join the dimension only when its fields are used, which saves work.
Without the annotation the association still exists in the CDS model, but the analytical engine does not treat it as a dimension relationship. A frequent symptom is that the customer name does not appear in the report even though the dimension view has a text field.
Query annotations
The third view is a consumption-layer query.
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Net amount by customer'
@Analytics.query: true
define view entity ZC_NetAmountByCustomer
as select from ZI_SalesItemCube
{
@AnalyticsDetails.query.axis: #ROWS
CustomerId,
@AnalyticsDetails.query.axis: #COLUMNS
NetAmount
}@Analytics.query: true marks the view as a query. It reads from the cube, not from a table. @AnalyticsDetails.query.axis places a field on an axis. #ROWS puts the customer down the left side. #COLUMNS puts the measure across the top. Fields without an axis annotation stay available for the user to add later.
A query is the object that front ends call. Chapter 7 builds queries in detail, including filters and variables.
Where annotations can go wrong
| Symptom | Likely cause | Fix |
|---|---|---|
| View activates but does not appear in analytical apps | Missing `@Analytics.dataCategory` or `@Analytics.query` | Add the correct annotation to the correct layer |
| Amount shows without a currency | `@Semantics.amount.currencyCode` points to the wrong alias | Use the alias of the currency field |
| Customer ID shows but not the name | No `@Semantics.text` on the dimension, or no `@ObjectModel.foreignKey.association` in the cube | Add the missing annotation |
| Totals look wrong | Wrong `@DefaultAggregation`, such as summing a price | Pick an aggregation that fits the field's meaning |
| Data is visible to everyone | `#NOT_REQUIRED` used on a view with sensitive rows | Use `#CHECK` and add an access control |
Annotations and the layers
Annotations belong to specific layers of the Virtual Data Model.
- Interface views carry the data category and the semantic annotations. These are the cube and dimension views.
- Composite views combine interface views. They inherit analytical meaning and may add calculated fields.
- Consumption views carry
@Analytics.queryand the axis annotations. They are what apps call.
Putting @Analytics.query on an interface view mixes layers. It works technically but makes reuse harder, because other consumers cannot build their own queries on a view that is already a query.
Checking your work
After activation, test the model in this order:
- Open the dimension in the data preview and confirm that the key and text are shown.
- Preview the cube and check that the measure aggregates and that the currency is shown.
- Preview the query and check that the rows, columns and totals match a SQL check against the base table.
Doing it in this order isolates the layer where a problem starts.
Exercise
A cube view has a field net_amount as NetAmount with @DefaultAggregation: #SUM, but no @Semantics.amount.currencyCode annotation. A colleague says this is fine because the SUM already gives the right total. What is the risk, and what would you add?
Show answer
The engine does not know which currency the amount is in, so it may add values in different currencies or show a number with no unit. Add a currency field with @Semantics.currencyCode: true (for example currency as Currency) and annotate the amount with @Semantics.amount.currencyCode: 'Currency'.
Exercise
A report built on your cube shows customer numbers but never customer names, although the dimension ZI_CustomerDim has @Semantics.text: true on CustomerName. Name two annotations or declarations you should check in the cube.
Show answer
Check that the cube field CustomerId has @ObjectModel.foreignKey.association: '_Customer', and that the association _Customer is exposed in the cube's field list. Also confirm that the association condition joins on the dimension key.
Exercise
Which data category would you assign to a view of products with a key, a description and a product group, and which to a view of order items with a quantity and a net amount?
Show answer
The product view is a #DIMENSION. The order item view is a #CUBE.