Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
AndreaUS
Product and Topic Expert
Product and Topic Expert

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:

  • CDS Amount Field
  • CDS Quantity Field
  • CDS Calculated Quantity with Calculated Unit
  • Unit and Currency Conversion Functions
  • Amounts and Quantities in Expressions:
    • Amounts and quantities in arithmetic expressions
    • Amounts and quantities in aggregate functions
    • Amounts and quantities in simple and complex case expressions
    • Amounts and quantities in comparisons
    • Amounts and quantities in UNION views and INTERSECT views

CDS Amount Field

    • Field that stores an amount in a specific currency.
    • Possible data types: CURR, DECFLOAT34, DEC, and FLTP.
    • Reference to a currency key using the annotation @Semantics.amount.currencyCode is mandatory.
    • A currency key must have data type CUKY and it can contain a currency ID from the DDIC database table TCURC.

Further details: ABAP CDS – Amount Fields on SAP Help Portal.

CDS Quantity Field

    • Field that stores a quantity in a specific unit.
    • Possible data types: QUAN, DECFLOAT16, DECFLOAT34, DEC, FLTP, INT1, INT2, or INT4.
    • Reference to a unit of measurement key using the annotation @Semantics.quantity.unitOfMeasure is mandatory.
    • A unit of measurement key must have data type UNIT and can contain a unit ID from the DDIC database table T006.

Further details: ABAP CDS – Quantity Fields on SAP Help Portal.

CDS Calculated Quantity with Calculated Unit

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.

  • A calculated quantity is a field that stores a quantity in a self-defined unit.
  • It is always the result of a calculation using amounts and quantities.
  • A calculated quantity always has data type DECFLOAT34.
  • A reference to a calculated unit using the annotation @Semantics.quantity.unitOfMeasure: 'calculatedUnit' is mandatory.
  • A calculated unit has data type CHAR and specifies a self-defined unit, such as €/m2 (cost per square meter of an apartment, for example).

Further details:

Example

Arithmetic expressionResult
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.

  • Field rent_curr has data type CURR and a currency key reference. It is converted into data type DECFLOAT34 using the conversion function CURR_TO_DECFLOAT_AMOUNT.
  • Field apartment_size has data type QUAN(10,2) and a unit key reference.
  • The calculated unit is defined by means of a concatenation of a currency code, a forward slash, and the apartment unit. The resulting calculated unit is EUR/MTK, MTK being the unit ID for 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      
}

 

 

Unit and Currency Conversion Functions

FunctionEffect
UNIT_CONVERSION( quantity              => arg1,
source_unit       => arg2,
target_unit        => arg3[,
client                  => arg4][,
error_handling => arg5])
  • Performs a unit conversion for arg1.
  • Has been available "forever", i.e. since ABAP release 7.40.
  • Available in CDS DDIC-based views and in CDS view entities.
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])
  • Performs a currency conversion for arg1.
  • Has been available "forever", i.e. since ABAP release 7.40.
  • Available in CDS DDIC-based views and in CDS view entities.
GET_NUMERIC_VALUE( arg )
  • Returns the numeric value of a currency or quantity field without its currency or unit key.
  • Available since kernel release 7.83, SAP BTP ABAP Environment 2102, ABAP release 7.56.
  • Available in CDS view entities.
CURR_TO_DECFLOAT_AMOUNT( arg )
  • Converts a currency field of data type CURR into a currency field of data type DECFLOAT34.
  • Available since kernel release 7.83, SAP BTP ABAP Environment 2102, ABAP release 7.56.
  • Available in CDS view entities.

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

The following CDS view entity applies the function GET_NUMERIC_VALUE to calculate the number of bookings. It divides the total value of current bookings (field paymentsum) by the price of a flight (field price). This would not be possible without the function GET_NUMERIC_VALUE, since elements of data type CURR are not allowed in arithmetic expressions.
Details on the elements:
  • paymentsum has data type CURR, a reference annotation that assigns a currency key, and it contains the total value of current bookings.
  • price has data type CURR, a reference annotation that assigns a currency key, and it contains the price of a flight.
  • number_of_bookings has data type DECFLOAT34 and it has no currency key assigned.

 

 

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 
} 

 

 

Amounts and Quantities in Expressions

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:

  • Amounts and quantities in arithmetic expressions
  • Amounts and quantities in aggregate functions
  • Amounts and quantities in simple and complex case expressions
  • Amounts and quantities in comparisons
  • Amounts and quantities in UNION and INTERSECT views

Note: Cast expressions do not handle amounts. Only the target type CURR requires a reference to a currency key.

Amounts and quantities in arithmetic expressions

Release info: available since kernel release 7.84, SAP BTP ABAP Environment 2105, ABAP release  7.56

operand / operator/*+, -
amount, amountcalculated quantitycalculated quantityamount
quantity, quantitycalculated quantitycalculated quantityquantity
calculated quantity, amountcalculated quantitycalculated quantityamount
amount, quantitycalculated quantitycalculated quantity-
calculated quantity, numbercalculated quantitycalculated quantity-
calculated quantity, quantity,calculated quantitycalculated quantityquantity
calculated quantity, calculated quantitycalculated quantitycalculated quantitycalculated quantity
amount, numberamountamount-
number, amountcalculated quantityamount-
quantity, numberquantityquantity-
number, quantitycalculated quantityquantity-

Further details: Amounts and Quantities in Arithmetic Expressions on SAP Help Portal

Amounts and quantities in aggregate functions

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 FunctionType of OperandResult Type
MAX, MIN, SUM, AVGamountamount
MAX, MIN, SUM, AVGquantityquantity
MAX, MIN, SUM, AVGcalculated quantitycalculated quantity
COUNTamount, quantity, calculated quantitynumber of type INT4

Further details: Amounts and Quantities in Aggregate Expressions on SAP Help Portal.

Amounts and quantities in simple and complex case expressions

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 / Operand2AmountQuantityCalculated QuantityNumber
Amountamountcalculated quantitycalculated quantitycalculated quantity
Quantitycalculated quantityquantitycalculated quantitycalculated quantity
Calculated Quantitycalculated quantitycalculated quantitycalculated quantitycalculated quantity
Numbercalculated quantitycalculated quantitycalculated quantitynumber

Further details on SAP Help Portal:

Example


The example shows a CDS view entity with two complex case distinctions using amount and quantity fields.

 

 

@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 

} 

 

 

Amounts and quantities in comparisons

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:

  • Both operands must have the same reference type. Both must be either amounts, or quantities, or calculated quantities. Comparing operands with references of different types (currency key, unit key, or calculated unit) results in a syntax check warning.
  • An operand of type CURR can only be compared with another operand of type CURR. The number of decimal places of both operands must match exactly.

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 OperandComment
amount, numbersyntax check warning occurs.
Exception: if number is specified as literal, no syntax check warning occurs.
amount, amountOk.
Note: Elements of data type CURR must have exactly the same number of decimal places.
quantity, numbersyntax check warning occurs.
Exception: if number is specified as literal, no syntax check warning occurs.
quantity, quantityok
amount, quantitysyntax check warning occurs.
amount, calculated quantitysyntax check warning occurs.
quantity, calculated quantitysyntax check warning occurs.
calculated quantity, numbersyntax check warning occurs.
Exception: if number is specified as literal, no syntax check warning occurs.
calculated quantity, calculated quantityok

Further details: Comparisons with Amounts and Quantities  on SAP Help Portal.

Amounts and quantities in UNION views and INTERSECT views

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:

  • Calculated quantity fields can be merged only with other calculated quantity fields.
  • Amount fields and quantity fields can be merged with each other, as long as the data types match.
  • For amount fields of type CURR, the number of decimal places must match exactly in all UNION branches.
  • Elements of data type CURR are incompatible to any other data type. The function CURR_TO_DECFLOAT_AMOUNT can be used to convert an amount field of data type CURR into an amount field of data type DECFLOAT34.

Questions, comments, or further details required? Use the Comments section below. Discussions are welcome.

20 Comments