BigQuery · Date function
BigQuery GENERATE_DATE_ARRAY
GENERATE_DATE_ARRAY(start, end[, INTERVAL n part]) returns an ARRAY of dates from start to end, both included. UNNEST it to build a calendar (date spine) and LEFT JOIN your data so days without rows still appear.
GENERATE_DATE_ARRAY creates every date between two bounds, inclusive, one day apart by default. The step can be any number of DAY, WEEK, MONTH, QUARTER or YEAR units, and a negative step counts down. If start is after end with a positive step, the result is an empty array; a NULL bound gives a NULL array, and a zero step is an error.
The main use is the date spine: SELECT day FROM UNNEST(GENERATE_DATE_ARRAY(...)) AS day turns the array into one row per date. LEFT JOIN facts to that calendar and days with no orders, events or pipeline runs appear with zero instead of disappearing from the chart.
Each element is computed from the previous one, so monthly steps from the 31st drift: from January 31 you get February 28, then March 28. Start from the first of the month and apply LAST_DAY when you need month ends. An array can hold at most 1,048,576 elements, which is roughly 2,870 years of days.
Syntax
GENERATE_DATE_ARRAY(start_date, end_date[, INTERVAL step_size step_unit])Arguments and return value
| Argument | Description |
|---|---|
start_date | The first DATE in the array (included). |
end_date | The last possible DATE (included when a step lands on it). |
step_size | Optional INT64, default 1. Negative values count down; 0 is an error. |
step_unit | DAY (default), WEEK, MONTH, QUARTER or YEAR. |
Returns: ARRAY<DATE>
Examples
Every day of a week
SELECT GENERATE_DATE_ARRAY('2026-11-23', '2026-11-29') AS days;| days |
|---|
| [2026-11-23, 2026-11-24, 2026-11-25, 2026-11-26, 2026-11-27, 2026-11-28, 2026-11-29] |
Date spine: show days with zero orders
COUNT(o.order_id) counts only matched rows, so empty days show 0. COUNT(*) would show 1.
WITH orders AS (
SELECT 1 AS order_id, DATE '2026-11-23' AS order_date UNION ALL
SELECT 2, DATE '2026-11-23' UNION ALL
SELECT 3, DATE '2026-11-25' UNION ALL
SELECT 4, DATE '2026-11-27'
),
calendar AS (
SELECT day
FROM UNNEST(GENERATE_DATE_ARRAY('2026-11-23', '2026-11-27')) AS day
)
SELECT
c.day,
COUNT(o.order_id) AS orders
FROM calendar AS c
LEFT JOIN orders AS o ON o.order_date = c.day
GROUP BY c.day
ORDER BY c.day;| day | orders |
|---|---|
| 2026-11-23 | 2 |
| 2026-11-24 | 0 |
| 2026-11-25 | 1 |
| 2026-11-26 | 0 |
| 2026-11-27 | 1 |
Monthly steps: start on the 1st, not the 31st
After February 28 the steps continue from the 28th. Use LAST_DAY(month_start) on the first array to get real month ends.
SELECT
GENERATE_DATE_ARRAY('2026-01-01', '2026-05-01', INTERVAL 1 MONTH) AS month_starts,
GENERATE_DATE_ARRAY('2026-01-31', '2026-05-31', INTERVAL 1 MONTH) AS drifting;| month_starts | drifting |
|---|---|
| [2026-01-01, 2026-02-01, 2026-03-01, 2026-04-01, 2026-05-01] | [2026-01-31, 2026-02-28, 2026-03-28, 2026-04-28, 2026-05-28] |
Empty when the range runs backwards
SELECT
GENERATE_DATE_ARRAY('2026-11-30', '2026-11-23') AS empty,
GENERATE_DATE_ARRAY('2026-11-30', '2026-11-23', INTERVAL -3 DAY) AS countdown;| empty | countdown |
|---|---|
| [] | [2026-11-30, 2026-11-27, 2026-11-24] |
Common mistakes
- Counting with
COUNT(*)after the LEFT JOIN. Each calendar day has at least one row, so empty days count as 1; count a column from the joined table instead. - Building monthly arrays from a month end. Steps drift to the 28th after February; start from the 1st and use LAST_DAY.
- Porting
generate_series(start, end, '1 day')from PostgreSQL or Snowflake'sGENERATOR. In BigQuery the function returns an array, so you needUNNESTin FROM to get rows. - Forgetting that the array is per row. Calling GENERATE_DATE_ARRAY(start_date, end_date) on a table with a million subscriptions creates a million arrays; that is how you expand ranges into days, but check the row count before you run it on a large table.
Cost and performance
- A generated calendar scans no table data and is free on on-demand pricing. The cost comes from the fact table you join: filter it to the same date range so partitions are pruned.
- Exploding long ranges per row (for example every subscription into every active day) multiplies rows and slot time. Clamp the ranges to the reporting window before calling GENERATE_DATE_ARRAY.
FAQ
How do I create a calendar table in BigQuery?
SELECT day FROM UNNEST(GENERATE_DATE_ARRAY('2026-01-01', '2026-12-31')) AS day returns one row per date. Add columns with EXTRACT and FORMAT_DATE as needed.
How do I fill missing dates with zeros?
Generate the calendar with GENERATE_DATE_ARRAY and UNNEST, LEFT JOIN your aggregated data to it, and wrap the measure in COALESCE(x, 0) or use COUNT(joined_column).
What is the maximum size of GENERATE_DATE_ARRAY?
1,048,576 elements. Larger ranges fail; split them or use a coarser step.
Is there a GENERATE_SERIES in BigQuery?
Not under that name. Use GENERATE_DATE_ARRAY for dates, GENERATE_ARRAY for numbers and GENERATE_TIMESTAMP_ARRAY for timestamps, each with UNNEST.
Related
Errors you may hit
More date functions
All BigQuery date functions · All functions · BigQuery error messages
