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

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

Introduction

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 guide 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

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

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.




Conclusion

Dynamic filters transform static reports into interactive tools for decision-making. By mastering them in Query/Script Reports, you enable:
Self-service analytics for business users.
Real-time data exploration.
Reduced dependency on developers.


For simple reports, use static filters in Report Builder; for complex needs, combine dynamic filters with Script Reports.




Key Takeaways

✔ Use %(...)s placeholders in SQL and filters dict in Python.
✔ Optimize with indexes and default values.
✔ Secure data with input sanitization.




    • Related Articles

    • Key Reports in iVendNext: An Overview

      Introduction iVendNext offers a comprehensive suite of Key Reports that provide valuable insights into various aspects of your business operations. These reports are designed to help you monitor sales performance, track customer behavior, manage ...
    • Key Operational Reports in iVendNext: A Comprehensive Guide

      Overview Operational reports are the backbone of retail management, providing critical insights into sales, inventory, and financial performance. iVendNext offers a robust suite of reports that help retailers monitor daily operations, identify ...
    • Other Useful Reports in iVendNext: A Comprehensive Guide

      Introduction While the Key Reports in iVendNext provide critical insights into sales performance, customer behavior, and inventory management, the platform also offers a range of Other Useful Reports that cater to specific business needs. These ...
    • Advanced Field Types: Geolocation and Dynamic Link

      Introduction iVendNext offers specialized advanced field types that enable powerful data handling beyond standard text or dropdown fields. Two particularly versatile options are the Geolocation Field (for mapping and spatial data) and the Dynamic ...
    • Operational Reports

      This article provides an overview of the key reports available in iVendNext. It also shows how to create custom reports and explains how to add, remove, and rearrange columns, apply both predefined and custom filters, and sort the data. Introduction ...