SAC Analytic Application – Scripting for Beginners : Part 3 – How-to handle and calculate Dates

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.

Fig. 1
Fig. 2

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”.

Fig. 3

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

Fig. 4

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:

Fig. 5

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:

Fig. 6

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

Fig. 7
Fig. 8

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.

Fig. 9

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:

Fig. 10

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.

Related Blogs