Skip to content

bigfunctions > prophet

prophet

Call or Deploy prophet ?

✅ You can call this prophet bigfunction directly from your Google Cloud Project (no install required).

  • This prophet function is deployed in bigfunctions GCP project in 39 datasets for all of the 39 BigQuery regions. You need to use the dataset in the same region as your datasets (otherwise you may have a function not found error).
  • Function is public, so it can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
  • You may prefer to deploy the BigFunction in your own project if you want to build and manage your own catalog of functions. This is particularly useful if you want to create private functions (for example calling your internal APIs). Discover the framework

Public BigFunctions Datasets:

Region Dataset
eu bigfunctions.eu
us bigfunctions.us
europe-west1 bigfunctions.europe_west1
asia-east1 bigfunctions.asia_east1
... ...

Description

Signature

prophet(records, periods, kwargs)

Description

Return Time Series Forecast as json using prophet python library.

Parameters of this function are passed as is to prophet python function and python function result is returned as is.

(Inspired from this Felipe Hoffa medium's post)

Examples

1. Using a table with columns named ds and y for date and value respectively

with sample_data as (

  select date('2022-01-01') as ds, 1 as y,
  union all
  select date('2022-01-02') as ds, 2 as y,

)


select bigfunctions.eu.prophet(to_json(array_agg(sample_data)), 3, null)
from sample_data
with sample_data as (

  select date('2022-01-01') as ds, 1 as y,
  union all
  select date('2022-01-02') as ds, 2 as y,

)


select bigfunctions.us.prophet(to_json(array_agg(sample_data)), 3, null)
from sample_data
with sample_data as (

  select date('2022-01-01') as ds, 1 as y,
  union all
  select date('2022-01-02') as ds, 2 as y,

)


select bigfunctions.europe_west1.prophet(to_json(array_agg(sample_data)), 3, null)
from sample_data
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| forecasted_records                                                                                                                                                             |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| [
  {"ds": "2022-01-03", "yhat": 3, "yhat_upper": 3, ...},
  {"ds": "2022-01-04", "yhat": 4, "yhat_upper": 4, ...},
  {"ds": "2022-01-05", "yhat": 5, "yhat_upper": 5, ...}
]
 |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

2. Using a table with columns to rename to ds and y

with sample_data as (

  select date('2022-01-01') as date, 1 as value,
  union all
  select date('2022-01-02') as date, 2 as value,

)


select bigfunctions.eu.prophet(to_json(array_agg(struct(date as ds, value as y))), 3, null)
from sample_data
with sample_data as (

  select date('2022-01-01') as date, 1 as value,
  union all
  select date('2022-01-02') as date, 2 as value,

)


select bigfunctions.us.prophet(to_json(array_agg(struct(date as ds, value as y))), 3, null)
from sample_data
with sample_data as (

  select date('2022-01-01') as date, 1 as value,
  union all
  select date('2022-01-02') as date, 2 as value,

)


select bigfunctions.europe_west1.prophet(to_json(array_agg(struct(date as ds, value as y))), 3, null)
from sample_data
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| forecasted_records                                                                                                                                                             |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| [
  {"ds": "2022-01-03", "yhat": 3, "yhat_upper": 3, ...},
  {"ds": "2022-01-04", "yhat": 4, "yhat_upper": 4, ...},
  {"ds": "2022-01-05", "yhat": 5, "yhat_upper": 5, ...}
]
 |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Need help using prophet?

The community can help! Engage the conversation on Slack

For professional suppport, don't hesitate to chat with us.

Found a bug using prophet?

If the function does not work as expected, please

  • report a bug so that it can be improved.
  • or open the discussion with the community on Slack.

For professional suppport, don't hesitate to chat with us.

Use cases

A use case for this prophet BigQuery function would be forecasting future sales based on historical sales data. Imagine you have a table in BigQuery called sales_data with two columns: date (DATE) and sales (INTEGER). You want to predict sales for the next 7 days.

SELECT bigfunctions.<your-region>.prophet(
    (
        SELECT
            JSON_ARRAY(CAST(date AS STRING), sales)
        FROM
            `your-project.your_dataset.sales_data`
        ORDER BY
            date
    ),
    7
) AS forecasted_sales;

Replace <your-region> with the appropriate BigQuery region for your dataset (e.g., us, eu, us-central1). This query will:

  1. Prepare the input data: The subquery selects the date and sales data from your sales_data table, converts the date to a string, and uses JSON_ARRAY to create an array of [date, sales] pairs for each row. This is the format expected by the prophet function. The data is ordered by date, which is crucial for time series forecasting.

  2. Call the prophet function: The prophet function is called with the JSON array of historical data and the number of periods (7 days) to forecast.

  3. Return the forecast: The function returns a JSON array containing the forecasted sales for the next 7 days in the same [date, sales] format. The result is aliased as forecasted_sales.

You can then use the forecasted sales data for inventory planning, resource allocation, and other business decisions.

More advanced example with custom seasonality:

You can also pass additional parameters to the underlying Prophet model using the kwargs argument. For example, to add a weekly seasonality:

SELECT bigfunctions.<your-region>.prophet(
    (
        SELECT
            JSON_ARRAY(CAST(date AS STRING), sales)
        FROM
            `your-project.your_dataset.sales_data`
        ORDER BY
            date
    ),
    7,
    STRUCT(JSON'{"weekly_seasonality": true}' as kwargs)
) AS forecasted_sales_with_weekly_seasonality;

This allows you to customize the model to better fit your specific data and business needs, such as accounting for daily, weekly, or yearly seasonality. Refer to the Prophet documentation for a complete list of available parameters.

This example demonstrates how the prophet BigQuery function can be used for practical time series forecasting directly within BigQuery, simplifying the process and leveraging the power of Prophet without needing external libraries or tools.

Spread the word

BigFunctions is fully open-source. Help make it a success by spreading the word!

Share on Add a on