When working with time series data, you may be commonly switching between hourly/daily/weekly or even other time granularities. Omni provides a built-in way to flip between these options using the Time Frame Switcher control.
However, maybe you want this timeframe to switch automatically based on the date filter selection, such as showing you hourly numbers when filtered to < 2 days of data and flipping to daily when > 2 days. Here’s a working pattern to do this in Omni (see video at the end for a walkthrough!):
- Figure out which date field will drive all of this, and add it as a filter to your query/dashboard
- Open the Workbook IDE (Model → Model layers → Workbook) and navigate to the view file (shortcut: click the three dots next to view name → Modeling → Go to definition)
- Paste the code below as a new dimension and modify the field/view references. Also modify the threshold and/or add criteria as needed
- Back in the workbook, build your query using this new Dynamic Date Part and any other fields you need
That’s it! Now you should observe the dynamic date aggregating differently when above/below the threshold(s) you specified!
Snowflake
dynamic_date_part:
sql: |
case
when timestampdiff(hour, {{filters.omni_dbt_ecomm__order_items.created_at.range_start}}, {{filters.omni_dbt_ecomm__order_items.created_at.range_end}}) <= 24*2
then TO_CHAR(${omni_dbt_ecomm__order_items.created_at[hour]}, 'MM-DD-YYYY HH24:MI')
else TO_CHAR(${omni_dbt_ecomm__order_items.created_at[date]}, 'MM-DD-YYYY')
end
label: Dynamic Time Frame
description: Switches between daily and hourly based on the dates selected
BigQuery
dynamic_date_part:
sql: |
case
when timestamp_diff({{filters.omni_dbt_ecomm__order_items.created_at.range_start}}, {{filters.omni_dbt_ecomm__order_items.created_at.range_end}}, hour) <= 24*2
then cast(${omni_dbt_ecomm__order_items.created_at[hour]} as string)
else FORMAT_TIMESTAMP('%B %d, %Y',${omni_dbt_ecomm__order_items.created_at[date]})
end
label: Dynamic Time Frame
description: Switches between daily and hourly based on the dates selected
Other Dialects
This should work with all SQL dialects, just replace the timestampdiff and TO_CHAR syntax with the syntax required by your dialect.