Skip to main content

Intervals in FQL

FQL, implemented in Feature Express, allows defining intervals over which to calculate aggregations and extract features.

Interval Types

The main types of intervals that can be defined in FQL include Direction Only, Fixed, and Keyword intervals.

1. Direction Only

Direction Only intervals extract features over a relative period before or after the current row. They include:

  • past - from the beginning of time until the current row
  • future - from the current row until the end of time

Example:

avg(price) over past // average price from beginning until current row

2. Fixed Intervals

Fixed intervals are defined with a direction, number of units, and unit:

  • last or next - look back or forward from the current row
  • N - number of units
  • units - milliseconds, seconds, minutes, hours, days, weeks

Examples:

avg(price) over last 5 days // look back 5 days
avg(price) over next 2 weeks // look forward 2 weeks

These can also be expressed with variations in terminology, such as "previous" or "future", and units may optionally be followed by an 's', like "last 5 days".

3. Keyword Intervals

Keyword intervals represent common predefined periods and relative periods:

  • yesterday, lastweek, lastmonth, lastyear, ytd, wtd, mtd
  • tomorrow, nextweek, nextmonth, nextyear

Example:

avg(sales) over lastweek // last week from current row

Below is a table providing examples of how keyword intervals translate into specific dates for a given date (2023-05-17):

Keyword IntervalDescriptionStart DateEnd Date
YTDYear-to-Date2023-01-012023-05-17
MTDMonth-to-Date2023-05-012023-05-17
WTDWeek-to-Date2023-05-152023-05-17
YesterdayPrevious day2023-05-162023-05-16
LastWeekPrevious calendar week2023-05-082023-05-14
LastMonthPrevious calendar month2023-04-012023-04-30
LastQuarterPrevious calendar quarter2023-01-012023-03-31
LastYearPrevious calendar year2022-01-012022-12-31
SameDayLastWeekSame day of the previous week2023-05-102023-05-10
SameDayLastMonthSame day of the previous month2023-04-172023-04-17
SameDayLastYearSame day of the previous year2022-05-172022-05-17
TomorrowNext day2023-05-182023-05-18
NextWeekNext calendar week2023-05-222023-05-28
NextMonthNext calendar month2023-06-012023-06-30
NextQuarterNext calendar quarter2023-07-012023-09-30
NextYearNext calendar year2024-01-012024-12-31
SameDayNextWeekSame day of the next week2023-05-242023-05-24
SameDayNextMonthSame day of the next month2023-06-172023-06-17
SameDayNextYearSame day of the next year2024-05-172024-05-17
NextWorkDayNext business day2023-05-182023-05-18
PreviousWorkDayPrevious business day2023-05-162023-05-16

Using Intervals

Intervals are used in FQL inside aggregation and window functions:

avg(price) over last 5 days // average price over last 5 days
min(price) over next 2 weeks // min price over next 2 weeks
sum(revenue) over lastyear // total revenue over previous year

Where clauses can filter data over the interval:

avg(sales) over lastweek where category = 'electronics' // for electronics

Intervals can be combined with grouping:

avg(price) over lastmonth by category // averages grouped by category

Future Plans

Additional interval types like session and event-based intervals will be added to FQL in the future.

Syntax and Conventions

Defining time intervals in Feature Express is done using string definitions of the intervals based on PEST grammar. Please make sure to follow the specific syntax and conventions when defining time intervals in Feature Express.

Conclusion

Intervals in FQL enable powerful time-based queries and aggregations. By understanding and using these interval types, you can unlock advanced analytics capabilities within Feature Express.