Einstein Analytics – ABSYZ https://absyz.com Salesforce Gold Consulting and Implementation Partner Fri, 27 Nov 2020 10:52:20 +0000 en-US hourly 1 https://absyz.com/wp-content/uploads/2020/06/cropped-favicon-1-1-32x32.png Einstein Analytics – ABSYZ https://absyz.com 32 32 REST API call from Einstein Analytics Dashboard https://absyz.com/rest-api-call-from-einstein-analytics-dashboard/ https://absyz.com/rest-api-call-from-einstein-analytics-dashboard/#comments Tue, 26 May 2020 06:22:34 +0000 http://blogs.absyz.com/?p=11219

In Einstein Analytics we can create a lens and dashboard with the dataset available in your analytics studio. You have a dataset that is created from the dataflow which is scheduled every hour to update the dataset. Here your dataset might not have the updated data for every second or minute. What if you want to get the live data from Salesforce or any external system or handling complex solution when it is not possible in analytics to be shown on your dashboard. Using the apex step type in Dashboard JSON we can achieve displaying the manipulated data or real-time data.

Using SOQL from Dashboard

Scenario 1: The user wanted to show the real-time data from salesforce on the analytics dashboard. It is simple to achieve, just follow the steps mentioned. So we create an apex step in the dashboard and using ‘@restresource’ in apex call we fetch the data to the dashboard. Create an apex class as AnalyticsDashboardStep with restResource name as accountdata. Define a @HttpPost method that will return the value to the dashboard JSON. So your code will look as shown below.

@RestResource(urlMapping='/accountdata')
global with sharing class AnalyticsDashboardStep {
    @HttpPost 
    global static String fetchAccount(String selectedIndustry) { 
         //selectedIndustry - attribute value passed from the analytics Dashboard
         //return the output
    } 
}

Define a wrapper that parses the data to the dashboard JSON. The WrappedData wrapper creates a mapping between the parameters defined and the queried fields of account. The ReturnMetadata wrapper is to define the data type for each returned column. The ChartFormatJSON wrapper is to combine the data(rows) and header(columns).

    public class WrappedData{
        public String Account_Name;
        public String Account_Id;
        public String Account_Industry;
        public String Account_AccountSource;
        public Decimal Account_AnnualRevenue;
        public WrappedData(){}
        public WrappedData(Account data){
            this.Account_Name = data.name;
            this.Account_Id = data.Id;
            this.Account_Industry = data.Industry;
            this.Account_AccountSource = data.AccountSource;
            this.Account_AnnualRevenue = data.AnnualRevenue;
        }
    }
    public class ReturnMetadata {
        public List<String> strings; // columns that return as text
        public List<String> numbers; // columns that return as numeric
        public List<String> groups;  // columns that return as groups
        public ReturnMetadata(List<String> strings, List<String> numbers, List<String> groups) {
            this.strings = strings;
            this.numbers = numbers;
            this.groups = groups;
        }
    }
    public class ChartFormatJSON {
        public List<WrappedData> data;
        public ReturnMetadata metadata;
        public ChartFormatJSON(List<WrappedData> data) {
            this.data = data;
            this.metadata = new ReturnMetadata(new List<String>{'Account_Id','Account_Name','Account_Industry'}, 
                                                new List<String>{'Account_AnnualRevenue'}, new List<String>{'Account_Name'});
        }   
    }

Since your wrapper parameters are set up, now pass the values in the @HttpPost fetchAccount method as stated above. In the below method we have a queried List of accounts as a return statement to the Dashboard. For the complete class check this link.

    @HttpPost 
    global static String fetchAccount(String selectedIndustry) {
        List<Account> dataDisplay = new List<Account>();
        List<WrappedData> wrpData = new List<WrappedData>();
        // If the Industry is not selected from the interaction step
        if (selectedIndustry == null) {
            dataDisplay = [select Id,Name,Industry,AnnualRevenue,AccountSource from account order by AnnualRevenue desc];
        }else{
            dataDisplay = [select Id,Name,Industry,AnnualRevenue,AccountSource from account where industry=:selectedIndustry order by AnnualRevenue desc];
        }
        for(Account acc : dataDisplay){
            wrpData.add(new WrappedData(acc));
        }  
        //Serialize the wrapper that you have created with account data
        return JSON.serialize(new ChartFormatJSON(wrpData));
    }

Once your apex class is ready, move to the analytics studio to create a dashboard with an apex step. Create a toggle with filter values as Account Industry. Follow the below steps to create a dashboard.

In the new dashboard if you have completed the above steps, click ctrl + E in windows or command + E in Mac to edit your Dashboard JSON. Add the apex step as shown below. The GetChartData is the query name in the dashboard. In the query parameter, you set the body value as an apex input parameter named selectedIndustry that holds the value selected from the dashboard. Define the apex class name in the path parameter and apex in the type parameter.

"GetChartData": {
	"query": {
		"body": {
			"selectedIndustry": "Agricultrue"
		},
		"path": "accountdata"
	},
	"type": "apex"
},

If you want to pass the selected industry value dynamically then use interaction syntax that can be generated from the advance editor of the GetChartData query. Edit your query value with “{{cell(Industry_1.selection, 0, \”Industry\”).asString()}}” to pass the value dynamic. You can use both Result or Selection type of interaction.

"GetChartData": {
     "query": {
          "body": {
                "selectedIndustry": "{{cell(Industry_1.selection, 0, \"Industry\").asString()}}"
          },
          "path": "accountdata"
     },
     "type": "apex"
},

Click on Done after completing the JSON edit. You can see the GetChartData query in the query list on your right-hand side of the dashboard. Drag the query to the dashboard building area. To get the full dashboard JSON click here.

Dashboard Output:

dashboard output1

REST API call from Dashboard

Scenario 2: Similarly, the user wanted to show the live updates from an external system to your analytics dashboard, then we do an apex REST API call to fetch the details whenever the dashboard is loading or refreshing. Here we have taken an example of COVID ’19 to show the number of updated cases in India. So we are using https://api.covid19india.org API to fetch the COVID updated details. Similarly, you can choose your API according to your necessity.

It is similar to scenario 1, whereas here we are creating an apex class with REST API callout and passing the parameters in the same format that is required for the dashboard. Add the URL in remote site settings.

Create two custom labels with the details mentioned below:

  • Custom Label 1
    • Name: CovidBaseEndpoint
    • Value: https://api.covid19india.org/
  • Custom Label 2
    • Name: CovidStateWise
    • Value: /data.json

CustomLabel

The next step is to create an apex class to make an Http request and get the response. The getStateWiseData method makes an API request and is serialized to the dashboard through the data_val method. Notice PackagedReturnItem wrapper where we have categorized the columns as a string, number, and group in ReturnMetadata.

@RestResource(urlMapping='/covid')
global with sharing class CovidData {
    @HttpPost  // Annotation Specified to highlight that this method needs to be called.
    global static String data_val() {
    	CovidStatusCoreData1 data = getStateWiseData();
        return JSON.serialize(new PackagedReturnItem(data.statewise));
    }
    
    public static CovidStatusCoreData1 getStateWiseData() {
        String BaseEndpoint = System.Label.covidBaseEndpoint; //Retrieve the endpoint and statewise variable from custom label
        String StateWise = System.Label.covidStateWise;
        HttpResponse resp = makeAPICallout(BaseEndpoint,StateWise);
        CovidStatusCoreData1 response = (CovidStatusCoreData1)System.JSON.deserialize(resp.getbody(), CovidStatusCoreData1.class);
        if (response != null) {
            return response;
        }
        return null;
    }
    public static HttpResponse makeAPICallout(String BaseEndpoint,String StateWise) {
        Http h = new Http();			//Make a request with the parameters set
        HttpRequest req = new HttpRequest();
        String endpoint = BaseEndpoint + StateWise;
        req.setEndpoint(endpoint);
        req.setMethod('GET');
        HttpResponse res = h.send(req);		// Send the request, and return a response
        if (res.getStatusCode() == 200 ) {
            return res;
        }
        return null;
    }
    public class ReturnMetadata {
        public List<String> strings; 
        public List<String> numbers; 
        public List<String> groups;  
        public ReturnMetadata(List<String> strings, List<String> numbers, List<String> groups) {
            this.strings = strings;
            this.numbers = numbers;
            this.groups = groups;
        }
    }
    public class PackagedReturnItem {
        public List<StateWiseData> data;
        public ReturnMetadata metadata;
        public PackagedReturnItem(List<StateWiseData> data) {
            this.data = data;
            this.metadata = new ReturnMetadata(new List<String>{'state','statecode','lastupdatedtime'}, 
                                               new List<String>{'active','recovered','deaths','confirmed','deltaconfirmed','deltadeaths','deltarecovered'}, 
                                               new List<String>{'state'});
        }   
    }  
    public class CovidStatusCoreData1 {
        public List<DailyKeyValues> key_values;
        public List<StateWiseData> statewise;
    }
    public class DailyKeyValues {
        public String confirmeddelta;
        public String counterforautotimeupdate;
        public String deceaseddelta;
        public String lastupdatedtime;
        public String recovereddelta;
        public String statesdelta;
    }
    public class StateWiseData {
        public Integer active;
        public String confirmed;
        public String deaths;
        public String recovered;
        public String state;
        public String statecode;
        public String lastupdatedtime;
        public String deltaconfirmed;
        public String deltadeaths;
        public String deltarecovered;
    }
}

Create an apex step GetChartData in dashboard JSON to display the data. After placing the apex step click on done from the dashboard. Place the query on top of the table widget as shown below.

"GetChartData": {
	"query": {
		"body": {},
		"path": "covid"
	},
	"type": "apex"
},

covid states

In the final step, we have created a Static step to filter by states in India. To show a different type of output chart in the dashboard pass the wrapper from the apex class. Set the ReturnMetadata string, number, and group by columns correctly so that you could see the correct output when you place the query in the chart widget. Likewise, create the apex classes to fetch the data and display it in different chart types as shown in the below dashboard. Refer to the link for the apex classes and dashboard JSON. Using the dashboard inspector we can check the output for each lens that helps us to identify the performance and time taken for the query. Click on show details for a query that will show the details on your right-hand side panel.

There you go! you are on the last step to verify your dashboard, check the clip below.

[wpvideo RHIjNB4j]

NOTE: When you work with API apex step in analytics dashboard remember you have certain limits provided by salesforce that you can refer here. Firstly, Maximum concurrent Analytics API calls per org limit as 100. Secondly, Maximum Analytics API calls per user per hour limit as 10,000.

]]>
https://absyz.com/rest-api-call-from-einstein-analytics-dashboard/feed/ 3
Einstein Analytics: Creating Date Fields in Recipes for Toggles https://absyz.com/einstein-analytics-creating-date-fields-in-recipes-for-toggles/ https://absyz.com/einstein-analytics-creating-date-fields-in-recipes-for-toggles/#respond Tue, 04 Dec 2018 12:32:16 +0000 http://blogs.absyz.com/?p=9489
Originally Posted on December 4, 2018; Last updated on November 5, 2020.

Einstein Analytics by Salesforce is a cloud-based AI-powered advanced analytics tool that helps in exploring data quickly and with Salesforce Analytics Query Language (SAQL) to query, manage your datasets and customize dashboards programmatically. Formerly known as Wave Analytics Cloud, it integrated very well with the Salesforce platform. We have already covered Wave Analytics in detail in our previous blog post “Introduction to Salesforce Wave Analytics Cloud”

Update: Einstein Analytics has a new name now, Tableau CRM, the tool remains the same. Read about this major update here

Einstein Analytics dashboard is built up of many widgets, which is similar to a component over the Salesforce dashboard; however, a date filter or list filter can also be referred to as widgets in EA. For the purpose of our tutorial, we would be using the Toggle widget, which can be used to create a filter for the viewers on Analytics dashboard results based on date or dimension. 

In the following example, we will create a filter for the Users on the date field as Quarterly and Yearly. 

To begin, we will create a Dataset. In EA, a Dataset is a specific view of a data source based on how you’ve customised it. We’ve already covered steps to create a Dataset in “How to create a Dataset, Lens and Dashboard in Wave Analytics”. After creating the dataset, create a Dataset Recipe as shown below. You can find the created recipe in the Dataflows & Recipes from the sidebar menu.

Datasets Einstein Analytics
Datasets in Einstein Analytics / Tableau

 

 

create recipe tableau einstein analytics
Create Recipe Option – Einstein Analytics / Tableau

Recipe Name einstein Analytics
Provide Recipe Name – Einstein Analytics / Tableau

List of Dataflows & Recipes
List of Data Flows & Recipes – Einstein Analytics

Create a Bucket

Click on the Recipe, you will be redirected to a table that shows all the data. We write the filter on the Close Date field in Opportunity. Create a bucket to categorise the fields.

Customising a Recipe
Recipe Editor / Customisation Pane

Selecting the row
Row for the Bucket to be Created

selecting bucket from data fields
Creating Bucket from Data fields – Recipe – Einstein Analytics

You have 2 options shown while creating a Bucket. Choose the Relative option. A drop-down list appears below with the options by Year, Quarter, Month, Week or Day. Below you have another option to choose your range provided namings for the start and end of your selected type.

Customising the Bucket
Bucket Customisation Pane – Select Relative.

analytics9analytics10

Now create two buckets: By Year and By Quarter. Select the range and name each range that you want to display. As you create each bucket, add the bucket to the Dataset and update as shown below. The Dataset Recipe will get updated and can be monitored from the Data Manager. In the left side panel click on the monitor tab, where you can check the status.

adding additional conditions buckets
Additional Conditions – Bucket

 

applying changes bucket remaining values
Select Bucket Remaining Values to Other and Apply changes to Bucket

 

Dataset reciple table preview selected columns

Find Recipe after successful creation

 

steps to update attributes for column in dataset recipe
Steps to update attributes for column in dataset recipe

 

Updating Attributes for Column in Dataset
Updating Attributes for Column in Dataset

 

Saving changes to the Dataset
Saving changes to the Dataset
Select our Derived fields from Close Date
Select our Derived fields from Close Date

 

Running the Recipe
Running the Recipe – One time or Scheduled

 

Find Recipe after succesful creation
Find Recipe after successful creation

 

Create a Dashboard

The Dashboard is where users explore and analyse widgets, So go ahead and create a blank Dashboard and rename it to a more specific and suitable description.

Create a Step

Create a step to add a toggle option. So first click on Create a step and choose the dataset you have created at the start. In the left side panel, under Bars choose Quarterly according to your naming’s saved above. Add a condition under Filters, choosing Quarterly equals to Current Quarter and Last year’s same Quarter. After following the steps as mentioned click on done. On the Dashboard, you can see the created step.

Creating step for a Toggle Einstein Analytics
Creating step for a Toggle Einstein Analytics

"<yoastmark

Add Bars to the Custom Step
Add Bars to the Custom Step

"<yoastmark

Ensure correct Bars are selected
Ensure correct Bars are selected

Select Filters from to Eliminate unnecessary Dimensions
Select Filters from to Eliminate Unnecessary Dimensions

Select the required Dimensions
Select the required Dimensions

Filters for Bar Steps
Click on Done after verifying correct selection of correct Dimensions in Step

Step is visible in the Right pane of Toggle Designer
Step is visible in the Right pane of Toggle Designer

Create Toggle

On the left side panel, select the Toggle option. Drag and drop the created step on the Toggle box. The options that are added to the filter are displayed on the Toggle.

Select Toggle from Left Pane
Select Toggle from Left Pane – Einstein Analytics

Drag toggle to the Designer pane
Drag Toggle to the designer pane and Select the Step Created earlier

Ensure both filter custom dimensions are presentCreate the same toggle for a current year and last year. Follow the same steps given above to create a step for Yearly.

Step - Current Year

"<yoastmark

Create a Step

Now we create a step to show a chart with Account Industry data. Click on create a step and select under bars AccountId.Industry. Under Bar Length add criteria to count already available choosing the option: the sum of the amount. Rename the step and drag it to the Dashboard as shown below. Save your Dashboard and click on the preview to see how the toggle helps out.

default step accountid.industry
Creating a new Step AccountId.Industry selecting our Dataset Recipe

Select Bars - AccountId.Industry
Select Bars – AccountId.Industry

Select Bar Length - Sum of Amount
Select Bar Length – Sum of Amount

Rename Step, ensure the fields are reflecting correct data
Rename Step, ensure the fields are reflecting correct data then hit “Done”

Drag Industry Step onto the Dashboard
Drag Industry Step onto the Dashboard

Save the Toggle Dashboard and Hit Preview
Save the Toggle Dashboard and Hit Preview

On Preview, you can see the output by selecting on the toggle options as shown below.

Preview - Einstein Analytics : Tableau - Current Quarter
Preview – Einstein Analytics – Tableau – Current Quarter

"<yoastmark

"<yoastmark

"<yoastmark

"<yoastmark

So That’s it! If you are still facing problems, you can touch base with us over email team@absyz.com, we ABSYZ are one of the largest Salesforce integrators and a salesforce partner company providing End to End Salesforce implementation services for not just Tableau CRM but also for Salesforce Marketing Cloud and Salesforce Health Cloud. Do reach out to us for any help with integrations.

]]>
https://absyz.com/einstein-analytics-creating-date-fields-in-recipes-for-toggles/feed/ 0