What-if scenario is a common use case in the analytics world. With SAP Analytics Cloud you get the perfect solution to handle this scenario. To illustrate this, my colleague Nick Verhoeven and I have created a guided application released in the SAC sample content. In this blog I will explain how to build the application.
Find the functional explanation from my colleague here.
The application is composed of 4 tabs. In the first one we only have a text explaining how to use the application.
Create Scenario Tab
In the 2nd tab "Create Scenario" a user can select a planning source version and create a new version based on the source version.
The available source versions are retrieved by using the following code in the "onInitialization" event
var versions = Table_1.getDataSource().getMembers("Version");
while (versions.length>0)
{
var value = versions.pop();
Dropdown_3.addItem(value.id,value.description);
}
Note that Table_1 is not present in the Tab2 but in Tab3. The Table_1 contains the "Version" dimension in the column feed.
Once the version is selected the user gives a name to the new version and creates it.
So first we create a private version with the given name:
var key = Dropdown_3.getSelectedText();
var text = Dropdown_3.getSelectedKey();
var name = InputField_1.getValue();
var res = Table_1.getPlanning().getPublicVersion(key).copy(name,PlanningCopyOption.AllData,PlanningCategory.Forecast);
Then we publish this private version:
var bool = Table_1.getPlanning().getPrivateVersion(name).publishAs(name,PlanningCategory.Forecast);
In the 3rd tab we have tables and a chart that show the "Account" dimension for a version. As we want to see the values for the new created version and the source version, we filter these widgets:
var id = Table_1.getPlanning().getPublicVersion(name).getId();
Table_1.getDataSource().setDimensionFilter("Version",[id,text]);
Table_2.getDataSource().setDimensionFilter("Version",[id]);
Table_3.getDataSource().setDimensionFilter("Version",[id,text]);
Chart_2.getDataSource().setDimensionFilter("Version",[text,id]);
Finally, we are redirected to the 3rd tab.
Run Scenario Tab
This tab contains a lot of elements. At the top right we have a table containing the different drivers which already exist and which Account they influence. A user can add a new driver by clicking on the "Add driver button". By doing so he will be prompted (popup component) to provide information regarding the new driver: Description, Impact in %, From Date, To Date and Influence Account. By clicking on the ok button, a new driver will be added in the table.
In term of code, it corresponds first to add a new member in the model for the "Drivers" dimension. For that we create a Planning Model’s technical component called "Model".
From the popup we retrieve the values entered for the Description, From Date, To Date and Influence Account in order to create a new PlanningModelMember object. With the createMembers() method the new member is added to the SAC model.
Member = {id:ID.getValue(),
description:ID.getValue(),
properties:{FromDate: result1,
ToDate: result2,
InfluencedAccount:InfluencedAccount
}
};
var result = Model.createMembers("Drivers", Member);
To add this new member in the table we use the following code
var selection = {
"Drivers": ID.getValue() ,
"Version": "public."+InputField_1.getValue()
};
var test = Table_2.getPlanning().setUserInput(selection, InputField_2.getValue());
planning.submitData();
var ds = Table_2.getDataSource();
Application.refreshData([ds]);
Once the driver has been created, a user can run a data action to calculate the impact on the influence account. Without going into full detail on Data Actions, the Analytics Designer is able to call below data action. The action is filtering the accounts to influence based on the driver dimension with the created drivers. It will multiply the account with the entered driver values within the maintained dates.”
To execute the data action we use the following code in the application
DataAction_1.setParameterValue('TargetVersion', ScenarioVersionId);
DataAction_1.execute();
Table_1.getDataSource().refreshData();
Chart_2.getDataSource().refreshData();
Where the ScenarioVersionID is the id of the new version.
Compare Scenario Tab
In the last tab we compare the source scenario with the new scenario.
The result is display in a table. From this table we can open the navigation panel to explore more details with the following code:
Table_3.openNavigationPanel();
Or we can jump to a SAP Analytics Cloud story. When the user clicks on the " Open Formatted Report" button it opens a popup.
The user publishes the version that has been impacted in the previous tab and selects the versions he wants to compare.
Table_3.getPlanning().getPublicVersion(ver).publish();
var str ="[" + "\"" + key + "\",";
var values = CheckboxGroup_1.getSelectedKeys();
for (var i = 0 ; i < values.length; i++)
{
if (i===values.length - 1)
{
str = str + "\"" +values[i] + "\"";
}else{
str = str + "\"" +values[i] + "\",";
}
}
str = str + "]";
NavigationUtils.openStory("7CD022064D158FDA3FA2F0EA7E0FFF24", "b7e51a0e-4d62-4592-b4e7-66843fa6af41",
[UrlParameter.create("mode","view"),
UrlParameter.create("f01Model","t.L.Ccj19hr3crpneq1800x53ajzeo:Ccj19hr3crpneq1800x53ajzeo"),
UrlParameter.create("f01Dim","Version"),
UrlParameter.create("f01Val",str)],
true);
Where the “str” string corresponds to the versions selected. To jump to the story we use the openStory() method for which we pass different parameters (see the code).
In conclusion we were able to see that with the Analytics Designer capability in SAP Analytics Cloud, you can create a What-if application on top of a planning model. I hope you enjoy this blog and stay tuned for more blogs on this topic!
https://www.youtube.com/watch?v=qxOpskcSM30&t=9s