Pages

Monday, June 25, 2018

Einstein Analytics: Binding syntax

Binding is one of the most powerful technique to implement when building a dashboard, although it is also one of the most difficult as you need to edit the JSON directly. In a few blogs post earlier here and here, you may notice a function call coalesce(), let's see what is this function do before we jump further into the binding technique.

coalesce function returns the first non-null source from a list of sources. This function is useful for providing a default value in case function returns a null value.
syntaxcoalesce(source1, source2,...)
example: coalesce(cell(step1.selection, 0, "column1"), "green")
  output: the output is the result returned by cell(step1.selection, 0, "column1"). However, if cell(step1.selection, 0, "column1") returns null, then the output is "green".


We can use cell, row and column function for binding, let's see each of them:
cell function returns a single cell of data as a scalar, like "This salesperson rocks", 2, or null
syntax: cell(source, rowIndex, columnName)
An error occurs if the rowIndex is not an integer, the columnName is not a string, or the cell doesn’t exist in the table.
example: assume that following rows from the step.
    {stateName: ‘CA’, Amount:100}, 
    {stateName: ‘TX’, Amount:200}, 
    {stateName: ‘OR’, Amount:300}, 
    {stateName: ‘AL’, Amount:400}, 

Although Einstein Analytics doesn’t store this data as a table, let’s show the data in this format to make it easier to understand the example
(row index) stateName Amount
0 CA 100 
TX 120 
OR  115 
AL  105 

sample function: cell(myStep.selection, 1, "stateName")
result: "TX"


column function returns one column of data (as a one-dimensional array) or multiple columns of data (as a two-dimensional array) -- allow users to select multiple values.
Syntax: column(source), [columnNames...])

Let's use similar step with cell functions above
    {stateName: ‘CA’, Amount:100}, 
    {stateName: ‘TX’, Amount:200}, 
    {stateName: ‘OR’, Amount:300}, 
    {stateName: ‘AL’, Amount:400}, 

And here is the presentation virtual table format.
(row index) stateName Amount
0 CA 100 
TX 120 
OR  115 
AL  105 

sample function: column(myStep.selection, "stateName")
result: ["CA", "TX", "OR", "AL"]


Now, let us continue with binding. There are two types of bindings: selection binding and results binding, the selection or results of one step triggers updates in other steps in the dashboard.

Selection binding is a method used to update a step based on the selection in another step. Selection bindings are interaction-driven, where it’s evaluated each time the user selects something in a widget.

Results binding is a method used to update a step based on the results of another step.

syntax: {{ cell(<stepName>.<result|selection>,<rowIndex>,<columnName>).asString() }}
example: {{ cell(Static_Step_1.selection, 0, \"value\").asString() }}
--> from above is from 1st row
--> {{ }} = binding

syntax: {{ column(<stepName>.<result|selection>,<columnName>).<asString()|asObject()> }}
example: {{ column(Static_Step_1.selection, \"value\").asObject() }}

If you saw that coalesce function is to return the first not null function, we can combine it in our cell binding.
{{ coalesce(cell(Static_Step_1.selection, 0, \"value\"), cell(Static_Step_1.result, 0, \"value\")).asString() }}


From above samples, there is also 2 more functions asString() and asObject()
asString() function serializes a scalar, one-dimensional array, or two-dimensional array as a string, escapes double quotes in strings.
syntax: <input data>.asString()
example: cell(stepOpportunity.selection, 1, \"measure\").asString()
--> 1 from above is from 2nd row
{{ cell(color_1.result, 0, \"color\").asString() }}


asObject() function passes data through with no serialization, returns data as an object (an array of strings).
syntax: <input data>.asObject()
example: column(StaticMeasureNamesStep.selection, [\"value\"]).asObject()
{{ cell(static_1.selection, 0, \"value\").asObject() }}



Reference: