Demand Forecasting at Scale with Snowflake Cortex ML Functions

9 minutes to read
Get free consultation

 

The shift toward in-database machine learning is changing how businesses operate. Historically, building a robust predictive engine required complex data movement. Engineers extracted data from the warehouse and moved it into Python environments. They then trained models and pushed the results back for reporting. Modern solutions now prevent the complex MLOps overhead that previously drained resources and delayed insights.

Today, you can generate automated sales projections using SQL directly where your data lives. Snowflake Cortex ML Functions eliminate the need for external infrastructure. You can build a highly accurate Snowflake forecasting model natively. We work with you to unlock data potential by bridging the gap between historical data and real-world supply chain events.

This guide explores how to scale your forecasting efforts. We will cover formatting historical time-series datasets for modeling. We will also demonstrate training and deploying forecasting models using ML.FORECAST. Finally, we will show you how adding prediction intervals and custom holiday lists improves accuracy.

Understanding the Snowflake Forecasting Model

The SNOWFLAKE.ML.FORECAST function provides a powerful native algorithm selection. It acts as a well-oiled data machine for your predictive analytics. The function uses an automated ensemble approach. It tests multiple algorithms under the hood. It then selects the best and fastest method for your specific data structure.

This process simplifies the Snowflake Cortex ML forecasting setup. Automated features remove the need to manually tune hyperparameters or select between ARIMA and Prophet models. Snowflake handles the heavy lifting automatically. This allows your team to focus on the business impact of the forecast.

You can review the official Snowflake forecasting model documentation to understand specific syntax constraints.

Preparing Historical Time-Series Data for Modeling

High-quality data creates the foundation for a highly accurate model. Formatting historical time-series datasets for modeling requires careful attention to detail. We must ensure the data accurately reflects real-world demand.

Handling Missing and Irregular Timestamps

Machine learning models require consistent time intervals. Your data might have missing days if a store was closed or no sales occurred. Continuous data structures prevent model training failures and skewed results caused by missing store days.

You must aggregate your data into regular intervals. You can use daily, weekly, or monthly aggregations. Snowflake makes this easy with date generation functions. You should join your sales data against a continuous date dimension table. This ensures every time period has a record. You can then fill missing sales values with zeros or interpolate them based on your business logic.

Managing Stockouts and Censored Demand

Distinguishing between zero sales and zero demand is a critical business reality. Available inventory allows customers to complete their purchases. The recorded sales might show zero during a stockout, but the true demand often remains high. This concept is known as censored demand.

When we implement these models for enterprise clients, we observe a common trend. Accounting for inventory stockouts ensures your future demand forecasts remain accurate and avoids severely under-forecasted projections. You must adjust for stockouts before training your model.

You can resolve this by joining your sales data with your daily inventory snapshots. If inventory is zero and sales are zero, you should flag that record. You can then replace the zero sales value with a moving average of the previous weeks. This proactive correction guides the model away from learning artificial drops in demand.

Training and Deploying Forecasting Models with ML.FORECAST

Once your data is clean, you can begin training and deploying forecasting models using ML.FORECAST. The process involves creating a model object and then calling it to generate predictions.

Single-Series vs. Multi-Series Forecast Models

A single-series model predicts one outcome based on one timeline. Enterprise businesses achieve better visibility by forecasting multiple outcomes simultaneously. You likely need to forecast thousands of SKUs across hundreds of locations.

Snowflake handles this seamlessly through multi-series forecasting. You can group your data logically by passing a series column.

CREATE OR REPLACE SNOWFLAKE.ML.FORECAST demand_model (  
  INPUT_DATA => SYSTEM$REFERENCE('VIEW', 'historical_sales_prepared'),  
  TIMESTAMP_COLNAME => 'sales_date',  
  TARGET_COLNAME => 'adjusted_sales_quantity',  
  SERIES_COLNAME => 'sku_location_id'  
);  

This single SQL command trains a tailored model for every unique SKU and location combination. It scales effortlessly across massive datasets.

Including Holidays, Promotions, and External Variables

Enriching historical data with external variables produces highly accurate predictions. Demand fluctuates based on external factors. Adding prediction intervals and custom holiday lists to improve accuracy is an essential step.

You can feed exogenous variables into your Snowflake forecasting model. These variables might include weather data, competitor pricing, or promotional flags. You simply include these columns in your training view.

CREATE OR REPLACE SNOWFLAKE.ML.FORECAST demand_model_with_holidays (  
  INPUT_DATA => SYSTEM$REFERENCE('VIEW', 'historical_sales_with_features'),  
  TIMESTAMP_COLNAME => 'sales_date',  
  TARGET_COLNAME => 'adjusted_sales_quantity',  
  SERIES_COLNAME => 'sku_location_id'  
);  

To generate the forecast, you must also provide an inference dataset. This dataset must contain the future dates and the known future values of your external variables. For example, you already know the dates of future holidays and planned promotions.

You can learn more about configuring these features in the Snowflake time-series forecasting guide for advanced setups.

Prediction Intervals and Uncertainty Quantification

Comprehensive prediction intervals provide the depth needed for effective supply chain planning. Knowing that you will sell 100 units is helpful. Knowing that you will sell between 80 and 120 units with high confidence is actionable.

You can configure the prediction_interval during the forecast generation. This quantifies uncertainty and enables upside and downside scenario planning.

CALL demand_model_with_holidays!FORECAST(  
  INPUT_DATA => SYSTEM$REFERENCE('VIEW', 'future_features_view'),  
  TIMESTAMP_COLNAME => 'future_date',  
  SERIES_COLNAME => 'sku_location_id',  
  CONFIG_OBJECT => {'prediction_interval': 0.95}  
);  

A 95% prediction interval means you can be highly confident that actual demand will fall within the upper and lower bounds. Supply chain analysts use the upper bound to set safety stock levels. Financial analysts use the lower bound for conservative revenue projections.

Evaluating Forecast Accuracy and Feature Importance

You must measure the performance of your model before relying on it for production decisions. Standard evaluation metrics include Mean Absolute Error (MAE) and Root Mean Squared Error (RMSE). You should also track Mean Absolute Percentage Error (MAPE).

Evaluating inventory-weighted error provides a deeper understanding of business impact. Prioritizing accuracy on a fast-moving, high-margin item protects your business far more than focusing on a slow-moving, low-margin item.

You must also measure interval coverage. If you requested a 90% prediction interval, you need to verify that historical actuals fell within those bounds 90% of the time. This ensures your safety stock calculations remain valid.

For broader insights into academic standards on evaluation, we recommend reviewing Practical Time Series Forecasting for the Hospitality Manager to understand foundational metric applications.

Automating Sales Projections and Forecast Refreshes via SQL

Continuous learning keeps your model current and highly effective. As new sales data flows into your warehouse, your model must learn from it. You can achieve automated sales projections in SQL using Snowflake tasks.

You can set up a scheduled task to retrain the model weekly. You can set up another task to generate new daily forecasts.

CREATE OR REPLACE TASK weekly_model_refresh  
  WAREHOUSE = my_warehouse  
  SCHEDULE = 'USING CRON 0 0 * * 0 UTC'  
AS  
  CREATE OR REPLACE SNOWFLAKE.ML.FORECAST demand_model (...);  

This approach streamlines operations. Your data pipeline acts as a highway. The raw data enters, the model retrains, and fresh forecasts are pushed into downstream reporting tables. You achieve fully automated Snowflake time-series analysis without managing external servers.

Overcoming Pain Points: Stockouts, MLOps, and Unreliable Forecasts

Achieving reliable forecasts is a top priority for forward-thinking organizations. Adding business context to native models greatly improves their performance. Incorporating holidays, external variables, and multi-series aggregation solves common forecasting challenges smoothly.

Modern tools successfully eliminate the overhead of complex Python MLOps. Data engineers can now focus on high-value tasks instead of building data extraction pipelines. They execute machine learning securely within the data governance boundaries of Snowflake.

Finally, connecting prediction intervals to inventory planning transforms your supply chain. You can minimize inventory stockouts by aligning safety stock with the upper bounds of your forecast intervals. This directly impacts financial targets and customer satisfaction.

Stellans’ Role in Advanced Data Science for Demand Forecasting

Snowflake provides the scalable forecasting engine. We provide the data architecture and operational workflows required to make those forecasts usable in production. We ensure your data governance layer is secure and optimized.

Our team specializes in Advanced Data Science. We build hybrid machine-learning models that bridge the gap between historical data and real-world supply chain events. We help you define custom business logic for stockout corrections and promotional lifts. You can see examples of our supply chain analytics and hybrid models in our data-driven demand forecasting case study.

We work with you to turn raw data into a competitive advantage. Our goal is your growth. Clients report significantly faster insights post-implementation. We ensure your technology choices align with long-term strategy.

Take the next step in your digital transformation. Explore how we can build scalable systems and predictive models for your organization. Learn more about our demand and supply planning services and partner with us today.

Frequently Asked Questions

What is a Snowflake forecasting model?

A Snowflake forecasting model uses the native SNOWFLAKE.ML.FORECAST function to predict future values based on historical time-series data. It automatically tests and selects the best machine learning algorithms directly within your data warehouse using SQL.

Can Snowflake forecast multiple SKUs using automated sales projections in SQL?

Yes. You can configure a multi-series forecast by defining a SERIES_COLNAME in your SQL statement. This allows you to generate independent forecasts for thousands of distinct SKUs or store locations simultaneously using a single command.

What are prediction intervals in a Cortex ML forecasting setup?

Prediction intervals quantify the uncertainty of a forecast. Instead of providing just one predicted number, the model provides an upper and lower bound. A 95% prediction interval indicates a high probability that the actual future value will fall between those two boundaries.

References

Article By:

https://stellans.io/wp-content/uploads/2024/09/DavidStellans2-1-2.png
David Ashirov

Co-founder, CTO