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 the user flexibility to change chart grouping, for 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 for 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 the 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 to change Grouping

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

When previewing the dashboard, if the selected field in toggle different from 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 to 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": "{{cell(static_2.selection, 0, \"value\").asString()}}"



Binding for Date

In this scenario, we would like to give users the flexibility to view a timeline chart by week and by day. Sure we need binding for this.

Here is the static step:
                    {
                        "display": "Day",
                        "value": [
                            "TIMESTAMP_Year",
                            "TIMESTAMP_Month",
                            "TIMESTAMP_Day"
                        ]
                    },
                    {
                        "display": "Week",
                        "value": [
                            "TIMESTAMP_Year",
                            "TIMESTAMP_Week"
                        ]
                    }

and change step query for "groups" 
from: 
"groups": [
                        [
                            "TIMESTAMP_Year",
                            "TIMESTAMP_Month",
                            "TIMESTAMP_Day"
                        ]
                    ]
to:
"groups": [
   "{{cell(staticTime_1.selection, 0, \"value\").asObject()}}"
          ]
* staticTime_1 is the step name


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":[
             "{{cell(static_1.selection, 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": [
             "{{cell(static_1.selection, 0, \"value\").asString()}}"
            ],
  "order": [
            [
              "count",
              {
                "ascending": "{{cell(sort_1.selection, 0, \"value\").asObject()}}"
              }
            ]
           ]
}



B. Binding for table
We also can use a toggle to sort a table widget.

Here is a sample with JSON snippet from steps:
  "query": {  
           "values": [  
             "Region__c",
             "Skill__c",
             "Name",
             "Id",
             "LastModifiedDate"  
           ],  
           "order": [  
             [  
               "{{ cell(static_2.selection, 0, \"value\").asString() }}"               
               {  
                  "ascending": "{{ cell(sort_2.selection, 0, \"value\").asObject }}"
               }  
             ]  
           ]  
         }  

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 order of the field:
     "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 are not in the double quote ""

Table result:


Here is the full JSON for the dashboard.


Side note: old-style binding
"{{ value(selection(static_2)) }}" 
this is equal to
"{{ cell(static_2.selection, 0, \"value\").asObject() }}"



Reference:

6 comments:

  1. Really good article! I have one question regarding the "Binding to change Chart type" section.

    I am trying actually without any success for now to have a toggle to switch from Map view to Hbar view. Have you implemented something similar and do you know if it is possible?

    ReplyDelete
    Replies
    1. I have not do binding for map chart, you need to know all parameters needed for map and compare the parameters for Hbar, and combine all the parameters into the JSON dashboard.

      Delete
  2. hi @Johan Yu - greatewxplanation on this complex topic. I wa looking for the explanation/information for binding Date Picker widget to a chart in SF Analytics. It seems to me that the official documentation is not really up to date since I couldn't reproduce their scenario. Do you have any experience with binding Date Picker widget by any chance?

    ReplyDelete
    Replies
    1. Hello, have you read Rikke's blog on binding?
      http://www.salesforceblogger.com/2019/01/14/einstein-analytics-demystifying-bindings-part-4/

      Delete
  3. hi @Johan Yu - greate xplanation on this complex topic. I wa looking for the explanation/information for binding Date Picker widget to a chart in SF Analytics. It seems to me that the official documentation is not really up to date since I couldn't reproduce their scenario. Do you have any experience with binding Date Picker widget by any chance?

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete

Page-level ad