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
42,930

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.

22 Comments
shais
Participant
0 Kudos
Thanks for the detailed overview.

 

What would be the result of GET_NUMERIC_VALUE ( 60.00 JPY )?

(6000 JPY in external format)
AndreaUS
Product and Topic Expert
Product and Topic Expert
Hi,

the value stays exactly the same.

The function removes the reference to a currency key and it converts the data type to DECFLOAT34. If arg has data type CURR, an implicit decimal shift takes place.

Purpose: strict checks have been implemented for calculations with amounts in expressions and with this function, some limitations can be circumvented.


Docu on SAP Help Portal.

Does that answer your question? Any further issues?

shais
Participant
Almost.

The statement "If arg has data type CURR, an implicit decimal shift takes place." is quite amorphic.

I brought JPY as an example. The (external) value of 6000 JPY is internally stored in ABAP as 60.00 JPY. What would be the result of GET_NUMERIC_VALUE? 6000 or 60.00?
AndreaUS
Product and Topic Expert
Product and Topic Expert

It depends how the value 60.00 JPY is stored in your system:

  • If the value is stored as DEC(x,2), the value remains 60.00. No decimal shift takes place.
  • If the value is stored in a CURR(x,2) column, an implicit DECIMAL_SHIFT statement is generated.

The decimal shift calculates the difference between the number of decimal places of the currency and the number of decimal places of the column.

JPY does not have any decimal places. Your column apparently has two decimal places.

2 - 0 = 2. The comma is shifted two places to the right. In other words, the value is multiplied by 100. The result of GET_NUMERIC_VALUE is 6000.

 

shais
Participant
Thanks. That answers my question.
twnew
Explorer
0 Kudos
Hi,

I have 2 CDS Views; one read aggregation (SUM) in EKBE (Po History) for field DMBTR (CURR13.2)

DMBTR is always positive but SHKZG H or S needs to handle it positive or negative.

For a PO line I want to have the total amount of DMBTR

--> SUM DMBTR S subtracted by SUM DMBTR H into new field.

But the value of the new field is zero even if there is a value in DMBTR S.

Function GET_NUMERIC_VALUE or CURR_TO_DECFLOAT_AMOUNT are not available in Eclipse ("Specified Function xxx is not known as a predefined function")

ABAP Kernel 785 Patch Level 232
SAP_BASIS 756 0001
SAP_ABA 75G 0001
S4CORE 106 0001

How can I add these functions into CDS View? or even get the calculation done?

Best regards,

Thomas
AndreaUS
Product and Topic Expert
Product and Topic Expert
Hi Thomas,

both functions GET_NUMERIC_VALUE and CURR_TO_DECFLOAT_AMOUNT are available in CDS view entities (DEFINE VIEW ENTITY) as of release 7.83.

I suspect your views are classic CDS views (DEFINE VIEW). Please check.

You can migrate your CDS views to CDS view entities as described in the following blog post: A new generation of CDS views: how to migrate your CDS views to CDS view entities | SAP Blogs

If you use CDS view entities and you work on release 7.85, then both functions should be available. If you get an error, it is a bug. Please open an incident on BC-DWB-DIC.

Best regards

Andrea
twnew
Explorer
0 Kudos
Hi Andrea,

thanks for the Reply.

Indeed I used the "old" CDS view for this. Now the functions are available.

because of SAP note https://launchpad.support.sap.com/#/notes/3213096 I could not use the CDS directly.

another problem was the association...when one entry is not available ( no H in EKBE ) the calculation also does not calculate 10.000 - 0 ... it just do nothing and set to 0.

I had to add a case line...when H is NULL then S ELSE do H - S (converted with CURR_TO_DECFLOAT_AMOUNT before )

So I think my problem is solved...but handling this in CDS is not very easy. with ABAP SQL it would take 4 lines of code

 

Thank you very much!

Best regards,

Thomas
shavneet1
Participant
0 Kudos
Hello Andrea ,

I have a CDs View where i calculate the Total Bid value , from the fields of EKPO table.

Something like this :


key Item.ebeln as PO,


max( CURR_TO_DECFLOAT_AMOUNT(Item.netpr ) * GET_NUMERIC_VALUE(Item.ktmng) ) as TotalBidValue.




Now this TotalbidValue is used in the projection view.

IT is giving me error : Entity ZC_HEADERU01TP contains the unsupported datatype DECFLOAT34.


How to resolve this ?, as after using these functions , data type for the new field is DECFLOAT34 , which is not supported.

 

Thanks and Regards ,

Shavneet

 
AndreaUS
Product and Topic Expert
Product and Topic Expert
0 Kudos
Hi Shnaveet,

what kind of projection view is that? Which provider contract does the projection view have? Or none at all?

Which release do you work on?

Can you try casting to another data type?

I have tried to reproduce the scenario and i do not get any error message for DECFLOAT34 in a projection view.


The field TotalBidValue has data type DECFLOAT34 here and there is no error message.

Please provide further details.
stefan-siemes
Explorer

Can you show how to implment these new calculated quantities in an OData service?

System: ABAP 7.56

Example (Maintenance Hours per Usage (e.g. km, runtime, etc.):

concat( concat( EinheitIHZeit, '/'), EinheitBetriebswert) as EinheitCalc
@Semantics.quantity.unitOfMeasure: 'EinheitCalc'
IHZeit / Betriebswert as IstZeitProBetriebswert
  • An OData v2 Service Binding does not accept DECFLOAT34 data type at all
  • An OData v4 Service Binding does accept the DECFLOAT34, but fails in the gateway client with error code 036 (No configuration for unit of measure '...' )

This is very inconvenient ...

Thanks!

 

Martin_4
Participant
0 Kudos
Hi,

wouldnt it be much easier  to stop all this cumbersome CDS analytics query syntax
and to use the BW Eclipse query designer
which has got all this already
and to enable teh BW Eclipse query designer to generate a Fiori App ?0

 
AndreaUS
Product and Topic Expert
Product and Topic Expert
0 Kudos
Hi,

this amount and quantity handling is available in CDS view entities in general, not just for analytical queries. So it can be used in transactional apps as well. Many more use cases than just analytical queries.

"enable teh BW Eclipse query designer to generate a Fiori App ?" -> andreas.riehl can maybe answer this.
stefan_unnebrink
Product and Topic Expert
Product and Topic Expert
0 Kudos
Dear Martin,

it is a good question. I agree that CDS language is not the best choice for modelling an analytical query and annotations are not the best way to express core features (e.g. AnalyticsDetails.query.formula). I think with the new "transient view entities with provider contract analytical_query" we made good progress to overcome some CDS/SQL limitations. E.g. you can use native CDS formulas instead of the annotation AnalyticsDetails.query.formula.

But we at SAP decided to go for CDS analytics query (in short CDS query) for the following reasons: (in the following I will call the object you store with the BW Eclipse query designer in short BW-query)

  1. CDS views have a different lifecycle then a BW query.

    1. BW query is delivered in so called D-version has to be activated by the customer

    2. Where-use check will break from cube to query



  2. Different extensibility concepts

  3. Different concept for start authorization (cloud enablement)

  4. Seamless tool integration for CDS query with other objects (CDS, services) in ADT, e.g. navigation to CDS cube, lookup entities etc.

  5. In the BW tool you see the BW names but not the CDS names

  6. In the BW tool there is no native support for Consumption.derivation. Instead you have to define an "exit variable" and write some ABAP code

  7. ...


We could have solved some of these limitations but some others are fundamental such that the BW query always would work like a foreign body.

Of course you can create BW queries on top of CDS providers. In the on-prem world this is technically possible. It is especially useful, if you need a feature which is not (yet) provided by the CDS query. But you have to be aware of the limitations.

 
freder1k_han
Explorer
0 Kudos
What ABAP CDS views are really missing is a graphical modeler like we use for calculation views which is so nice. It seems strange that this has not been by prioritized. stefan.unnebrink are there any plans for this? It would help customers a lot and make ABAP CDS views much more user friendly

 

BR Frederik
stefan_unnebrink
Product and Topic Expert
Product and Topic Expert
0 Kudos
Thanks for the input. There are discussions about it but no finalized plans. Only in cloud there exists a query modelling tools for key users.
dhanunjai27
Member
0 Kudos
Hi Andrea,

I'm getting the following errors.

  1. When we tried to reuse the field in the same cds entity using $projection getting the error "$projection expression cannot be reused".



$projection expression cannot be reused


    2. Getting the error when trying to create the cds projection of type analytical query. The error is "Provider contract AQ is not supported yet".


Provider contract AQ is not supported yet.


 

S/4 HANA version is 2021 SP Stack 01 (02/2022).

 

Thanks,

Dhanunjai Gandavarapu.
AndreaUS
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Dhanunjai Gandavarapu,

  1. $projection is available as of 2021, so it should be supported. I could not reproduce the error, not even in an 2021 system. Can you please open an incident on

    BC-DWB-DIC for further investigation?

  2. Analytical projection views are available as of SAP S/4HANA and SAP S/4HANA Cloud, private edition as of 2022. So unfortunately, your release does not support this feature.

 

Best

Andrea

thomas_knobloch
Product and Topic Expert
Product and Topic Expert
0 Kudos




Hi Andrea,

I tried nearly everything, but I can't find a solution.

I just want to do the following operation:

AMOUNT = AMOUNT / NUMBER * QUANTITY

I tried to get at least something buit I get an error without details at activation. Here is my example:

image.png


AndreaUS
Product and Topic Expert
Product and Topic Expert
0 Kudos
Hi Thomas,

thanks for your question.

The screenshot is broken, difficult to fix an issue with so few details.

Here are a few thoughts:

  • AMOUNT / NUMBER = AMOUNT

  • AMOUNT * QUANTITY = calculated quantity

  • The result of your calculation is a calculated quantity and NOT an amount.

  • You could use GET_NUMERIC_VALUE and then use the annotation @Semantics.amount.currencyCode to get an amount field.


Example:

rent_per_size is a calculated quantity. You need to create a view stack to use it with the function GET_NUMERIC_VALUE. Then, annotate it with a currency key to get an amount field.
@Semantics.amount.currencyCode:'currency'
get_numeric_value(rent_per_size) as myAmountField

Does that help?
bharatbajaj
Active Participant
0 Kudos

Hi @AndreaUS ,

I have a use case where I need to calculate AMOUNT / QUANTITY to get PER_UNIT_AMOUNT .

I am facing some CDS errors which I just posted in my post yesterday : CDS VIEW ENTITY - CURR / QUAN calculated field gives activation error 

In your blog post, I saw the use of CalculatedIUnit to achieve this calculation, which is bit strange in my opinion, as we are still trying to get a Calculated AMOUNT (and not quantity), (Amount / Quantity -> gives Amount), so why we mandatorily need a separate unit reference and not just use the CUKY reference instead? 

Moreover, if we have multiple calculated amounts, then for each Amount field calculation, I need a separate calculatedUnit (having the same or different value).

 I have a real use case to convert atleast 15 cost fields from KEPH table (say KST001...KST015 ) to get per unit cost for materials in business report. So with this approach, I need to create 15 'calculatedUnit' fields in the CDS having the same value (say EUR / KG).

This is not CLEAN ABAP in my opinion, and I am still looking for a better and simpler way of achieving this.

If I compare with trditional ABAP programming, a simple arithmetic operation of CURR / QUAN (eg. LV_AMOUNT = BSEG-DMBTR / BSEG-MENGE) works perfectly fine, without any additional logic or unit conversions, and I was expecting the same in CDS VIEW ENTITY as well.

I wonder, why so much complexity in CDS for a simple calculation.

any suggestions? 

Regards,

Bharat Bajaj

AndreaUS
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi @bharatbajaj,

 In your blog post, I saw the use of CalculatedIUnit to achieve this calculation, which is bit strange in my opinion, as we are still trying to get a Calculated AMOUNT (and not quantity), (Amount / Quantity -> gives Amount), so why we mandatorily need a separate unit reference and not just use the CUKY reference instead? Think of the rental price of an apartment. 600 Euro for a 60m2 flat makes a price of 10 Euro per m2. So the unit reference is Euro/square meter. CUKY references must have one of the values from the database table TCURC, so they can be only, for example, Euro. That is not suitable for example of the apartment price PER M2.

Moreover, if we have multiple calculated amounts, then for each Amount field calculation, I need a separate calculatedUnit (having the same or different value). 

Consider the following example: 

Currency,
@Semantics.amount.currencyCode: ‘Currency’
Case when Land = DE then sales else null as SalesDE,

@Semantics.amount.currencyCode: ‘Currency’
Case when Land = US then sales else null as SalesUS,

=>  different values would have to be written to the currency field for one line. This would lead to a short dump in certain scenarios. Generally spoken, the logic is that a key figure is a tuple consisting of its number and a unit (currency, unit or complex unit). Therefore, each key figure (especially formula) should refer to its own unit field.

I wonder, why so much complexity in CDS for a simple calculation.

The additional checks provide more type safety and identify problematic situations early on. if you divide a CURR by a QUAN (eg. LV_AMOUNT = BSEG-DMBTR / BSEG-MENGE), then you need some reference to provide a semantic sense to your result. The stricter CDS checks support you with that.

Hope this helps.