Pages

Monday, May 14, 2018

Einstein Analytics: The quest for Binding in Dashboard

This blog is written in Summer '18 release, so it may become invalid when Salesforce makes "binding" become more user-friendly in the future.

Until Summer '18 release, if you would like to implement binding in the dashboard, you need to manually edit the dashboard JSON.

Use case: you would like to give user flexibility to change chart grouping, example: group by Region, or by Country, or by Status. Another use case, the user would like to have the flexibility to change chart type without the need to edit the dashboard or would like to implement both in the same dashboard, this makes sense, when you change grouping, the chart type probably needs to change for better visualization.

This blog will not share on how to create binding or static with toggle, you can watch the awesome Peter Lyon's videos Binding Basic, and Rikke Hovgaard's blog The power of static steps.

A. Binding to Chart
In this sample, I have one chart and 3 toggles:
- Chart
   Step Id: step_1
- Field Names for grouping
   Step Id: static_1
- Chart Type to change visualization
   Step Id: static_2
- Order by ascending or descending
   Step Id: sort_1



This is how the dashboard looks


To make the binding work, I am going to edit the JSON with Ctrl-E.

Binding for Grouping
1. Change "groups" query under "step_1"
change from
"groups": ["field_name"]
change to
"groups": ["{{coalesce(cell(static_1.selection, 0, \"value\"), cell(static_1.result, 0, \"value\")).asString()}}"]

When previewing the dashboard, if the selected field in toggle different with the field use in the initial value for grouping, the chart will error:



2. Change "columnMap" in "widgets"
"columnMap": null OR delete the whole columnMap


Binding for change Chart Type
1. Change "columnMap" in "widgets" (if you have not done it)
"columnMap": null OR delete the whole columnMap

2. Change "visualizationType" under "parameters" in "widgets" 
change from
"visualizationType": "hbar"
change to
"visualizationType": "{{coalesce(cell(static_2.selection, 0, \"value\"), cell(static_2.result, 0, \"value\")).asString()}}"



Binding for Order

1ST OPTION  - put "ascending" in static
1. Edit the static step under "sort_1"
change from 
"values": [
           {"display": "Asc","value": "true"},
           {"display": "Desc","value": "false"}
          ]
change to 
"values": [
           {
             "display": "Asc","value": 
               [
                 -1,
                 {"ascending": true}
               ]
           },
           {
             "display": "Desc","value": 
               [
                 -1,
                 {"ascending": false}
               ]
           }
          ]

2. add "order" query under "step_1"
"query": 

  "measures": [["count","*"]],
  "groups": [
             "{{ coalesce(cell(static_1.selection, 0, \"value\"),                    cell(static_1.result, 0, \"value\")).asString()
              }}"
            ],
  "order": "{{
             column(sort_1.selection,[\"value\"]).asObject()
            }}"
}


2ND OPTION - put "ascending" in query order
1. Edit the static step under "sort_1"
change from 
"values": [
           {"display": "Asc","value": "true"},
           {"display": "Desc","value": "false"}
          ]
change to 
"values": [
            {
              "display": "Asc",
              "value": true
            },
            {
              "display": "Desc",
              "value": false
            }
          ]

2. add "order" query under "step_1"
"query": 

  "measures": [["count","*"]],
  "groups": [
             "{{ coalesce(cell(static_1.selection, 0, \"value\"),                    cell(static_1.result, 0, \"value\")).asString()
              }}"
            ],
  "order": [
            [
              "count",
              {
                "ascending": "{{coalesce(cell(sort_1.selection, 0, \"value\"), cell(sort_1.result, 0, \"value\")).asObject()}}"
              }
            ]
           ]
}



B. Binding table
We also can use a toggle to sort table widget, but this will also cause, out of the box feature to sort the table by clicking table header will no longer work. So, this is particularly needed when the SAQL has been edited.

Here is another sample with JSON snippet from steps:
  "query": {  
           "values": [  
             "Region__c",
             "Skill__c",
             "Name",
             "Id",
             "LastModifiedDate"  
           ],  
           "order": [  
             [  
               "{{ value(selection(static_2)) }}",  
               {  
                 "ascending": "{{ value(selection(sort_2)) }}"  
               }  
             ]  
           ]  
         }  

This is the JSON step for the fields selection:
      "static_2": {  
         "broadcastFacet": true,  
         "label": "static 2",  
         "selectMode": "singlerequired",  
         "type": "staticflex",  
         "values": [             {  
             "display": "Region",  
             "value": "Region__c"  
           },  
           {  
             "display": "Status",  
             "value": "Status__c"  
           }  
         ]  
       }  

This is the JSON step for the fields order:
     "sort_2": {  
         "type": "staticflex",  
         "broadcastFacet": true,  
         "selectMode": "singlerequired",  
         "label": "sort 2",  
         "values": [  
           {  
             "display": "Ascending",  
             "value": true  
           },  
           {  
             "display": "Descending",  
             "value": false  
           }  
         ]  
       }  
notice that true and false not in the double quote ""

Table result:


Here is the full JSON for the dashboard.