On this page
Kater SQL Functions Date conditions
KATER__IS_WITHIN_LAST
True where the date is in the last few periods, counted back from today or from end.
Family: Date conditions
Syntax
KATER__IS_WITHIN_LAST(field, amount, unit [, complete => TRUE] [, end => date])Arguments
Required
| Argument | Description |
|---|---|
field | The date or timestamp to test. |
amount | How many periods to count back. A whole number, or a parameter a reader supplies. |
unit | The length of one period. One of 'minute', 'hour', 'day', 'week', 'month', 'quarter', 'year'. |
Optional
| Argument | Description |
|---|---|
complete | TRUE counts only whole periods, and leaves out the period that holds today, or end. TRUE or FALSE. It is FALSE when left out. |
end | The date to count back from, in place of today, such as the day after the last day with data. An expression. |
Returns
TRUE or FALSE. It is NULL where the field is empty, so a filter leaves that row out.
Usage notes
- Without complete or end, it keeps the dates from today minus amount units. It has no upper end, so a date after today is kept too.
- With
complete => TRUE, it keeps amount whole periods before the current one. In May, 3 months keeps February, March and April. - With end, it keeps the dates after end minus amount units, up to and including end.
- With both, it keeps amount whole periods before the period that holds end. Set end to the day after the last day with data, and a month with all its data counts as complete.
- Today is the warehouse’s
CURRENT_DATE, orCURRENT_TIMESTAMPfor minute and hour, in the time zone of the warehouse session. - A parameter can give amount. You write unit, complete and end in the call.
Example
This Sandbox query reads the sample shop’s orders. The table shows the rows that Kater gave.
SELECT orders.order_month AS "Order Month", EVAL(orders.count_distinct__order_id) AS "Orders"FROM kater_dataset.orders AS ordersWHERE KATER__IS_WITHIN_LAST(orders.order_date, 3, 'month', complete => TRUE, end => DATE '2026-10-01')GROUP BY orders.order_monthORDER BY 1| Order Month | Orders |
|---|---|
| 2026-07-01 | 137 |
| 2026-08-01 | 145 |
| 2026-09-01 | 137 |
Compiles to
orders.order_date >= DATE_TRUNC('MONTH', CAST('2026-10-01' AS DATE)) + INTERVAL (-3) MONTHAND orders.order_date < DATE_TRUNC('MONTH', CAST('2026-10-01' AS DATE))orders.order_date >= DATEADD(MONTH, -3, DATE_TRUNC('MONTH', CAST('2026-10-01' AS DATE)))AND orders.order_date < DATE_TRUNC('MONTH', CAST('2026-10-01' AS DATE))