Date formulas are useful when you want to compare data collected between two date periods. Date formulas allow you to apply date related functions to your formulas. The date functions include:
Function | Description | Examples |
---|---|---|
add_days |
Returns the result of adding the specified number of days to the given date. |
|
date |
Returns the date portion of a given date. |
|
day |
Returns the number (1-31) of the day for the given date. |
|
day_number_of_week |
Returns the number (1-7) of the day in a week for the given date with 1 being Monday and 7 being Sunday. |
|
day_number_of_year |
Returns the number (1-366) of the day in a year for the given date. |
|
day_of_week |
Returns the day of the week for the given date. |
|
diff_days |
Subtracts the second date from the first date and returns the result in number of days, rounded down if not exact. |
|
diff_time |
Subtracts the second date from the first date and returns the result in number of seconds. |
|
hour_of_day |
Returns the hour of the day for the given date. |
|
is_weekend |
Returns true if the given date falls on a Saturday or Sunday. |
|
month |
Returns the month from the given date. |
|
month_number |
Returns the number (1-12) of the month for the given date. |
|
now |
Returns the current timestamp. |
|
start_of_month |
Returns the date for the first day of the month for the given date. |
|
start_of_quarter |
Returns the date for the first day of the quarter for the given date. |
|
start_of_week |
Returns the date for the first day of the week for the given date. |
|
start_of_year |
Returns the date for the first day of the year for the given date. |
|
time |
Returns the time portion of a given date. |
|
year |
Returns the year from the given date. |
|
Calculate date formulas
Calculating date formulas is useful when you want to compare data from different date periods. Here are some examples of using date formulas:
Example 1
The following example shows you how to create formulas that you can use to compare data from this week to last week.
- The formula for this week is:
week ( today () ) - week (date)
- The formula for last week is:
diff_days ( week ( today ) ) , week ( date ) )
Example 2
The following example shows you how to calculate the percent increase from the last date period to this period in terms of revenue.
- Create the formula:
this week revenue = sum ( if ( this week ) then revenue else 0 )
- Then create the formula:
last week revenue = sum ( if (last week ) then revenue else 0 )
- Use nested formulas to calculate the percent increase by creating a parent formula:
percent increase = ( ( last week revenue - this week revenue) / last week revenue ) \* 100