Currency Conversion in CDS
Like Unit (quantity) conversion, currency conversion is also supported by both Open SQL and CDS in select statements. Unlike unit conversion, currency conversion inbuilt functions can be used more reliably.
In the case of unit conversion, sometimes conversion is required between two units which are dependent on other factors such as atmospheric condition or other material properties. For example, packaging foil can be sold as length and also as weight. Converting length to weight requires additional information – the density of the material and thus, this conversion is stored against the material master record.
That is not the case with currency conversion but it depends on the date of the conversion. Let’s see an example for currency conversion:
define view zcds_currency with parameters p_to_curr :abap.cuky(5), p_conv_Date :abap.dats as select from sflight { key carrid, connid, fldate, price, currency, currency_conversion( amount => price, source_currency => currency, target_currency => :p_to_curr, exchange_rate_Date => :p_conv_Date ) as ConvPrice }
If you still need to perform a direct calculation using the factors stored in some table, then please check out this blog. However in this example, denominators are not zeroes, is not ensured. Can you figure out how to do that?