Data Modeling with CDS Views – Part 2: Currency/Unit Conversion & Date Functions

Introduction

This blog series is about the basics of data modeling with CDS Views. Part 1 gave you an overview about the Syntax of a CDS view, Joins, Associations and Unions.

Part 2 will give you a short overwiew about Currency/Unit Conversion and Date Functions.

Currency/Unit Conversion

To create a currency or unit conversion there are solutions provided by SAP out of the box. Now we will create an example with a currency conversion calculation the values to Canadian Dollars.

This code snippet shows the minimal scenario to set up a conversion. You have to assign the following fields:

  • amount à takes the table field price
  • source_currency à takes the table field currency
  • target currency à represents a constant „CAD“
  • exchange_rate_date à shows the table field fldate (flight date)

There are also some optional parameters you can use in your function – for example define a rounding or decimal_shift to provide the result you require.

During execution the calculation will perform the currency conversion. It is important that the exchange rates will be taken from the system.

As a result, you can see that the price column in USD has been converted to the created field “PriceInTargetCurrency”.

For unit conversion there is a similar function unit_conversion where you have mandatory to fill the quantity, source_unit and target_unit.

Date Functions

There are several date functions available which can be used out of the box. For example to check if a date is valid or calculate how much days are between two dates.

Exactly this usecase is shown by the example below. Therefore, we use the VBAK table and the fields „erdat“ (creation date) and „vdatu“ (requested delivery date). In our example we just call them “creation” and “reqdeldate” and calculate the difference in the field “datediff”.

With this function call we calculate the days between the two dates. The name of the function is “dats_days_between” where you just need to provide two dates for the comparison. As output, we use the column “datediff.

Conclusion

In this blog we gave you a short overview about currency/unit conversion and date functions with ABAP CDS Views. As you can see above it is easy to use such provided functions by SAP.

In our next Blog (Part 3) we will focus on session information, case, cast, where, Input Parameter & consumption filters.

More Posts from the Blog Series