Dynamic Filters in Reports: When and How to Use Them in iVendNext

Dynamic Filters in Reports: When and How to Use Them in iVendNext

Overview

Dynamic filters empower users to refine report data on the fly, making them indispensable for real-time analytics in iVendNext. Unlike static filters, dynamic filters allow end-users to adjust parameters (e.g., date ranges, statuses, or item groups) without modifying the report itself. This article covers:
Where dynamic filters work (Query Reports and Script Reports).
How to implement them in SQL/Python.
Best practices for performance and usability.




1. Dynamic Filters vs. Static Filters


Feature

Dynamic Filters

Static Filters

Flexibility

Adjustable by users at runtime

Hardcoded in report design

Report Types

Query/Script Reports only

All report types

Use Case

Dashboards, ad-hoc analysis

Standardized reports


Note: Report Builder does not support dynamic filters.




2. Implementing Dynamic Filters

A. In Query Reports (SQL)

Use %(...)s placeholders for filter values:


SELECT * FROM `tabSales Invoice`  

WHERE  

  posting_date BETWEEN %(from_date)s AND %(to_date)s  

  AND customer = %(customer)s  

Step-by-Step Setup:

  1. Define Filters in UI:


  • Navigate to Report > Edit.

  • Add filter fields (e.g., Date, Customer).


  1. Map Placeholders:


  • Ensure SQL placeholders match filter names (e.g., from_date).


  1. User Interaction:


  • Users select values when running the report.




B. In Script Reports (Python)

Define filters in the execute() function:


def execute(filters=None):  

    conditions = "1=1"  

    if filters.get("from_date"):  

        conditions += f" AND posting_date >= '{filters['from_date']}'"  

    if filters.get("customer"):  

        conditions += f" AND customer = '{filters['customer']}'"  


    data = frappe.db.sql(f"""  

        SELECT name, customer, grand_total  

        FROM `tabSales Invoice`  

        WHERE {conditions}  

    """, as_dict=True)  


    return data  

Filter Configuration:

Add a filters dictionary to specify UI fields:


filters = [  

    {  

        "fieldname": "from_date",  

        "label": "From Date",  

        "fieldtype": "Date"  

    },  

    {  

        "fieldname": "customer",  

        "label": "Customer",  

        "fieldtype": "Link",  

        "options": "Customer"  

    }  

]  




3. Best Practices

Some of the best practices are:


Performance Optimization

  • Indexed Fields: Apply filters to indexed columns (e.g., posting_date).

  • Default Values: Pre-set common ranges (e.g., current fiscal year):


"default": frappe.utils.nowdate()  

User Experience

  • Dropdowns for High-Cardinality Fields: Use Link fields instead of text inputs.

  • Mandatory Filters: Force critical filters (e.g., company):


"reqd": 1  

Security

  • Sanitize Inputs: Always use placeholders (%(...)s) to prevent SQL injection.

  • Restrict Access: Limit filterable fields to sensitive data (e.g., cost_center).




4. Common Use Cases

A. Date-Range Analytics

WHERE posting_date BETWEEN %(start_date)s AND %(end_date)s  


Ideal for: Financial period comparisons, sales trends.


B. Status-Based Filtering

if filters.get("status"):  

    conditions += f" AND status = '{filters['status']}'"  


Ideal for: Tracking workflows (e.g., "Draft" vs. "Submitted" invoices).


C. Multi-Select Filters

WHERE item_group IN (%(item_groups)s)  /* Requires CSV input */  


Ideal for: Category-level reports.




5. Troubleshooting

Here’s a quick look at some common issues you might run into.


Issue: Filters Not Applying

Cause: Placeholder name mismatch (e.g., from_date vs. start_date).
Fix: Audit SQL/Python for consistency.


Issue: Slow Performance

Cause: Non-indexed filters or fetching excessive data.
Fix:


  • Add LIMIT 1000.

  • Use EXPLAIN to optimize queries.


Issue: Blank Results

Cause: Overlapping conditions (e.g., status="Paid" AND status="Unpaid").
Fix: Simplify logic with OR clauses.




    • Related Articles

    • Understanding the Three Types of Reports

      Overview Reports are a critical component of iVendNext, enabling users to extract, analyze, and present data efficiently. This article explores the three primary types of reports available in iVendNext: Report Builder, Query Reports, and Script ...
    • Script Reports: Leveraging Python for Complex Calculations

      Overview Script Reports in iVendNext use Python for advanced, server-side reporting with dynamic filters, custom formatting, and multi-dataset support—features beyond Report Builder and Query Reports. This article explains their use, setup, and admin ...
    • Advanced Reporting - Custom Reports and Scripts

      Overview This article gives you an overview of advanced reporting in iVendNext, highlighting tools like custom templates, SQL queries, and Python scripts for deeper, tailored insights. When to Use Advanced Reporting Use these tools when you need: ✔ ...
    • Creating Custom Reports Using Report Builder

      Overview Report Builder in iVendNext lets you create custom reports easily—no technical skills needed. From sales summaries to inventory updates, it helps you pull and format data quickly. This article guides you through building a report, choosing ...
    • Working with Query Reports - Fetching Data Directly from the Database

      Overview Query Reports in iVendNext let advanced users pull data using SQL for detailed, custom reporting. Unlike the no-code Report Builder, they offer full control over data queries—ideal for complex needs. However, cloud users can't write SQL ...