Skip to main content

Edge Cleaning

Remove leading and trailing zeros from time series edges.

FunctionDescription
drop_leading_zerosRemove initial zero values
drop_trailing_zerosRemove trailing zero values
drop_edge_zerosRemove both leading and trailing zeros
Showing 3 of 3

anofox_fcst_ts_drop_leading_zeros

Removes initial zero values from the start of each series. Useful for products that had no sales before launch.

Parameters

ParameterTypeRequiredDescription
table_nameVARCHARYesSource table
group_colANYNoGroup column
date_colDATE/TIMESTAMP/INTEGERYesDate column
value_colDOUBLEYesValue column

Output

Returns table with leading zeros removed from each series.

Example

-- Remove pre-launch zeros
SELECT * FROM anofox_fcst_ts_drop_leading_zeros(
'sales_by_product',
product_id,
date,
sales
);
warning

Results differ if gap-filling functions were previously applied. Gap-filled NULLs converted to zeros will also be removed.


anofox_fcst_ts_drop_trailing_zeros

Removes trailing zero values from the end of each series. Useful for discontinued products.

Parameters

ParameterTypeRequiredDescription
table_nameVARCHARYesSource table
group_colANYNoGroup column
date_colDATE/TIMESTAMP/INTEGERYesDate column
value_colDOUBLEYesValue column

Output

Returns table with trailing zeros removed from each series.

Example

-- Remove post-discontinuation zeros
SELECT * FROM anofox_fcst_ts_drop_trailing_zeros(
'sales_by_product',
product_id,
date,
sales
);
warning

Results differ if gap-filling functions were previously applied.


anofox_fcst_ts_drop_edge_zeros

Removes both leading and trailing zero values in one operation.

Parameters

ParameterTypeRequiredDescription
table_nameVARCHARYesSource table
group_colANYNoGroup column
date_colDATE/TIMESTAMP/INTEGERYesDate column
value_colDOUBLEYesValue column

Output

Returns table with edge zeros removed from each series.

Example

-- Clean both edges at once
SELECT * FROM anofox_fcst_ts_drop_edge_zeros(
'sales_by_product',
product_id,
date,
sales
);
warning

Results differ if gap-filling functions were previously applied.


When to Use Edge Cleaning

ScenarioFunctionReason
New product launchdrop_leading_zerosRemove pre-launch history
Discontinued productdrop_trailing_zerosRemove post-discontinuation
Product lifecycledrop_edge_zerosClean both ends
Intermittent demandDon't useZeros are valid data points

Edge Cleaning Workflow

-- 1. Analyze edge zeros before cleaning
SELECT
product_id,
MIN(CASE WHEN sales > 0 THEN date END) as first_sale,
MAX(CASE WHEN sales > 0 THEN date END) as last_sale,
MIN(date) as data_start,
MAX(date) as data_end
FROM sales_by_product
GROUP BY product_id;

-- 2. Clean edges
CREATE TABLE sales_cleaned AS
SELECT * FROM anofox_fcst_ts_drop_edge_zeros(
'sales_by_product',
product_id,
date,
sales
);

-- 3. Verify cleaning
SELECT
product_id,
MIN(date) as new_start,
MAX(date) as new_end,
COUNT(*) as remaining_rows
FROM sales_cleaned
GROUP BY product_id;

Order of Operations

The recommended order for data preparation:

-- 1. Edge cleaning FIRST (before gap filling)
CREATE TABLE step1 AS
SELECT * FROM anofox_fcst_ts_drop_edge_zeros(
'raw_data', product_id, date, sales
);

-- 2. Gap filling SECOND
CREATE TABLE step2 AS
SELECT * FROM anofox_fcst_ts_fill_gaps(
'step1', product_id, date, sales, '1d'
);

-- 3. Imputation THIRD
CREATE TABLE step3 AS
SELECT * FROM anofox_fcst_ts_fill_nulls_forward(
'step2', product_id, date, sales
);

-- 4. Filtering LAST
CREATE TABLE ready AS
SELECT * FROM anofox_fcst_ts_drop_short(
'step3', product_id, 30
);

🍪 Cookie Settings