How To Handle Common Date Challenges in Einstein Analytics
How To Handle Common Date Challenges in Einstein Analytics
One of the most common use case on dates is to have custom date toggles. From EA UI user
will be able to have a calendar filter based on either Absolute date range or Relative from
and to periods. However, if a custom period with custom label in a toggle or list widget is
desired then it needs to be customized. Ex: In a dashboard, user would like to have a handy
filter which will filter data based on following:
● Last 30 days
● Last 90 days
● Last 180 days
● Prior Year
or
In order to achieve this, you have to first open the dashboard JSON code and add the fol-
lowing step under “steps” section:
"toggle_dates": {
"datasets": [],
"dimensions": [],
"groups": [],
"isFacet": false,
"isGlobal": false,
"numbers": [],
"selectMode": "singlerequired",
"start": {
"display": [
"Last 30days"
]
},
"strings": [],
"type": "staticflex",
"useGlobal": false,
"values": [
{
"display": "Last 30days",
"current": [
[
"day",
-30
],
[
"day",
0
]
]
},
{
"display": "Last 90days",
"current": [
[
"day",
-90
],
[
"day",
0
]
]
},
{
"display": "Last 180 days",
"current": [
[
"day",
-180
],
[
"day",
0
]
]
},
{
"display": "Prior Year",
"current": [
[
"day",
-365
],
[
"day",
0
]
]
}
],
"label": "Toggle Dates"
}
Add a toggle or list widget to the dashboard and drag and drop “toggle_dates” step onto the
widget. In order to use the Toggle output in other lens with in the dashboard you need to
bind the steps.
Compact form selection binding: If you want to filter a lens based on field “CloseDate” add
the following compact form under filter section of the lens:
[
"CloseDate",
"{{column(toggle_dates.selection, [\"current\"]).asObject()}}",
"in"
]
Saql selection binding: If your lens is based on SAQL then the selection binding will be:
If you want to compare data between a time period and same time prior year you need to
customize the dashboard. In this use case we will see following comparisons
● Compare data between month to date of current year with same month to date of prior
year
● Compare data between quarter to date of current year with same quarter to date of
prior year
● Compare data between year to date of current year with year to same date of prior year
or
Edit the dashboard JSON and add the following step:
"compare_period": {
"datasets": [],
"dimensions": [],
"groups": [],
"isFacet": false,
"isGlobal": false,
"label": "Compare Period",
"numbers": [],
"selectMode": "singlerequired",
"start": {
"display": [
"MTD"
]
},
"strings": [],
"type": "staticflex",
"useGlobal": false,
"values": [
{
"display": "MTD",
"current": [
"current month",
"current day"
],
"prev": [
"12 months ago",
"current day - 1 year"
]
},
{
"display": "QTD",
"current": [
"current quarter",
"current day"
],
"prev": [
"4 quarters ago",
"current day - 1 year"
]
},
{
"display": "YTD",
"current": [
"current year",
"current day"
],
"prev": [
"1 year ago",
"current day - 1 year"
]
}
]
},
Basically range is defined for current period and perior period same date. For example when
display value “YTD” is selected then current period query will be based on filter condition
[“current year”, “current day”] and prior query will be based on [“1 year ago”, “current day -
1 year”]. For example if todays date is 30-Aug-2017, current period will be {[2017, 01, 01],
[2017, 08, 01]} and prior period will be {[2016, 01, 01], [ 2016, 08, 30]}.
Add a toggle or list widget to the dashboard and drag and drop “compare_period” step onto
the widget. In order to use the Toggle output in other lens with in the dashboard you need
to bind the steps.
saql selection binding: If you want to see both time periods in the same lens, then you have
to use saql to customize the lens. You will have to load the data set once and filter out twice
with two different periods and then union the results. A sample lens showing Account Name
and period with saql code is as follows:
This use case is focused on Dates filter based on a relative to a specific date . For example 10
days before 15-Sep-2017.
Use case: For a selected campaign, user wants to compare a specific metric "N days before
the campaign date" with "N days after the campaign date". Here N could be 10, 30, 60, 90,
120. User wants to filter the data relative to the selected campaign's date.
● Step 1: Derive the millisecond equivalent for the dates involved in the given date range.
For example, if you want to filter records based on "30 days ago" from the Campaign-
StartDate, create a saql type step that returns you the millisecond equivalent for the
(CampaignStartDate) & (CampaignStartDate - 30 Days).
If you derive this in a saql type step called GetLowerUpperDateRange, the saql statement
should look something like this:
● Step 2: - Use the values of variables calculated in the above statement as a upper and
lower limit in the "filters" section. The binding will look something like this:
"filters": [
[
"CampaignStartDate",
[
[
"{{cell(GetLowerUpperDateRange.result,0,\"Lower_N_Days_Ago\").asObject()}}",
"{{cell(GetLowerUpperDateRange.result,0,\"CampaignStartDate\").asObject()}}"
]
],
">=<="
]
]
Suppose there is a dashboard built on two datasets and you want the date filter of one
dataset to be applied on the lens built out of second dataset. Faceting doesn't work in this
case as the data is from different datasets. This can be achieved by either connecting the
datasets or by selection binding of the steps.
In this example, Quota dataset is created from an external source (csv).Grain of Quota is at
Sales Rep's Monthly Target Quota. User wants to find out Target Quota based on Opportu-
nity Close Date Month/Qtr/Year.
To the Sales Rep Dashboard or any dashboard where you want to add second dataset lens,
add a number widget. By default EA will create a lens pointing to current dataset. Click on
Back to select the QuotaExternal dataset. Change the measure to Sum of Quota Amount.
Then click done. Once you have the lens from the second data, you need to connect the
datasets or do selection binding with the Close Date filter that you already have on the dash-
board. Close_Date_1 is the “Close Date” Date filter on Opportunity.
● Connecting datasets:
Then click on “Create New Connection” and then give it a name. Under Data source1, select
first dataset and the date field you wanted to connect. Then select second datasource and
the date field with which you want to connect the first dataset.
Then click Save and Close. Now both datasets are connected with the date field.
"filters": [
[
"QuotaDate",
[
"{{row(Close_Date_1.selection, [0], [\"min\",
\"max\"]).asObject()}}"
],
">=<="
]
]
Handling null dates is not possible as of now in EA UI. You need to do workaround to handle
null dates. If null dates are converted through ELT process then it can be handle via standard
filters in UI. Following are some of the workarounds
After converting the date into a default date handle 9999-01-01 as null in dashboard. Exam-
ple of a compute expression to convert null into a default date
Null values can be handled by checking the epoch value of the date also. Following is a sam-
ple code:
Date Queries
Query Explanation: After loading in the opportunity dataset, we split it into two streams.
One stream, curr, g ets data for the current quarter using the date range ["current
quarter".."current quarter"] w hich filters for data ranging from the start of the current
quarter to the end of the current quarter. The other stream, prev, gets data for the previous
quarter. We then bring the two streams back together using a co-group. Since we are trying
to calculate the overall QoQ number, we can group by alla nd then perform the QoQ calcu-
lation.
Calculate the percent change in total value of the opportunities expected to close for the
current quarter to date compared to last quarter to date.
q = load "Opportunity";
curr = filter q by date('CloseDate_year', 'CloseDate_month', 'CloseDate_day') in
["current quarter".."current day"] ;
prev = filter q by date('CloseDate_year', 'CloseDate_month', 'CloseDate_day') in
["1 quarter ago".."current day - 1 quarter"] ;
QoQ = group curr by all, prev by all;
QoQ = foreach QoQ generate (sum(curr['Amount'])/sum(prev['Amount'])) - 1 as
'sum_QoQ';
● Query Explanation: This query utilizes techniques from the QTD and QoQ calculations.
Similar to the QoQ query, we split the dataset into curr and prev streams; however,
rather than filtering for the entire quarter, we filter for the quarter to date as we did in
the QTD query. The way to get the same number of days from the previous quarter as
the number of days elapsed in the current quarter is by using date math. "current day
1 quarter" returns the current day relative to the previous quarter.
● Current QTD over last year same QTD
Calculate the percent change in total value of the opportunities expected to close for the
current quarter to date compared to the same period last year.
q = load "Opportunity";
curr = filter q by date('CloseDate_year', 'CloseDate_month', 'CloseDate_day') in
["current quarter".."current day"] ;
prev = filter q by date('CloseDate_year', 'CloseDate_month', 'CloseDate_day') in
["current quarter - 1 year".."current day - 1 year"] ;
QoQ = group curr by all, prev by all;
QoQ = foreach QoQ generate (sum(curr['Amount'])/sum(prev['Amount'])) 1 as
'sum_QoQ';
Query Explanation: This query is similar to the QTD over QTD calculation. The only differ-
ence is that we refer to the previous year using relative date math ["current quarter - 1
year".."current day - 1 year"].
In this use case we will look at following calculations (if you have null dates then follow Han-
dling Null Date values section above) :
If you don’t see the results as expected (there is a defect right now), edit the JSON and look
for:
numbers:[]
numbers:["QD"]
Typecast numbers with the projection alias you used in the saql.
If you don’t see the results as expected (there is a defect right now), edit the JSON and look
for :
numbers:[]
numbers:["LastDay"]
Sample saql code to find out the same date prior year:
If you don’t see the results as expected (there is a defect right now), edit the JSON and look
for :
numbers:[]
numbers:["LastYearSameDay"]
● Get an interval (Years, Months, Days, Seconds etc.) between two dates
date_diff(interval ,start date, end date) : Interval to calculate can be "day", "month", "year",
"quarter", "week", "hour", "minute", "second". If 'start date' is after 'end date' the result is a
negative integer of the difference.
1 now() function always returns current time in UTC. You may need to adjust this to your
required time zone. Check the “Adjust current date & time to reflect Pacific Time” sec-
tion above.
2 To pass date field as an argument to any function, always use toDate() function. Simply
using the date field name as an argument will not work.
3 Check out the supported date-formats here
4 date_diff(interval ,start date, end date) : Interval to calculate can be "day", "month",
"year", "quarter", "week", "hour", "minute", "second". If 'start date' is after 'end date' the
result is a negative integer of the difference.