Skip to main content
Data source class for populating date 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 DateDataSource object.
def __init__(
    self, table_or_query: str, default_date_col: str, 
    *, min_date_col: str | None = None, max_date_col: str | None = None, 
    date_format: str = '%Y-%m-%d', 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 DateDataSource 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 DateParameter.

Usage example in parameters.py

from squirrels import parameters as p, data_sources as ds

@p.DateParameter.create_from_source(
    name="report_date", 
    label="Report Date",
    description="Date to generate the report for"
)
def report_date_source():
    return ds.DateDataSource(
        table_or_query="""
            SELECT 
                CURRENT_DATE AS default_date,
                CURRENT_DATE - INTERVAL '1 year' AS min_date,
                CURRENT_DATE AS max_date
        """,
        default_date_col="default_date",
        min_date_col="min_date",
        max_date_col="max_date"
    )
In addition, the following are some additional examples for creating a DateDataSource object.

Using a table from a specific connection

This example uses a table called “fiscal_calendar” from the “analytics_db” connection.
ds.DateDataSource(
    table_or_query="fiscal_calendar",
    default_date_col="reporting_date",
    min_date_col="period_start",
    max_date_col="period_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_config”.
ds.DateDataSource(
    table_or_query="date_config",
    default_date_col="default_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/31/2025' AS default_date
    """,
    default_date_col="default_date",
    date_format="%m/%d/%Y"
)

Enabling cascading effects with a parent parameter

In this example, the default, minimum, and maximum dates are determined by the selected value of another parameter called “region”.
@p.DateParameter.create_from_source(
    name="availability_date", 
    label="Availability Date",
    description="The date of availability for the selected region",
    parent_name="region"
)
def report_date_source():
    return ds.DateDataSource(
      table_or_query="regional_availability",
      default_date_col="availability_date",
      min_date_col="earliest_date",
      max_date_col="latest_date",
      parent_id_col="region_id"  # Cascades based on region selection
  )