Using Arrays in a Script to Enhance a Story

Objective

After completing this lesson, you will be able to use arrays to loop the results set.

Result Set Iteration

Visualization of a Result Set

Before we use the result set in script, we must ensure to understand the elements of the result set.

  • The Dimension Header contains dimension names.
  • In Column Header and Row Header, you find members that usually form a tree. The combination of all members on one axis is called axis tuple.
  • At the crossing point of a row and a column axis tuple, you find a data cell in the Data area.

You could combine the two tuples from rows and column to a larger tuple with the same effect: select a single data cell. Such a "large tuple" selection also survives some changes, for example, if a dimension is moved from rows to columns or if axes are swapped.

Such a "single tuple" approach is used in story design to select data cells.

A table divided into four, which corresponds to the list above. Top left: Dimension Header (purple). Top Right: Column Header (orange) Bottom Left: Row Header (green). Bottom Right: Data (yellow).

Reference of a Single Data Cell

You can reference a single data cell with the getData({selection}) function of a data source. To specify the cell, you pass a selection to the function. The selection is written in the common JSON format.

For each cell, you can get either of the following values:

  • Formatted value, for example, 10,803,914.00
  • Raw value, for example, 10803914

Reading of a Set of Selections

If you must work with more than one single data cell, you can work with selections. For example, you can read a set of selections into an array and then, to generate your desired visualization, loop over the array.

Reading a Cell Value from a Selection

Step 1: Read the desired selections into an array.

Step 2: Loop over the array and use a function. For example, use the getData({selection}) function to visualize/react on the data values.

The code from the code block above. With Selections (yellow) and cell (green) highlighted. The results are displayed at the bottom of the image.

Here is the code used in the example above:

Code Snippet
1234567891011
var selections = Table_1.getDataSource().getDataSelections({ [Alias.MeasureDimension]:"00O2SOEHXWHF3AU4LBEW8WZSX", "0D_NW_BP__0D_NW_CNTRY":"US" }); console.log(selections); for (var x=0; x<selections.length; x++ ){ var cell = Table_1.getDataSource().getData(selections[x]); console.log(cell.formattedValue); }

Reading a Dimension Member from a Selection

It's possible that only working with the values isn't enough because your users want to know the element to which the values belong.

You can do this with the getResultMember(dimension, {selection}) function, as shown in the following figure.

The code from the code block below. With Selections (yellow) and cell (green), year (blue) and country (blue) highlighted. The results are displayed at the bottom of the image.

Here is the code used in the example above:

Code Snippet
123456789101112
var selections = Table_1.getDataSource().getDataSelections({ [Alias.MeasureDimension]:"00O2SOEHXWHF3AU4LBEW8WZSX", "0D_NW_BP__0D_NW_CNTRY":"US" }); console.log(selections); for (var x=0; x<selections.length; x++ ){ var cell = Table_1.getDataSource().getData(selections[x]); var year = Table_1.getDataSource().getResultMember("0CALYEAR", selections[x]); var country = Table_1.getDataSource().getResultMember("0D_NW_BP__0D_NW_CNTRY", selections[x]); console.log(country.description + " / " + year.description + " / " + cell.formattedValue);}

Reading the Complete Result Set

As an alternative to what is shown so far, you can read the whole (or parts) of the result set into an array at once. For each cell, you get a full list of properties that you can reference in further script processing.

For example:

Code Snippet
123
var resultset = Table_1.getDataSource().getResultSet(); console.log(resultset);

To read the value in the example above, you would use:

Code Snippet
123
console.log(resultset[0][Alias.MeasureDimension].formattedValue);

The syntax for the getResultSet offers three optional parameters. If you don't specify any of these parameters, you retrieve the whole available result set.

Code Snippet
1
Table_1.getDataSource().getResultSet(selection?, offset?, limit?: integer)

  • selection Can be used to restrict the returned result set, for example, to a measure or dimension member.
  • offset Skips the offset cells before beginning to return the cells.
  • limit Constrains the number of returned cells.

Scripting Aliases

The scripting environment offers an Alias object that can be used to reference different standard objects. The available objects are as follows:

  • Alias.DefaultTheme
  • Alias.FlatHierarchy
  • Alias.MeasureDimension
  • Alias.NullMember
  • Alias.TotalsMember

To reference the result member in script you can use the alias Alias.TotalsMember.

Example of code using Alias.TotalsMember.

To reference the measure dimension in script you can use the alias Alias.MeasureDimension.

Example of code using Alias.MeasureDimension.

Loop the Result Set

Business Scenario: Your team makes a request to add a leader board to the story. They want show the top three countries visually and the remaining countries in a table underneath the image. They want all visualized data to update automatically as soon as the data changes, for example, while a user selects a year.

The story you will use has already been created with visualization widgets and some of the required scripting, as in this exercise you will only write a script function to loop over the result set and update all text fields accordingly. This function is required wherever values need to be kept up to date.

This video will walk you through the final story and scripting required.

Simulate the Steps

The practice exercise below simulates being in Advanced Mode in SAP Analytics Cloud and takes you through using arrays to loop the results set as shown in the demo video.

Task Flow: In this practice exercise, you will:

  • Add scripting to the page and the table so that the text widgets update when the story is opened or when the table results change
  • Develop the script to visualize the leader board
  • Enhance the script function to visualize the tablular view
  • Test your story in view mode

Log in to track your progress & complete quizzes