In the first part of this SAC Scripting blog series, I shorty explained how Master Data can be read and changed in SAC Analytic Application with the help of scripting. In the second part, I showed how Data Actions can be used in Analytic Application and how parameters can be dynamically defined and handed over to the data action. In this part, I will explain how dates can be handled in scripting.

I am creating an Analytic Application to maintain the steps and activities per user, I also need to enable the user to enter steps per day as a new entry. The user will be able to choose the day, he/she wants to enter steps for, from a dropdown. This dropdown will be filled with the current day and the previous five days.  This is the first case I will shorty explain. As I currently don’t see any other option to do this, you will see that it is a small workaround by partly using string conversions here.

Secondly, I will take a look at how to dynamically set up date ranges and filter charts with these in the scripting.

1.      Fill a Dropdown with dates

1.1   Create Variable and add Dropdown

I create a new Variable which is called “Var_Date”. This variable is of the type string to store the date format dd.mm.yyyy in it and to not use the system date format.

Then I also added a dropdown, which is called “Dpd_Date”. This dropdown will then be filled in the script logic.

 

1.2 Create Method for reuse with import parameter

As I need the current date and the previous 5 days, I will need the following logic 5 times:

  • New Date = Current date, – x days
  • Convert New Date into my preferred date format dd.mm.yyyy

To make sure I do not create the same logic 5 times, I created a Script Object called HelperFunctions. After that, I clicked on the three dots on the right of the HelperFunctions and selected “Add Script Function”.

I created the Function “Calculate Date”. This function has an argument (Import Parameter) called “day_go_back”, which is defined as a number.

The starting point is always the current date, and the new date is calculated by subtracting the days that are imported with the parameter “days_go_back”. At the end of the logic , the previously created variable Var_Date is filled by the new date in the format dd.mm.yyyy.

Here is the logic:

// approach with millisec
var current_date = new Date(Date.now());
var date_new_millisec = current_date.setDate(current_date.getDate() - days_go_back);
var new_date = new Date(date_new_millisec);
var date_new_format = new_date.toJSON().slice(0,10);

//now I have YYYY-MM-DD change format to DD.MM.YYYY
var dd = date_new_format.slice(8,10);
var mm = date_new_format.slice(5,7);
var yyyy= date_new_format.slice(0,4);
Var_Date = dd+"."+mm+"."+yyyy;

 

1.3 Fill the Dropdown and set default date

To fill the Dropdown, I can now call the previously created function “CalculateDate” with the arguments 0-5. I also need to call this function for the current date (argument = 0) as the format of the system date is not equal to my desired date format dd.mm.yyyy. Therefore, the function is used for formatting here. While filling the dropdown I directly set the current date as default as well. Here is how the script looks like:

var i = 0;
do {	HelperFunctions.CalculateDate(i);
	Dpd_Date.addItem(Var_Date); 
	if (i === 0 ) {Dpd_Date.setSelectedKey(Var_Date);};
  	i +++ 1;}
while (i <= 5);

 

When taking the date 03.12.2021 as current date, this is how the result looks like in the dropdown:

2.      Filter Chart on a specific date range

Another case where dates can be used is when charts should be filtered on specific date ranges dynamically. The easiest way is to do it via the Filter Range Option for the default date in the data model.

However, this is not very dynamically. I can filter the current day – 5 until the current day for example but I cannot put any condition on that (for example when I want to compare two dates as start dates and take the smaller date). Therefore, it can be useful to do it in scripting as well. Here is, how a date range can be set up and da Chart can be filtered with it. In this example I want to filter on today-5 days until today:

//get the start date and the date of today
var current_date = new Date(Date.now());
var date_new_millisec = current_date.setDate(current_date.getDate() - days_go_back);
var start_date = new Date(date_new_millisec);
//create the range and filter with it
var range = TimeRange.create(TimeRangeGranularity.Day, start_date,current_date);
Chart_1.getDataSource().setDimensionFilter("Date",range);

In this blog I shortly explained two different cases where dates can be used in scripting and how to handle the dates in the logics. In the next part, I will shorty explain, how the results of a chart can be used to trigger actions in an Analytica Application,  for example rewarding the users for the activity.

Link to SAC Analytic Application – Scripting for Beginners: Part 1 – How-To read and change Master Data of Dimensions: https://blogs.sap.com/2021/11/29/sac-analytic-application-scripting-for-beginners-part-1-how-to-read-and-change-master-data-of-dimensions/

Link to SAC Analytic Application – Scripting for Beginners: Part 2 – How-to hand over Parameters for Data Actions:  https://blogs.sap.com/2021/12/06/sac-analytic-application-scripting-for-beginners-part-2-how-to-hand-over-parameters-for-data-actions/

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x