The most common SAQL interactions are data serializations. You can build filter, grouping, and projection interactions. These examples show queries that start with q = filter q by or q = group q by followed by the filter or grouping logic. To make the query dynamic, you replace this static logic with an interaction.
You can execute this full sample SAQL query and these interaction examples in a Trailhead Analytics developer org by using the DTC Opportunity data.
1q = load "DTC_Opportunity_SAMPLE";2q = filter q by date('Close_Date_Year', 'Close_Date_Month', 'Close_Date_Day')in["1 year ago".."1 year ago"];3q = filter q by 'Account_Type' == "Customer";4q = filter q by 'Amount'>= 2910928 && 'Amount'<= 8577295;5q = group q by 'Industry';6q = foreach q generate 'Industry' as 'Industry', sum('Amount')as 'sum_Amount';7q = order q by 'Industry' asc;8q = limit q 2000;
These examples use a static step as the query source for simplicity. Each example illustrates the SAQL without and then with an interaction.
Date Range Filter Interaction
The static step to filter is:
1"values": [2{3 "display": "CY",4 "value": ["current year", "current year"]5},6{7 "display": "LY",8 "value": ["1 year ago", "1 year ago"]9}10]
A date range filter in SAQL is:
1q = filter q by date('Close_Date_Year', 'Close_Date_Month', 'Close_Date_Year')in["1 year ago".."1 year ago"];
To get that specific format, use the asDateRange() interaction with the date function and parameters.
For nonstatic steps, use binding ranges with minimum and maximum values. To bind multiple cell values, use the row data selection function. Define one or more rows that you want to take data from directly after the binding function. In these examples, the value is [0], which indicates only data from the first row of the query results. To define multiple rows, use a comma-separated list, [0,1,2,3]. To include all the rows of the results, use an empty array, [].
1q = group q by 'Industry';2q = foreach q generate 'Industry' as 'Industry', ...;
To create the grouping interaction, use this statement:
1q = group q by{{cell(StaticGroup_1.selection, 0, "value").asGrouping()}};
The data selection function is cell and it references the value field from the static step.
The second part of the interaction, the projection, is a little more complex. The projection takes a field from the dataset, in this example it’s the grouping, and defines how it’s displayed.
This interaction uses the expression and alias values. With two or more values, you must use the row selection function.