Skip to main content
Data source class for populating date range parameter options from a database table or query. This class can be imported from the squirrels.data_sources or the squirrels module.

Constructor

Creates a DateRangeDataSource object.
def __init__(
    self, table_or_query: str, default_start_date_col: str, 
    default_end_date_col: str, 
    *, date_format: str = '%Y-%m-%d', min_date_col: str | None = None, 
    max_date_col: str | None = None, id_col: str | None = None, 
    source: SourceEnum = SourceEnum.CONNECTION, user_group_col: str | None = None, 
    parent_id_col: str | None = None, connection: str | None = None
) -> None:

Examples

A DateRangeDataSource object is created in the pyconfigs/parameters.py file. It must be created in a function decorated with the create_from_source factory method from DateRangeParameter.

Usage example in parameters.py

from squirrels import parameters as p, data_sources as ds

@p.DateRangeParameter.create_from_source(
    name="reporting_period", 
    label="Reporting Period",
    description="Date range for the report"
)
def reporting_period_source():
    return ds.DateRangeDataSource(
        table_or_query="""
            SELECT 
                CURRENT_DATE - INTERVAL '30 days' AS default_start,
                CURRENT_DATE AS default_end,
                CURRENT_DATE - INTERVAL '1 year' AS min_date,
                CURRENT_DATE AS max_date
        """,
        default_start_date_col="default_start",
        default_end_date_col="default_end",
        min_date_col="min_date",
        max_date_col="max_date"
    )
In addition, the following are some additional examples for creating a DateRangeDataSource object.

Using a table from a specific connection

This example uses a table called “fiscal_periods” from the “analytics_db” connection.
ds.DateRangeDataSource(
    table_or_query="fiscal_periods",
    default_start_date_col="period_start",
    default_end_date_col="period_end",
    min_date_col="fiscal_year_start",
    max_date_col="fiscal_year_end",
    connection="analytics_db"
)
The connection must either be defined in squirrels.yml or the connections.py file.

Using seeds as the data source

This example uses a seed file called “date_range_config”.
ds.DateRangeDataSource(
    table_or_query="date_range_config",
    default_start_date_col="start_date",
    default_end_date_col="end_date",
    source=ds.SourceEnum.SEEDS
)

Using a custom date format

This example demonstrates using a different date format for the dates stored in the database.
ds.DateRangeDataSource(
    table_or_query="""
        SELECT 
            '01/01/2025' AS start_date,
            '12/31/2025' AS end_date
    """,
    default_start_date_col="start_date",
    default_end_date_col="end_date",
    date_format="%m/%d/%Y"
)

Enabling cascading effects with a parent parameter

In this example, the date range is determined by the selected value of another parameter called “quarter”.
@p.DateRangeParameter.create_from_source(
    name="analysis_period", 
    label="Analysis Period",
    description="The date range for the selected quarter",
    parent_name="quarter"
)
def analysis_period_source():
    return ds.DateRangeDataSource(
        table_or_query="quarterly_periods",
        default_start_date_col="quarter_start_date",
        default_end_date_col="quarter_end_date",
        min_date_col="year_start_date",
        max_date_col="year_end_date",
        parent_id_col="quarter_id"  # Cascades based on quarter selection
    )