Filters
Learn how to define reusable filters in your semantic models
Introduction to Filters
Filters define named, reusable conditions that resolve to true
or false
. They encapsulate common query constraints, allowing users to easily apply predefined criteria without rewriting the logic repeatedly. Think of them as saved segments or common WHERE
clauses.
Using filters makes queries cleaner, more consistent, and easier to understand, focusing the analysis on specific questions rather than boilerplate filtering logic.
Here’s the basic structure for defining a filter:
Filters vs. Dimensions/Measures
It’s crucial to distinguish Filters from Dimensions and Measures:
- Dimensions: Describe attributes (e.g.,
country
,status
). Used for grouping, slicing, and direct filtering (WHERE country = 'USA'
). A boolean Dimension (e.g.,is_active
) represents a fundamentaltrue
/false
attribute stored in the data. - Measures: Quantifiable, aggregatable values (e.g.,
total_revenue
). Used in aggregation functions (SUM
) andHAVING
clauses. - Filters: Define reusable conditions or subsets (e.g.,
last_30_days
,high_value_customers
). They must resolve to boolean and generateWHERE
clause logic.
Guideline:
- If a boolean concept describes a direct attribute (e.g., a user’s stored active status), use a boolean Dimension.
- If you need a reusable condition (especially involving calculations, date logic, combined attributes, or related data), use a Filter.
Defining Basic Filters
Basic filters define simple, non-parameterized conditions.
Filter Fields
Field | Required | Description |
---|---|---|
name | Yes | Unique name for the filter within the model. |
description | Yes | Explains what condition the filter represents. This is mandatory. |
expr | No | The SQL expression defining the filter’s logic (must evaluate to boolean). Optional only if name matches a boolean dimension. |
args | No | A list of Argument objects to parameterize the filter’s expression. Defaults to an empty list if omitted. |
Writing the expr
- Purpose: Defines the filter’s logic as a SQL expression (compatible with your data warehouse) that evaluates to
true
orfalse
. - Optionality:
expr
is optional only if the filter’sname
exactly matches the name of a boolean dimension defined within the same model. In this case, the filter implicitly meansdimension_name = true
.- Convention: Name filters that map directly to boolean dimensions the same as the dimension (e.g., filter
is_active
for dimensionis_active
).
- Convention: Name filters that map directly to boolean dimensions the same as the dimension (e.g., filter
- Content: The
expr
can reference:- Dimensions in the model’s underlying table/view.
- SQL functions and operators.
- Literals (strings, numbers, booleans).
- Arguments (using
{{arg_name}}
syntax).
When referencing fields within the same model in the expr
, you typically don’t need a table prefix (e.g., country = 'USA'
). However, using prefixes (e.g., users.country
) can enhance clarity, especially when filters might be used in queries involving joins (even implicit ones via relationships). See Best Practices.
Example: Basic Filters
Advanced Filter Types
Filters can incorporate parameters or reference data from related models.
Parameterized Filters
Create dynamic filters that accept user input via args
. Reference parameters using {{parameter_name}}
.
args
: A list defining each parameter. See the Semantic Model Reference for the full specification ofArgument
fields (name
,type
,description
,default
).
Example: Parameterized Filter
Cross-Model Filters
Filters can define conditions based on data in related models, provided a relationship
exists. The semantic layer uses these relationships to potentially generate necessary joins or subqueries when the filter is applied in a query that also includes the required join defined by the relationship.
See the Semantic Model Reference for details on defining relationships.
Example: Cross-Model Filter
Using cross-model references requires careful consideration of joins when querying. See “Handling Joins and Related Models” below.
Best Practices & Considerations
Follow these guidelines for effective filter definition.
General Best Practices
Ensure Boolean Output
The expr
must evaluate to a boolean (true
/false
).
Clear Naming & Descriptions
Use descriptive name
s (snake_case) and mandatory, clear description
s explaining the condition.
Match Boolean Dimensions
For filters representing dimension = true
, name the filter identically to the boolean dimension and omit the expr
.
Document Dependencies
Clearly state required relationships
/joins or parameters in the description
.
Expression Best Practices
- Use Table Prefixes for Related Models: Strongly recommended when
expr
involves fields that might also exist in other models or when using cross-model filters viarelationships
. Usemodel_name.column_name
(e.g.,users.country
) orrelationship_name.column_name
(e.g.,user_orders.product_sku
). - Clarity and Readability: Write clear
expr
logic. Use parentheses()
for complex conditions to ensure correct operator precedence. - Null Handling: Be mindful of nulls. Use
COALESCE
,IS NULL
,IS NOT NULL
etc., withinexpr
as needed for robust logic, as filter expressions are often used inWHERE
clauses where null propagation matters.
Good Expression Example (Parameterized):
Parameter Best Practices
- Explicit Typing: Always define the
type
for each parameter (string
,integer
,number
,date
,boolean
). - Argument Fields: Ensure each argument in the
args
list includes the requiredname
,type
, anddescription
fields. - Defaults: Provide
default
values for optional parameters. - Clear Documentation: Provide a meaningful
description
for each parameter. - Placeholders: Reference parameters in the
expr
using double curly braces:{{parameter_name}}
.
Handling Joins and Related Models
- Primary Model Association: Filters are defined within a specific model.
- Explicit Joins Required in Query: If a filter’s
expr
references fields from a related model (via arelationship
), the query using that filter must explicitly include the necessary join to that related model (using therelationship
). - No Automatic Joins: The semantic layer does not automatically add joins based solely on a filter’s
expr
. It validates that joins used in a query correspond to definedrelationships
. - Document Join Requirements: The filter’s
description
must state if it relies on data from related models and whichrelationships
/joins are required. Example: “Filters for users whose latest order was cancelled. Requires joining using the ‘latest_order’ relationship.”
Comprehensive Example
Here’s an example within a customers
model context, incorporating various filter types and practices: