Filters

The filter function in DUQL is used to select rows from a dataset based on specified conditions. It allows you to narrow down your data to only the records that meet certain criteria.

Syntax

filter: <condition>

Parameters

Parameter
Type
Required
Description

filter

string

Yes

A boolean expression representing the filter condition

Behavior

  • Evaluates the specified condition for each row in the dataset.

  • Retains only the rows where the condition evaluates to true.

  • Can use complex expressions involving multiple columns, functions, and operators.

Examples

Basic Comparison

filter: age > 18

Date Range

Numeric Range

String Matching

Date Calculation

Using Parameters

Array Contains

Complex Conditions

Nested Conditions

Using SQL Functions

Best Practices

  1. 🎯 Be as specific as possible with your filter conditions to improve query performance.

  2. πŸ” Use parentheses to clarify the order of operations in complex conditions.

  3. πŸ§ͺ Test your filter conditions with sample data to ensure they produce expected results.

  4. πŸ“Š Consider using multiple filter steps for complex logic, improving readability.

  5. πŸš€ Place filters early in your query pipeline to reduce data volume for subsequent operations.

  6. πŸ”’ Use parameters ($1, $2, etc.) for dynamic filtering when appropriate.

Real-World Use Case

Here's an example of a DUQL query that uses multiple filter steps to analyze high-value orders from loyal customers:

This query demonstrates:

  1. Filtering orders by date

  2. Joining with customer and product data

  3. Filtering for loyal customers (account created before 2022)

  4. Calculating order totals and identifying discounted orders

  5. Filtering for high-value orders (over $1000)

  6. Aggregating customer data

  7. Filtering for repeat high-value customers (3 or more orders)

  8. Calculating average order value and discount rate

  9. Filtering for customers who mostly pay full price

  10. Sorting and limiting the results

The resulting top_loyal_customers dataset provides a focused view of the most valuable loyal customers who frequently make large purchases without heavy reliance on discounts.


πŸ’‘ Tip: The filter function is your primary tool for data selection in DUQL. Use it strategically throughout your query pipeline to focus your analysis on the most relevant data. Combine it with generate and group functions for powerful data insights!

Last updated

Was this helpful?