Sub topic 5
Modeling a Cube and Its Dimensions
This chapter assumes you can read basic ABAP CDS view entities, know what an association is, and have seen the @Analytics.dataCategory annotation. You should also know the difference between facts and master data from basic SQL and data modeling.
The star schema in CDS
An analytical model in CDS follows a star schema. A cube holds the facts: one row per business event, such as a sales order item. It carries measures (numbers to add up) and foreign keys. A dimension holds master data that describes those facts, such as a customer. The cube points to each dimension through an association.
The query layer later reads only the cube. It follows the associations to reach dimension attributes when a user needs them.
Step 1: Define the dimension
A dimension is a CDS view entity with @Analytics.dataCategory: #DIMENSION. It needs a key. Add a text field and mark it with @Semantics.text: true, so front ends can show names instead of IDs.
@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
}CustomerId is the key. CustomerName is the text for that key. Country is an attribute, which a user can use to group or filter. The table zcustomer is a hypothetical custom table used for this example.
Step 2: Define the cube
The cube uses @Analytics.dataCategory: #CUBE. Three things make it work:
- A key that identifies each fact row.
- A foreign key annotation that ties each dimension field to its association.
- Measures with an aggregation rule.
@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
}The annotation @ObjectModel.foreignKey.association: '_Customer' tells the analytical engine that CustomerId is the join field for _Customer. The engine can then read customer attributes and text only when a query asks for them. Without it, the dimension is not recognized as linked.
The association is also exposed in the select list (_Customer). If you leave it out, consumers cannot navigate to the dimension.
Measures need two things. @DefaultAggregation: #SUM says how to combine rows. @Semantics.amount.currencyCode: 'Currency' binds the amount to its currency field, so the engine does not add amounts in different currencies without handling them. The next chapter covers this in more detail.
Design choices
| Decision | Recommendation |
|---|---|
| Cube key | Use the unique key of the fact row, such as a UUID or document and item. |
| Dimension reuse | Build one dimension view and link it from many cubes. |
| Cardinality | Use `[0..1]` for a foreign key association to a dimension, so missing master data does not drop fact rows. |
| Text | Mark one text field per dimension key with `@Semantics.text`. |
| Authorization | Set `#CHECK` on the cube and pair it with an access control. |
Common errors
- Missing data category. Without
@Analytics.dataCategory, the view is a plain CDS view and does not behave as a cube or dimension in analytical tools. - Missing foreign key annotation. The association exists, but the engine does not use it for dimension access.
- Measure without aggregation. A numeric field with no
@DefaultAggregationmay be treated as an attribute, not a measure. - Amount without a currency link. Activation warnings or wrong totals follow when
@Semantics.amount.currencyCodepoints to nothing. - Dimension with no key. Every dimension view needs a key field.
Exercise
In the cube ZISalesItemCube, what does the annotation @ObjectModel.foreignKey.association: 'Customer' on CustomerId do, and why is _Customer also listed in the select list?
Show answer
The annotation tells the analytical engine that CustomerId is the join field for the Customer association, so it can read customer attributes and text from the dimension. Listing Customer in the select list exposes the association to consumers such as query views, so they can navigate to the dimension.
Exercise
You want to add a Country filter to reports on the cube. Which view should hold the Country field, and what must the cube provide so queries can use it?
Show answer
The dimension view ZICustomerDim holds Country as an attribute. The cube must expose the Customer association and mark CustomerId with the foreign key annotation, so queries can reach Country through the dimension.