There are the following three ways to use SAQL in Einstein Analytics:
The simplest way of using SAQL is while creating a step or lens. A user can switch between the modes here. To use SAQL for lens, perform the following steps:
In SAQL, the query is made up of multiple statements. In the first statement, the query loads the input data from the dataset, operates on it, and then finally gives the result. The user can use the Run Query button to see the results and errors after changing or adding statements. The user can see the errors at the bottom of the Query editor.
SAQL is made up of statements that take the input dataset, and we build our logic on that. We can add filters, groups, orders, and so on, to this dataset to get the desired output. There are certain order rules that need to be followed while creating these statements and those rules are as follows:
In SAQL, we can perform all the mathematical calculations and comparisons. SAQL also supports arithmetic operators, comparison operators, string operators, and logical operators.
The foreach statement applies the set of expressions to every row, which is called projection. The foreach statement is mandatory to get the output of the query. The following is the syntax for the foreach statement:
q = foreach q generate expression as 'expresion name';
Let's look at one example of using the foreach statement:
q = load "opportunity";
q = group q by all;
q = foreach q generate count() as 'count';
q = limit q 2000;
You can see the result of this query just below the Query editor:
4. Now replace the third statement with the following statement:
q = foreach q generate sum('Amount') as 'Sum Amount';
5. Click on the Run Query button and observe the result as shown in the following screenshot:
The user can group records of the same value in one group by using the group statements. Use the following syntax:
q = group rows by fieldName
Let's see how to use grouping in SAQL by performing the following steps:
q = group q by 'StageName';
q = foreach q generate 'StageName' as 'StageName',
sum('Amount') as 'Sum Amount';
2. Click on the Run Query button and you should see the following result:
Filters in SAQL behave just like a where clause in SOQL and SQL, filtering the data as per the condition or clause. In Einstein Analytics, it selects the row from the dataset that satisfies the condition added.
The syntax for the filter is as follows:
q = filter q by fieldName 'Operator' value
Click on Run Query and view the result as shown in the following screenshot:
The beauty of a function is in its reusability. Once the function is created it can be used multiple times. In SAQL, we can use different types of functions, such as string functions, math functions, aggregate functions, windowing functions, and so on. These functions are predefined and saved quite a few times. Let's use a math function power.
The syntax for the power is power(m, n). The function returns the value of m raised to the nth power. Replace the following statement with the fourth statement:
q = foreach q generate 'StageName' as 'StageName',
power(sum('Amount'), 1/2) as 'Amount Squareroot',
sum('Amount') as 'Sum Amount';
Click on the Run Query button.
We saw how to apply different kinds of case-specific functions in Salesforce Einstein to play with data in order to get the desired outcome.
[box type="note" align="" class="" width=""]The above excerpt is taken from the book Learning Einstein Analytics, written by Santosh Chitalkar. It covers techniques to set-up and create apps, lenses, and dashboards using Salesforce Einstein Analytics for effective business insights. If you want to know more about these techniques, check out the book Learning Einstein Analytics.[/box]