In ABAP CDS, a special handling for amounts and quantities is implemented. If differs from DDIC amount and quantity handling and it has recently been overhauled. Read this blog post and learn about ABAP CDS amount and quantity fields, conversion functions, and handling of amounts and quantities in expressions.
Contents:
Further details: ABAP CDS – Amount Fields on SAP Help Portal.
Further details: ABAP CDS – Quantity Fields on SAP Help Portal.
This is a new feature that was first released with kernel release 7.84, SAP BTP ABAP Environment 2105, ABAP release 7.56. It is available in CDS view entities and in CDS projection views.
Further details:
Example
Arithmetic expression | Result |
770 € (Amount) / 100m² (Quantity) | 7,70€/m² (Calculated Quantity) |
The arithmetic expression 770€ / 100 m2 calculates cost per square meter. It divides an amount by a quantity. The result of this calculation is a calculated quantity that has calculated unit reference assigned. This calculated unit is not registered in the unit check table T006.
Example
In the following CDS view entity, the field rent_per_size divides the amount of the rent for an apartment by the apartment size. The result is the cost per square meter.
@AccessControl.authorizationCheck: #NOT_ALLOWED
@EndUserText.label: 'CDS view entity, calculated quantity'
define view entity DEMO_CDS_CALCULATED_QUANTITY
as select from demo_rent
{
key apartment_id as ApartmentId,
apartment_size as ApartmentSize,
apartment_unit as ApartmentUnit,
currency as Currency,
// currency field and unit field in arith expression
@Semantics.quantity.unitOfMeasure: 'calculatedUnit'
curr_to_decfloat_amount(rent_curr) / apartment_size
as rent_per_size,
concat( concat(currency, '/' ), apartment_unit )
as calculatedUnit
}
If data is inserted into the underlying database talbe demo_rent, the data preview might look as follows:
In analytical projection views, the calculated quantity and the calculated unit can be defined as virtual elements using the keyword VIRTUAL, as shown in the following example.
@EndUserText.label: 'CDS projection view, analytical query'
@AccessControl.authorizationCheck: #NOT_ALLOWED
define transient view entity DEMO_ANALYTICAL_QUERY_ELEM
provider contract analytical_query
as projection on DEMO_CDS_CUBE_VIEW
{
@AnalyticsDetails.query.axis: #COLUMNS
@Semantics.amount.currencyCode: 'currency'
curr_to_decfloat_amount( paymentsum ) as paymentsum,
virtual unitPrice : abap.char( 10 ),
@EndUserText.label: 'price'
@Aggregation.default: #FORMULA
@AnalyticsDetails.query.axis: #COLUMNS
@Semantics.quantity.unitOfMeasure: 'unitPrice'
$projection.paymentsum / $projection.distance as price
}
Function | Effect |
UNIT_CONVERSION( quantity => arg1, source_unit => arg2, target_unit => arg3[, client => arg4][, error_handling => arg5]) |
|
CURRENCY_CONVERSION( amount => arg1, source_currency => arg2, target_currency => arg3, exchange_rate_date => arg4[, exchange_rate_type => arg5][, client => arg6][, round => arg7][, decimal_shift => arg8][, decimal_shift_back => arg9][, error_handling => arg10]) |
|
GET_NUMERIC_VALUE( arg ) |
|
CURR_TO_DECFLOAT_AMOUNT( arg ) |
|
Note: DECIMAL_SHIFT is available in the obsolete CDS DDIC-based views. It is not available in the “new world” of CDS view entities.
Further info on SAP Help Portal, Unit and Currency Conversion Functions
Example
AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'CDS view entity, GET_NUMERIC_VALUE'
@Metadata.ignorePropagatedAnnotations: true
define view entity DEMO_CDS_GET_NUMERIC_VALUE
as select from sflight
{
key carrid,
key connid,
key fldate,
@Semantics.amount.currencyCode: 'currency'
price,
currency,
@Semantics.amount.currencyCode: 'currency'
paymentsum,
GET_NUMERIC_VALUE( paymentsum )
/ GET_NUMERIC_VALUE( price ) as number_of_bookings
}
In the "new world" of CDS view entities, amounts and quantities are considered in expressions, union views, and intersect views. Special rules apply how amounts and quantities can be combined:
Note: Cast expressions do not handle amounts. Only the target type CURR requires a reference to a currency key.
Release info: available since kernel release 7.84, SAP BTP ABAP Environment 2105, ABAP release 7.56
operand / operator | / | * | +, - |
amount, amount | calculated quantity | calculated quantity | amount |
quantity, quantity | calculated quantity | calculated quantity | quantity |
calculated quantity, amount | calculated quantity | calculated quantity | amount |
amount, quantity | calculated quantity | calculated quantity | - |
calculated quantity, number | calculated quantity | calculated quantity | - |
calculated quantity, quantity, | calculated quantity | calculated quantity | quantity |
calculated quantity, calculated quantity | calculated quantity | calculated quantity | calculated quantity |
amount, number | amount | amount | - |
number, amount | calculated quantity | amount | - |
quantity, number | quantity | quantity | - |
number, quantity | calculated quantity | quantity | - |
Further details: Amounts and Quantities in Arithmetic Expressions on SAP Help Portal
Release info: available since kernel release 7.84, SAP BTP ABAP Environment 2105, ABAP release 7.56
If the operand of an aggregate function is a CDS amount field, a CDS quantity field, or a CDS calculated quantity, the result type might require a reference annotation as well. The following table shows the result type depending on the operand type of all available aggregate functions.
Aggregate Function | Type of Operand | Result Type |
MAX, MIN, SUM, AVG | amount | amount |
MAX, MIN, SUM, AVG | quantity | quantity |
MAX, MIN, SUM, AVG | calculated quantity | calculated quantity |
COUNT | amount, quantity, calculated quantity | number of type INT4 |
Further details: Amounts and Quantities in Aggregate Expressions on SAP Help Portal.
Release info: available since kernel release 7.84, SAP BTP ABAP Environment 2105, ABAP release 7.56
The result data type of a case expression (simple and complex) is determined by all THEN branches and the ELSE branch. If the result data type is a CDS amount field, a CDS quantity field, or a CDS calculated quantity, a reference annotation must be assigned. The following table shows how the result data type is calculated if one or more of the operands are amount and/or quantity fields.
Operand1 / Operand2 | Amount | Quantity | Calculated Quantity | Number |
Amount | amount | calculated quantity | calculated quantity | calculated quantity |
Quantity | calculated quantity | quantity | calculated quantity | calculated quantity |
Calculated Quantity | calculated quantity | calculated quantity | calculated quantity | calculated quantity |
Number | calculated quantity | calculated quantity | calculated quantity | number |
Further details on SAP Help Portal:
Example
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view entity DEMO_CDS_COMPLEX_CASE_1
as select from DEMO_CDS_CALC_QUANTITY_BASE
{
key Id,
//amounts and calculated quantities are compatible,
//result is a calculated quantity
@Semantics.quantity.unitOfMeasure: 'Calcunit'
case
when Char1 = abap.char'A'
then Dec10 //cuky reference -> amount field
when Char1 = abap.char'B'
then 100 / Dec10 //calculated quantity
else 0
end as calcQuan,
Cuky,
concat( concat( Cuky , '/' ), Unit2 ) as calcUnit,
//CURR requires conversion to DECFLOAT34
@Semantics.amount.currencyCode : 'cuky'
case when Char1 = abap.char'A'
then CURR_TO_DECFLOAT_AMOUNT( Curr102 )
else Dec10 //cuky reference
end as CurrConv
}
Release info: available since kernel release 7.87, ABAP release 7.57.
If one of the operands of a comparison is a CDS amount field, a CDS quantity field, or a CDS calculated quantity, special rules apply:
The following table shows the possible combinations of operands in comparisons. Number refers to an operand of a numeric data type without reference annotation that turns it into an amount or quantity field.
Type of Operand | Comment |
amount, number | syntax check warning occurs. Exception: if number is specified as literal, no syntax check warning occurs. |
amount, amount | Ok. Note: Elements of data type CURR must have exactly the same number of decimal places. |
quantity, number | syntax check warning occurs. Exception: if number is specified as literal, no syntax check warning occurs. |
quantity, quantity | ok |
amount, quantity | syntax check warning occurs. |
amount, calculated quantity | syntax check warning occurs. |
quantity, calculated quantity | syntax check warning occurs. |
calculated quantity, number | syntax check warning occurs. Exception: if number is specified as literal, no syntax check warning occurs. |
calculated quantity, calculated quantity | ok |
Further details: Comparisons with Amounts and Quantities on SAP Help Portal.
In UNION views and INTERSECT views, the data types of elements of different branches must match. For CDS amount fields, CDS quantity fields, and CDS calculated quantities, this implies:
Questions, comments, or further details required? Use the Comments section below. Discussions are welcome.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
11 | |
11 | |
11 | |
10 | |
10 | |
8 | |
7 | |
7 | |
6 | |
6 |