Ensuring Filter-Dependent Columns Always Exist in Dashboards

Last updated: July 23, 2025

When building dashboards that rely on SQL filter variables (e.g., {{ startDate }}, {{ endDate }}), it’s common to wrap conditional logic in templating syntax such as [[ ... ]] so the logic is only included when filters are set. However, this can introduce an issue:

If the filter isn’t applied, the entire CASE statement may be skipped, and the column won’t exist in the result set.

This can break downstream charts or filters that expect the column to always be available.

The Fix: Use a Dummy WHEN Clause Outside the Conditional Block

To ensure that your calculated column is always included in the dataset even when no filter is applied, add a dummy WHEN clause before the conditional block.

ClickHouse Example

SELECT *,

CASE 
  WHEN 1 = 1
  [[
    WHEN "created_at" < parseDateTimeBestEffortOrNull({{ startDate }}) THEN 'Before Filter Range'
    WHEN "created_at" > parseDateTimeBestEffortOrNull({{ endDate }}) THEN 'After Filter Range'
  ]]
  ELSE 'Within Filter Range'
END AS status_label

FROM my_table

PostgreSQL Example

SELECT *,

CASE 
  WHEN 1 = 1
  [[
    WHEN created_at < {{ startDate }}::timestamp THEN 'Before Filter Range'
    WHEN created_at > {{ endDate }}::timestamp THEN 'After Filter Range'
  ]]
  ELSE 'Within Filter Range'
END AS status_label

FROM my_table

BigQuery Example

SELECT *,

CASE 
  WHEN 1 = 1
  [[
    WHEN created_at < PARSE_TIMESTAMP('%Y-%m-%d', {{ startDate }}) THEN 'Before Filter Range'
    WHEN created_at > PARSE_TIMESTAMP('%Y-%m-%d', {{ endDate }}) THEN 'After Filter Range'
  ]]
  ELSE 'Within Filter Range'
END AS status_label

FROM my_table

Snowflake Example

SELECT *,

CASE 
  WHEN 1 = 1
  [[
    WHEN created_at < TO_TIMESTAMP({{ startDate }}) THEN 'Before Filter Range'
    WHEN created_at > TO_TIMESTAMP({{ endDate }}) THEN 'After Filter Range'
  ]]
  ELSE 'Within Filter Range'
END AS status_label

FROM my_table

Why This Works

  • The initial WHEN 1 = 1 ensures that the CASE expression is syntactically valid and always returns a column, even when the filter block is excluded.

  • Wrapping the dynamic conditions in [[ ... ]] ensures they’re only evaluated when the corresponding variables are set.

  • The ELSE clause guarantees a default fallback value when no condition is matched.

When to Use

Use this approach if:

  • Your dashboard uses filter variables with optional input

  • You’re conditionally rendering logic via [[ ... ]]

  • You need the output column to always exist for downstream joins, filters, or visualizations