Sub topic 5
Modeling a Cube and Its Dimensions
This chapter assumes you know basic SQL (joins, GROUP BY, aggregates) and can read a CDS view entity with associations and annotations. It also assumes you know the interface, composite and consumption layers of the Virtual Data Model. Here you build the core of an analytical model: a cube that holds facts, and the dimensions that describe them.
What a cube and a dimension are
A cube is a CDS view entity that holds the facts of a business process at a fixed level of detail. A sales item cube has one row per sales item, with measures such as net amount and fields that point to descriptive objects such as the customer.
A dimension is a CDS view entity that describes one of those objects. It has a key, optional text and attributes. A customer dimension has a customer ID, a name and a country.
This is a star schema, expressed as CDS views. The difference from a classic warehouse is that nothing is copied. The cube reads the live transactional tables when a query runs.
The data category annotation tells the analytical engine which role a view plays:
| Category | Annotation | Role |
|---|---|---|
| Cube | `@Analytics.dataCategory: #CUBE` | Facts with measures and links to dimensions |
| Dimension | `@Analytics.dataCategory: #DIMENSION` | Master data with key, text and attributes |
| Fact | `@Analytics.dataCategory: #FACT` | Facts without dimension links, for simpler cases |
| Not set | none | Plain CDS view with no analytical meaning |
Modeling the dimension
Start with the dimension, because the cube refers to it. This is the view from the topic plan, over the 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
}Three things matter here.
- The key identifies the member.
customer_idis the key. Every dimension needs a key that uniquely identifies one member. If the key is not unique, the cube joins to several rows and the measures are counted several times. - The text field has an annotation.
@Semantics.text: truemarksCustomerNameas a description. Front ends can then show "Acme Corp" next to or instead of the ID. - The other fields are attributes.
Countryis a plain attribute. Users can put it on an axis or filter by it.
@AccessControl.authorizationCheck: #NOT_REQUIRED is common on dimensions because master data is usually not restricted. Check this against your own authorization concept. Chapter 8 covers access controls.
Texts in several languages
A name in one column works for a single-language table. Most SAP master data keeps texts in a separate table with a language key. In that case, model a text view and link it with an association.
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Customer text'
@ObjectModel.dataCategory: #TEXT
define view entity ZI_CustomerText
as select from zcustomer_t
{
key customer_id as CustomerId,
@Semantics.language: true
key langu as Language,
@Semantics.text: true
customer_name as CustomerName
}The dimension then points to the text view. It annotates its key with @ObjectModel.text.association and exposes the association.
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Customer'
@Analytics.dataCategory: #DIMENSION
define view entity ZI_CustomerDim
as select from zcustomer
association [0..*] to ZI_CustomerText as _Text
on $projection.CustomerId = _Text.CustomerId
{
@ObjectModel.text.association: '_Text'
key customer_id as CustomerId,
country as Country,
_Text
}The table zcustomer_t is also hypothetical. The runtime picks the text row that matches the logon language of the user.
Modeling the cube
The cube holds the facts. This is the view from the topic plan, over the 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
}Each part has a purpose.
- The association
_Customerhas cardinality[0..1]. One cube row has at most one customer. The cardinality is a promise to the engine. If it is wrong and a customer appears twice in the dimension, the cube row count multiplies. @ObjectModel.foreignKey.association: '_Customer'marksCustomerIdas a foreign key to the dimension. This is what makesCustomerIda dimension field. Without it, the analytical engine sees an ordinary field and cannot navigate to the customer's name or country._Customerin the select list exposes the association. A query on top of the cube can then use_Customer.Countrywithout a join. If you leave it out, the link exists in the view but consumers cannot reach it.- The measure
NetAmounthas@DefaultAggregation: #SUM. Measures and the currency and unit rules are the subject of the next chapter. For now, note that the amount is tied toCurrencyand that the aggregation is declared on the field. - The key
ItemUuidmakes each row unique at the item level, which is the granularity of the cube.
On newer releases, you may also set @ObjectModel.modelingPattern: #ANALYTICAL_CUBE and @ObjectModel.supportedCapabilities: [#ANALYTICAL_PROVIDER] on the cube. A dimension uses #ANALYTICAL_DIMENSION with the capabilities #ANALYTICAL_DIMENSION and #CDS_MODELING_ASSOCIATION_TARGET. Which annotations your system expects depends on its release, so check the CDS annotation documentation for your version.
Choosing the granularity
Granularity is the level of detail of one cube row. Choose it before you write any code, because it limits every question the cube can answer.
- A cube at the item level can answer questions by product, customer and day. It can also be rolled up to the order level.
- A cube at the order header level cannot be broken down by product.
Model the lowest level that users need, and let aggregation roll up from there. Avoid building the same cube at several levels. Instead, keep one detailed cube and let queries aggregate.
Be careful when you add header data to an item-level cube. A header amount repeated on every item row would be summed once per item. Keep header-level measures out of item cubes, or allocate them to items first.
Associations to several dimensions
A real cube links to many dimensions. Each one needs its own association, its own foreign key annotation, and exposure in the select list.
association [0..1] to ZI_CustomerDim as _Customer
on $projection.CustomerId = _Customer.CustomerId
association [0..1] to ZI_ProductDim as _Product
on $projection.ProductId = _Product.ProductIdZI_ProductDim is another hypothetical dimension. The pattern repeats. Keep the number of dimension associations in line with the questions the cube must answer. Every dimension that you expose becomes part of the API of the cube, so it is harder to remove later.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Customer name or country is not offered in the query | `_Customer` is not in the select list of the cube | Add `_Customer` to the select list |
| Dimension fields not recognized for a field | Missing `@ObjectModel.foreignKey.association` | Add the annotation to the foreign key field |
| Totals are too high | Dimension has duplicate keys, or a header amount is repeated on item rows | Make the dimension key unique, or move the header measure |
| Activation error on the amount field | Amount has no currency reference | Add `@Semantics.currencyCode: true` to the currency field and reference it |
| Text shows as an ID | Text field not annotated, or text association not set on the key | Add `@Semantics.text: true` and `@ObjectModel.text.association` |
| Query cannot be built on the view | The view has no data category | Set `@Analytics.dataCategory: #CUBE` |
A quick check with SQL thinking
Because you know SQL, you can reason about what the engine does. A query on the cube that shows net amount by customer country behaves roughly like this:
SELECT d.country, SUM(c.net_amount) AS net_amount
FROM zsales_item AS c
LEFT OUTER JOIN zcustomer AS d
ON c.customer_id = d.customer_id
GROUP BY d.country, c.currencyTwo details follow from this. The join is an outer join, so items without a customer still count. Currency is part of the grouping, because amounts in different currencies must not be added together. The engine adds this on its own because of the currency annotation.
Practical checklist
- Decide the granularity of the cube and name the key that makes each row unique.
- Create the dimension views first, each with a unique key, a text and the attributes users need.
- Create the cube with an association per dimension and a foreign key annotation per link.
- Expose every association in the select list.
- Put the measures on the cube with currency or unit references and an aggregation.
- Test totals against the source tables before building queries.
Exercise
In the cube ZI_SalesItemCube, you remove _Customer from the select list but leave the association definition and the foreign key annotation in place. The view still activates. What changes for a query built on top of the cube, and why?
Show answer
The query can no longer reach customer fields such as the name or the country. An association is only usable by consumers when the view exposes it in its select list. The foreign key annotation still points to _Customer, but the dimension is not visible from outside the cube.
Exercise
A cube row represents a sales item. A colleague adds the order header field header_discount_amount to the cube as a measure with #SUM. Totals by customer now look too high. What is the cause, and what are two ways to fix it?
Show answer
The header value is repeated on every item row of the same order, so the sum counts it once per item. Fix it by removing the header measure from the item-level cube, or by allocating the discount to the items so that the item values add up to the header value.
Exercise
Why does a dimension need a unique key, and what happens to a cube's measures if the key is not unique?
Show answer
The cube joins to the dimension through the key. If a key value appears in more than one dimension row, one cube row matches several rows, and its measures are counted several times. A unique key keeps the join one-to-one from the cube side, which also matches the declared [0..1] cardinality.