🇨🇦 SAP Analytics Cloud – How to Simplify Input Control Management with .setSelectedMembers API

Hey everyone!

SAP has added a very useful API to set active dimension members on input controls to SAP Analytics Cloud called .setSelectedMembers().

This might not have grabbed all the attention it deserves, but trust me, it’s a game-changer when it comes to managing input controls.

In this brief tutorial, I’ll walk you through the capabilities of the .setSelectedMembers() API using two examples. Imagine you have a dashboard with tables linked to multiple data models, and to make things more challenging, these tables have restricted measures. Dealing with numerous custom input controls can be a real pain, right?

Here’s the deal: You can easily fetch the members selected from just one input control, store them in a variable, and then effortlessly pass them along to other input controls using the .setSelectedMembers() API. No more wasting time flipping selections in many different input controls.

Here’s a simple example first:

Example 1

Let’s say, for example, that you have two input controls (filters) in your story, and they are linked to two different models that contain the same Version dimension. The goal here is to ensure that the content of the first input control matches that of the second.

To achieve this, follow these steps:

  • Click on the fx button of the first input control that you want to select (Version_1).
  • Write the following code onSelect:
//Get selected member from Input control Version_1
var inputVersion = this.getInputControlDataSource().getActiveSelectedMembers();

//Set the same version to the second Input control Version_2.
Version_2.getInputControlDataSource().setSelectedMembers(inputVersion);

As explained in the comments, line 2 defines a variable to store the selected member, and line 5 passes it to the other input control. This method can be applied even if you have more than two input controls.

After saving the story, switch to View mode.

Here’s the final output in view mode:

Example 2

Now, let’s explore a more advanced example that adds some logic to the way we control the second input control.

In this scenario, we have two Date input controls. However, instead of having the same selection for both always, we want to ensure that the Date_1 filter drives the second one, while also ensuring that the Date_2 filter always represents the month prior to Date_1.

To achieve this, follow these steps:

  • Click on the fx button of the first input control that you want to select (Date_1).

Write the following code onSelect:

  • Get the selected member from the Input control Date_1.
var inputMonth = this.getInputControlDataSource().getActiveSelectedMembers();
  • Extract the year and month from the input control ID, which looks something like this: [Date].[YM].&[YYYYMM].
var year = inputMonth[0].id.slice(-7, -3);
var month = inputMonth[0].id.slice(-3, -1);
  • Convert the extracted strings to integers.
var yearInt = ConvertUtils.stringToInteger(year);
var monthInt = ConvertUtils.stringToInteger(month);
  • Calculate the previous month.
if (monthInt === 1) {
  yearInt--;
  monthInt = 12;
} else {
  monthInt--;
}
  • Check if the month is lower than 10 and add a leading zero if necessary.
if (monthInt < 10) {
  month = "0".concat(ConvertUtils.numberToString(monthInt));
}
  • Create the previousMonth variable by combining the year and month.
var previousMonth = ConvertUtils.numberToString(yearInt).concat(month);
  • Set the previousMonth to the second Input control Date_2.
Date_2.getInputControlDataSource().setSelectedMembers(previousMonth);
  • This code snippet grabs the ID of the selected member on the Date_1 filter, manipulates the strings to extract the previous month, and then passes it to the second input control. Imagine the possibilities this approach offers for different applications!

Here’s the complete code snippet:

//Get selected member from Input control Date_1
var inputMonth = this.getInputControlDataSource().getActiveSelectedMembers();

//Extract year and month from the input control id that looking something like this: [Date].[YM].&[YYYYMM]
var year = inputMonth[0].id.slice(-7, -3);
var month = inputMonth[0].id.slice(-3, -1);

//Convert strings to Integers
var yearInt = ConvertUtils.stringToInteger(year);
var monthInt = ConvertUtils.stringToInteger(month);

//Calculates previous month
if (monthInt === 1) {
  yearInt --;
  monthInt = 12;
} else {
  monthInt --;
}

//If statement checks if month is lower than 10 and adds leading zero if necessary
if (monthInt < 10) {
	month = "0".concat(ConvertUtils.numberToString(monthInt));

	var previousMonth = ConvertUtils.numberToString(yearInt).concat(month);
}

//Set the previousMonth to the second Input control Date_2
Date_2.getInputControlDataSource().setSelectedMembers(previousMonth);

After saving the story, switch to View mode. Here’s the final output in action:

Limitations

As mentioned by Orla Cullen here. This API does not support input controls that show:​

  • Unbooked members
  • Excluded members
  • Range filters and advanced filters

I hope you found these examples helpful. If you have any questions or suggestions, please leave a comment below.

Thanks for reading, and see you next time!

Jorge Rocha

Article in Brazilian Portuguese

2 thoughts to “🇨🇦 SAP Analytics Cloud – How to Simplify Input Control Management with .setSelectedMembers API”

Comments are closed.