Sub topic 6
Measures, Currency and Unit Semantics, and Aggregation
This chapter assumes you can write a CDS view entity with associations and that you know the cube and dimension annotations from the previous chapter. You should also know SQL GROUP BY and aggregate functions.
Why measures need semantics
In SQL, SUM(net_amount) is just a number. In an analytical query, the number must also carry its currency or unit, and the system must know how to combine rows. Adding 100 EUR and 100 USD gives no meaningful result. Adding two stock levels over time may also be wrong.
CDS annotations provide this information. The analytical engine reads them at runtime. It builds the aggregation, groups by the fields on the rows, and keeps each amount linked to its currency.
Marking a field as a measure
A measure is a numeric field that you aggregate. In a cube view, a numeric field becomes a measure when you give it an aggregation behavior with @DefaultAggregation. This is the cube from the topic example:
@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
}Three annotations work together on NetAmount:
@DefaultAggregation: #SUMtells the engine to add the values when rows are grouped.@Semantics.amount.currencyCode: 'Currency'links the amount to the field that holds its currency key.@Semantics.currencyCode: trueonCurrencyidentifies that field as a currency key.
Aggregation types
The @DefaultAggregation annotation accepts several values.
| Value | Meaning | Typical use |
|---|---|---|
| `#SUM` | Adds all values | Revenue, quantity ordered |
| `#MIN` | Smallest value | Earliest price |
| `#MAX` | Largest value | Highest order value |
| `#NONE` | No aggregation | Fields that only display |
| `#FORMULA` | Calculated from other measures | Ratios, margins |
Use #FORMULA for ratios. Summing a percentage across rows gives a wrong result. A formula measure is computed after the other measures are aggregated, so the ratio is correct at every level of detail.
Units of measure
Quantities follow the same pattern as amounts. You identify the unit field and link the quantity to it:
@Semantics.unitOfMeasure: true
order_unit as OrderUnit,
@DefaultAggregation: #SUM
@Semantics.quantity.unitOfMeasure: 'OrderUnit'
order_qty as OrderQuantity,These two fields go inside the select list of a cube view, next to the amount fields.
How the engine handles mixed currencies
When a query groups by customer, one customer can have items in EUR and USD. The engine does not add them together. It returns one value per currency, or it shows the result as mixed, depending on the front end. Currency conversion must be defined explicitly, either in the query or in a view that converts to a target currency.
The flow below shows what happens when a query runs.
Common errors
- Missing semantics link. An amount without
@Semantics.amount.currencyCodeis treated as a plain number. Totals appear without a currency, and the activation check may report a warning. - Wrong reference. The string in
currencyCode: 'Currency'must match the element alias in the view, not the source column name. Here the alias isCurrency, notcurrencyfrom the table. - Summing a ratio. Percentages and averages with
#SUMgive nonsense at higher levels. Use a formula or a calculated measure in the query. - Aggregation on a non-cube view. The annotation only has analytical effect in views with
@Analytics.dataCategory: #CUBE, or in queries built on them. - Unit mixed with currency. A quantity must reference a unit field and an amount must reference a currency field. Swapping them causes a syntax error.
Exercise
Exercise
A cube view has the field Margin with @DefaultAggregation: #SUM. It holds the ratio profit divided by revenue for each item. A query shows Margin by customer, and the values look too large. What is the cause, and how do you fix it?
Show answer
The engine adds the ratios of all items for each customer, so the result is the sum of percentages and not a true margin. Remove the stored ratio as a summed measure. Expose profit and revenue as separate #SUM measures, then define Margin as a formula (#FORMULA) or a calculated measure in the query. The division then runs after profit and revenue are aggregated.