Simple calculations on Salesforce Analytics Cloud

When looking at the most searched items on andrewprice.me I am finding that people are trying to find out how to do calculations on Analytics Cloud. For those vistors to the site there are a couple of ways to do calculations on the platform:

  1. Using the compare table, and adding a calculation as a column
  2. Use SAQL / PIGQL and create a calculation.

Compare Table Calulations

To create a compare table calculation it is very simple. Add a column that is a measure.

In this case I have chosen to calculate the sum of all flights.

Once the measure has been added to a column int the compare table , we then click the drop down arrow on the cell and choose add column.

Once the column has been added, we can then press the drop down again and scroll down and select "edit formula and format".

Once selected we have the option to add in a formula in the "Edit Columns" box. Each column is represented like a Excel Spreadsheet using the alphabet to detail the column names in the formula edit box. In this example I have created the below calculation.

Column A x 2 + Column A

Once the calculation has been added in the Formula Editor, press OK and this will be added to the compare table in a seperate column.

Using SAQL / PIGQL to create calculations:

SAQL is the Salesforce Analytics Cloud Query language. We use this query language in conjuntion with the UI framwork what we call the compact form. The SAQL query is used within a step in a dashboard and returns a result to the step , in which the compact form interprets and renders in eclair / the Salesforce Dashboard UI.

The calculation we are going to create is the same calculation we created in the compare table above however we will represent the outcome in a number widget.

  1. Use the cervello Salesforce Analytics Cloud SAQL Tester - (available here: )
  2. Get the Dataset ID using the details here:

Once we have the Dataset ID we can start to build out a query using the SAQL tester. To Start with we build out a query to get the sum of all flights within the dataset. This is a simple query and looks like the below:

Now we have the simple calculation of sum of flights, we now just need to adjust this to take the calculation in the above raw table and add it into the query.

We now add this to a step and then add that step to the number widget within the dashboard JSON. Below you can see this, it is important to make sure that the measures and groupings added / created in the JSON need to also be replicated in the compact form within the step.

Below is the dashboard functioning with the new calculation in SAQL rendering the number widget.

Using a Relative Date Static Step with with a Selection Bind in SAQL Pigql and Compact form Graphs

When building out dashboards for business consumption, there will be in most cases a requirement to have relative date filters and facet across various widgets on the dashboard ( A relative date for example is 3 months from today). A simple way to achieve this maybe to use the Date widget which can be easily added onto the dashboard from the widget toolbar, however another way is to provide a set of buttons that have static date ranges. This can result in a easier dashboard navigation experience, fixed date ranges, and makes it easier for end users to select date ranges that they would like / should only see.

To setup a widget such as the pillbox or the selector widget with relative date ranges you need a static step to drive the dates added as values on the widget. The picture below shows the outcome of a static step controlling two widgets, one widget which is being filtered via the static step in compact form , and the other filtered via pigql.

Below is the static step for the above dashboard. This is the step that is:

  1. Feeding the values of the buttons - everything in the "Values" section is a value,
  2. Detailing what should be displayed on the buttons per value under the "display" tag.

As you can see in each value there are two subordinate values with the parameters of:

  • saqlstepvalue
  • compactstepvalue

In each of the parameters there are the relative date ranges in the syntax required to the specific type of widget / query that will need to render their relative date range filter. The static step filter must be placed in the steps: section on a dashobard.

The below JSON is the code and syntax required to filter a compact form widget where there is no pigql passed to the widget. As you can see in the whole dashboard JSON at the bottom of the page, this filter must be added in the "query" section of a step. The filter is a selection bind in which it takes the value from the selected static step and passes it into the filter for the associated compact form step.

This next script is a pigql query with the outcomes of the query the same as the compact query above. Included in this query as well is a selection bind that takes the relative date selected in the static step and adds it into the query and therefore filters the associated widget.

Now that we have added a static step into our dashboard , and added the associated filters to both types of widgets / queries, we can see how they look in a completed dashboard. This dashboard is the JSON from the image above.

The JSON describes:

  1. Pillbox Widget connected to the static step
  2. Static Step with relative date ranges
  3. Compact from bar chart with relative date filtering
  4. Pigql query bar chart with relative date filtering

Using a Static Step to pass a SAQL / Pigql and Compact Query to a graph via Selection Binding

One of the things you may want to do in Salesforce Analytics cloud.. is to have a Static Step with Buttons attached to control a single graph on a dashboard. These buttons maybe attached to different queries some that can be done via compact form and some that must be done by pigql. This would mean that the static step will need to contain values for both a compact query and pigql query.

This is facilitated by the field binding, where the documentation can be found here

The first thing to start with is to build out the Static Step with both of the query types as seperate values, in my example you can see I have WaitTime and ServiceTime, and for each I have a field for the pigql query and the compact query:

Now that I have my Static Step setup with a piqql and compact field for each value of the Static step, I can now use these as selection bindings to graph these results. For graphs only needing compact form I just need to enter the 'compact' field into the compact query space on the graph, however graphs requiring the pigql query I need to add both the pigql and the compact form selection bindings to ensure that the graph renders.

To show context to the whole dashboard script, below is a full dashboard JSON. In this example there are 3 graphs and 1 selector. Two of the graphs have simple compact queries and dont have any selection binding attached. One of the graphs is controlled by the Pillbox static step and passes both the piql and compact query. We know this works because Pigql queries are always rendered before compact queries.

Below is the image of the final result after selecting the 'WaitTime' option on the Pillbox selector. The top two graphs are normal graphs and are not connected to the Pillbox static step selector however the bottom graph is controlled with the Pillbox static step selector. The top 2 graphs are included in this code, to show what the graphs are expecting in compact form.