
This blog is about CDS scalar functions - CALENDER_SHIFT and CALENDER_OPERATIONS and how to use it in analytical queries for time comparison. Similarly FISCAL_CALENDAR_SHIFT and FISCAL_CALENDAR_OPERATION can be used.
These CDS scalar functions are used to compare the measure value of current time period with previous time period. Further it can be used to calculate measure value for year to date (YTD), month to date (MTD) or quarter to date (QTD). Current member can only be used when time dimensions are in drilldown else restricted measure is used. The usage of current member is not as fast as restricted measure. Detail specification of the mentioned scalar functions can be found in Help.
This function can be used by modeling an association with the cube. In the ON-condition, the scalar functions are specified on the right side.
association to ZOQ_FLIGHT as _previousMonth
on _previousMonth.fyearMonth = calendar_shift( base => $projection.fyearMonth,
base_level => calendar_date_level.#MONTH,
shift => abap.int2'-1',
shift_level => calendar_date_level.#MONTH )
Constraints:
key fldate,
@ObjectModel.value.derivedFrom: 'fldate'
@Semantics.calendar.yearMonth: true
@ EndUserText.label: 'FL Month'
_calday.calmonth as fyearMonth,
Example 1: Time comparison with time in drill-down (Current Member Variables)
@AccessControl.authorizationCheck: #NOT_ALLOWED
@EndUserText.label: 'Query: Current Memeber on Month'
define transient view entity ZLR_PC_CM_1
provider contract analytical_query
as projection on ZOQ_FLIGHT
association to ZOQ_FLIGHT as _previousMonth
on _previousMonth.fyearMonth = calendar_shift( base => $projection.fyearMonth,
base_level => calendar_date_level.#month,
shift => abap.int2'-1',
shift_level => calendar_date_level.#month )
association to ZOQ_FLIGHT as _firstMonthOfQuarter
on _firstMonthOfQuarter.fyearMonth between calendar_operation( base => $projection.fyearMonth,
base_level => calendar_date_level.#month,
operation => calendar_date_operation.#first,
operation_level => calendar_date_level.#quarter )
and $projection.fyearMonth
association to ZOQ_FLIGHT as _CMM1_CMP1
on _CMM1_CMP1.fyearMonth between calendar_shift( base => $projection.fyearMonth,
base_level => calendar_date_level.#month,
shift => abap.int2'-1',
shift_level => calendar_date_level.#month )
and calendar_shift( base => $projection.fyearMonth,
base_level => calendar_date_level.#month,
shift => abap.int2'1',
shift_level => calendar_date_level.#month )
{
@AnalyticsDetails.query: {
axis: #ROWS,
totals: #SHOW
}
@EndUserText.label: 'Year Quarter'
flquarter,
@AnalyticsDetails.query: {
axis: #ROWS,
totals: #SHOW
}
@EndUserText.label: 'Year Month'
fyearMonth,
@AnalyticsDetails.query.axis: #FREE
.hidden: true
currency,
@Semantics.amount.currencyCode: 'currency'
paymentsum,
_previousMonth.currency as PrevMonthCurrency,
@Semantics.amount.currencyCode : 'PrevMonthCurrency'
@EndUserText.label: 'Previous Month'
_previousMonth.paymentsum as PrevMonthPayment,
_firstMonthOfQuarter.currency as firstMonthOfQuartCurrency,
@Semantics.amount.currencyCode : 'firstMonthOfQuartCurrency'
@EndUserText.label: 'Quarter to today'
_firstMonthOfQuarter.paymentsum as firstMonthOfQuartPayment,
_CMM1_CMP1.currency as last3MonthCurrency,
@Semantics.amount.currencyCode : 'last3MonthCurrency'
@EndUserText.label: 'Previous month To next month'
_CMM1_CMP1.paymentsum as CMM1_CMP1Payment
}
where flyear = '2019'
and currency = 'EUR'
Result:
In this Result
This can be done using the CASE statement, where one of the scalar function is used.
case when fyearMonth = calendar_shift( base => $parameters.p_month,
base_level => calendar_date_level.#MONTH,
shift => abap.int2'-1',
shift_level => calendar_date_level.#MONTH ) then paymentsum end as paymentPrev
Constraints:
Example 2: Time comparison (Restricted key figure)
@AccessControl.authorizationCheck: #NOT_ALLOWED
@EndUserText.label: 'Query: Current Memeber on Month'
define transient view entity ZLR_PC_RSRT_SHIFT1
provider contract analytical_query
with parameters
p_month : /bi0/oicalmonth
as projection on ZOQ_FLIGHT
{
@AnalyticsDetails.query: {
axis: #ROWS,
totals: #SHOW
}
carrid,
currency,
virtual currCur : abap.cuky,
@Semantics.amount.currencyCode : 'currCur'
@Consumption.dynamicLabel: {
label: 'Bookings &1',
binding: [{ index : 1 , element : 'fyearMonth' }]
}
case when fyearMonth = $parameters.p_month then paymentsum end as paymentCur,
virtual currPrev : abap.cuky,
@Semantics.amount.currencyCode : 'currPrev'
@Consumption.dynamicLabel: {
label: 'Previous Bookings &1',
binding: [{ index : 1 , element : 'fyearMonth' }]
}
case when fyearMonth = calendar_shift( base => $parameters.p_month,
base_level => calendar_date_level.#month,
shift => abap.int2'-1',
shift_level => calendar_date_level.#month ) then paymentsum end as paymentPrev,
virtual currLastInYear : abap.cuky,
@Semantics.amount.currencyCode : 'currLastInYear'
@Consumption.dynamicLabel: {
label: 'Bookings from &1 To &2',
binding: [{ index : 1 , element : 'fyearMonth', replaceWith: #LOW },
{ index : 2 , element : 'fyearMonth', replaceWith: #HIGH }]
}
case when fyearMonth between calendar_operation( base => $parameters.p_month,
base_level => calendar_date_level.#month,
operation => calendar_date_operation.#first,
operation_level => calendar_date_level.#year ) and $parameters.p_month then paymentsum end as paymentToDate
}
where currency = 'EUR'
Result:
In this example I have used @Consumption.dynamicLabel to dynamically set the column label with values calculated from parameter.
On-Prem SAP_BW from 7.58 and Cloud from 2308
3270090-CurrentMember + FemsN
3359468-Current Member on reference characteristic and selection for Partial Time Characteristic
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
18 | |
14 | |
11 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
5 |