0% found this document useful (0 votes)
46 views17 pages

How To Handle Common Date Challenges in Einstein Analytics

Uploaded by

ProtyayBhowmick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views17 pages

How To Handle Common Date Challenges in Einstein Analytics

Uploaded by

ProtyayBhowmick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

How to handle common Date chal-

lenges in Einstein Analytics


Building Date Toggles

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

UI toggle will have to look like this:

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:

q = filter q by {{cell(toggle_dates.selection, 0, \"current\").asDateRange(\"date('CloseDate_Y


'CloseDate_Month', 'CloseDate_Day')\")}};

Same time Prior Period/Year

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

UI toggle will have to look like this:

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:

"pigql": "q = load \"opportunity1\";\n curr = filter q by 'IsWon' ==


\"true\";\n curr = filter curr by {{cell(compare_period.selection, 0,
\"current\").asDateRange(\"date('CloseDate_Year', 'CloseDate_Month',
'CloseDate_Day')\")}};\n curr = group curr by 'Account.Name';curr = foreach
curr generate 'Account.Name' as 'Account.Name', \"Current Period\" as
'Period', sum('Amount') as 'Amount'; \n prior = filter q by 'IsWon' ==
\"true\"; \n prior = filter prior by {{cell(compare_period.selection,
0, \"prev\").asDateRange(\"date('CloseDate_Year', 'CloseDate_Month',
'CloseDate_Day')\")}}; \n prior = group prior by 'Account.Name'; \n prior =
foreach prior generate 'Account.Name' as 'Account.Name', \"Prior Period\" as
'Period', sum('Amount') as 'Amount'; \n result = union curr, prior; result =
foreach result generate 'Account.Name' as 'Account.Name', 'Period' as 'Period',
sum('Amount') as 'sum_Amount'; \n result = order result by 'Account.Name' asc;",
"groups": [
"Account.Name",
"Period"
],

Filtering on Dates relative to a period

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:

q = foreach q generate 'CampaignStartDate_sec_epoch' * 1000 as


'CampaignStartDate', ('CampaignStartDate_sec_epoch' - (30 * 86400)) * 1000 as
'Lower_N_Days_Ago';

● 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()}}"
]
],
">=<="
]
]

Date filter on Multi dataset dashboard

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:

From the dashboard UI, click on “Connect Data Sources”

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.

● Compact form selection binding:

"filters": [
[
"QuotaDate",
[
"{{row(Close_Date_1.selection, [0], [\"min\",
\"max\"]).asObject()}}"
],
">=<="
]
]

● Saql selection binding:

"pigql": "q = load \"QuotaExternal\";q = filter q by {{row(Close_Date_1.selection,[0],[\"min\


\"max\"] ).asDateRange(\"date('QuotaDate_Year', 'QuotaDate_Month',
'QuotaDate_Day')\")}};q = group q by all;q = foreach q generate
sum('QuotaAmount') as 'sum_QuotaAmount';",

Handing Null Date values

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

● Convert null dates in source into a default date such as 9999-01-01


● During ELT, default the value in SFDC digest step to 9999-01-01
● Have a compute expression in the dataflow to convert null to 9999-01-01
● Handle with case statement in saql

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

case when 'datefield' is null then toDate("9999-01-01", "yyyy-MM-dd") else


toDate('datefield', "yyyy-MM-dd") end

Sample filter for the lens will look like :


sample saql code to filter data for the default date value:

q = filter q by date('datefield_Year', 'datefield_Month', 'datefield_Day') in


[dateRange([9999, 1, 1], [9999, 1, 1])];

saql code to handle null dates:

case when 'datefield' is null then toDate("9999-01-01", "yyyy-MM-dd") else


toDate('datefield', "yyyy-MM-dd") end

Null values can be handled by checking the epoch value of the date also. Following is a sam-
ple code:

(case datefield_sec_epoch when 0 then toDate("9999-01-01", "yyyy-MM-dd") else


toDate(datefield_sec_epoch) end) as 'datefield';

Date Queries

● QoQ (Quarter over Quarter) without bindings:

Calculate the percent change in total value of the opportunities expected to


close for the current quarter compared to last quarter.
q = load "Opportunity";
curr = filter q by date('CloseDate_year', 'CloseDate_month', 'CloseDate_day') in
["current quarter".."current quarter"] ;
prev = filter q by date('CloseDate_year', 'CloseDate_month', 'CloseDate_day') in
["1 quarter ago".."1 quarter ago"] ;
QoQ = group curr by all, prev by all;
QoQ = foreach QoQ generate (sum(curr['Amount'])/sum(prev['Amount'])) 1 as
'sum_QoQ';

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.

● QTD over QTD

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"].

Date calculations in SAQL

In this use case we will look at following calculations (if you have null dates then follow Han-
dling Null Date values section above) :

● Days in current quarter


● Previous Quarter end date
● Same date prior year

All date functions can be seen here.

● Days in current quarter:

sample code to find how many days in the current quarter:

"q = load \"DatesDataset \”; q = foreach q generate quarter_days(toDate('datefield_sec_epoch')


as 'QD';"

If you don’t see the results as expected (there is a defect right now), edit the JSON and look
for:

numbers:[]

change this to:

numbers:["QD"]

Typecast numbers with the projection alias you used in the saql.

● Previous Quarter End date


Sample code to find out the previous quarter end date:

"q = load \"DatesDataset\";\nq = foreach q generate quarter_last_day(toDate('datefield_sec_epo


- quarter_days(toDate('datefield_sec_epoch'))*60*60*24)) as 'LastDay';

If you don’t see the results as expected (there is a defect right now), edit the JSON and look
for :

numbers:[]

change this to:

numbers:["LastDay"]

● Same date prior year

Sample saql code to find out the same date prior year:

"q = load \"DatesDataset\"; \n q = foreach q generate toDate('datefield_sec_epoch'


- (case toString(toDate(datefield_sec_epoch - 365*24*3600), \"dd\") when
toString(toDate(datefield_sec_epoch), \"dd\") then 365 else 366 end) *24*3600)
as 'LastYearSameDay';"

If you don’t see the results as expected (there is a defect right now), edit the JSON and look
for :

numbers:[]

change this to:

numbers:["LastYearSameDay"]

Some useful Date Functions

● Get Current date and time:

q = load "<Dataset Name>";


q = group q by 'all';
q = foreach q generate now() as 'CurrentDateTime';

● Get last day of current month


q = load "<Dataset Name>";
q = group q by 'all';
q = foreach q generate month_last_day(now()) as 'MonthLastDay';

● Get last day of current quarter:

q = load "<Dataset Name>";


q = group q by 'all';
q = foreach q generate quarter_last_day(now()) as 'QuarterLastDay';

● Get last day of current week:

q = load "<Dataset Name>";


q = group q by 'all';
q = foreach q generate week_last_day(now()) as 'WeekLastDay';

● Adjust current date & time to reflect Pacific Time

q = load "<Dataset Name>";


q = group q by 'all';
q = foreach q generate toDate(date_diff("second",toDate("1970-01-01 00:00:00"),
now())-(7*3600)) as 'CurrentPDT';

● 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.

q = load "<Dataset Name>";


q = group q by 'all';
q = foreach q generate date_diff("day", now(), month_last_day(now())) as
'DaysDifference';

● Get sec_epoch value for current date time


q = load "<Dataset Name>";
q = group q by 'all';
q = foreach q generate date_diff("second",toDate("1970-01-01 00:00:00"), now())
as 'CurentTime_sec_epoch';

● Get day of the month for current date

q = load "<Dataset Name>";


q = group q by 'all';
q = foreach q generate day_in_month(now()) as 'DayOfMonth';

● Get day of the week for current date

q = load "<Dataset Name>";


q = group q by 'all';
q = foreach q generate day_in_week(now()) as 'DayOfWeek';

● Get day of the year for current date

q = load "<Dataset Name>";


q = group q by 'all';
q = foreach q generate day_in_year(now()) as 'DayOfYear';

● Get day of the quarter for current date

q = load "<Dataset Name>";


q = group q by 'all';
q = foreach q generate day_in_quarter(now()) as 'DayOfQuarter';

● Get days in the month for current date

q = load "<Dataset Name>";


q = group q by 'all';
q = foreach q generate month_days(now()) as 'DaysInCurrentMonth';

● Get days in the quarter for current date

q = load "<Dataset Name>";


q = group q by 'all';
q = foreach q generate quarter_days(now()) as 'DaysInCurrentQuarter';

● Get days in the year for current date

q = load "<Dataset Name>";


q = group q by 'all';
q = foreach q generate year_days(now()) as 'DaysInCurrentYear';

● Useful Tips on Dates functions:

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.

You might also like