Blogs / SAP / SAP Analytics Cloud / Analytic Applications / SAP Analytics Cloud – Application Design Series: 13 – Date & Time Range Functions

SAP Analytics Cloud – Application Design Series: 13 – Date & Time Range Functions

Apr 2, 2019

SHARE

In the previous blog of this series, we learned how to launch Smart Discovery from analytic applications in SAP Analytics Cloud. In this blog, let us look at Date and Time Range functions and how to use them.

Analytic Applications support passing single or multiple members for filtering but do not support ranges. However, for Date dimensions, you can pass the time range as a parameter. Here below you can see various time range filters in action.

SAP Analytics Cloud – Application Design Series: 13 – Date & Time Range Functions

Creating a Time Range

TimeRange is a parameter type that can be used to filter dimensions of type Date.

SAP Analytics Cloud – Application Design Series: 13 – Date & Time Range Functions

A Time Range can be created using the function TimeRange.create(), which takes three parameters namely TimeRangeGranularity, Start Date and End Date.

Syntax: TimeRange.create(TimeRangeGranularity, Start Date, End Date)

TimeRangeGranularitycan be of type Year, Halfyear, Quarter, Month, Day, Hour, Minute, Second or Millisecond as seen below.

SAP Analytics Cloud – Application Design Series: 13 – Date & Time Range Functions

For example, the following script can be used to create a month range between two dates.

SAP Analytics Cloud – Application Design Series: 13 – Date & Time Range Functions

TimeRange.create(TimeRangeGranularity.Month, new Date(2017, 12, 27), new Date(2019, 3, 19));

By simply changing the granularity type you can get other time ranges based on year, quarter, day, etc.

Time Range Functions

Apart from the create function for Time Range, you can also create time ranges using three more functions.

SAP Analytics Cloud – Application Design Series: 13 – Date & Time Range Functions

The syntax for these functions are as follows:

  1. Week Range – createWeekRange (start year, start week, end year, end week)
  2. Month Range – createMonthRange (start year, start month, end year, end month)
  3. Year Range – createYearRange (start year, end year)

For example, to create a range of months we can directly use the Month Range function as in the following script:

TimeRange.createMonthRange(2018, 2, 2019, 3);

Similarly, other time ranges can also be created by using these functions with appropriate parameters.

SAP Analytics Cloud – Application Design Series: 13 – Date & Time Range Functions

Dynamic Time Range

Let us see how to get the current date, month, quarter and year for dynamic filtering. You can generate the current (system) date using Date functions with Date.now() as the parameter. Once you have the local variable of type Date you can extract other information like a month, year, etc.

SAP Analytics Cloud – Application Design Series: 13 – Date & Time Range Functions

Since there is no function to extract quarter, you can use the Math function and extract quarter from current_month.

SAP Analytics Cloud – Application Design Series: 13 – Date & Time Range Functions

Now you can use these local variables in the TimeRange function to achieve dynamic filtering. Note that getMonth() function returns month as a value between 0 – 11 for January to December.

Also, note that you need to assign an appropriate hierarchy to the Date dimension for the Time Range filter to work properly.

SAP Analytics Cloud – Application Design Series: 13 – Date & Time Range Functions

The following example shows how to filter a chart to the last two quarters dynamically, using scripting. First, a TimeRange variable of granularity Quarter is created (QuarterFilter). Then the variable is used in the setDimensionFilter function as a parameter.

SAP Analytics Cloud – Application Design Series: 13 – Date & Time Range Functions

var current_date = new Date(Date.now());
var current_year = current_date.getFullYear();
var current_quarter = Math.floor(current_date.getMonth()/3);
if(current_quarter<1){
   var passed_quarter = 3;
    var passed_year =  current_year-1;
}
else{
   passed_quarter = current_quarter-1;
}
var passed_quarter_starting_month = (passed_quarter*3);
var QuarterFilter = TimeRange.create(TimeRangeGranularity.Quarter, current_date, passed_Quarter_date); 
Chart_1.getDataSource().setDimensionFilter(“OrderDate”, QuarterFilter);

* * *

Reach out to our team here if you are interested to evaluate if SAP Analytics Cloud is the right tool for you.


Corporate HQ:
5920 Windhaven Pkwy, Plano, TX 75093

+1 888-227-2794

+1 972-232-2233

+1 888-227-7192

solutions@visualbi.com


Copyright © Visual BI Solutions Inc.

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

Share This!

Share this with your friends and colleagues!