apache / superset

Apache Superset is a Data Visualization and Data Exploration Platform
https://superset.apache.org/
Apache License 2.0
62.9k stars 13.91k forks source link

Big number with threadline ERROR with "cumsum" advanced analytics and filters defined in jinja template #30066

Open maksperskiy opened 2 months ago

maksperskiy commented 2 months ago

Bug description

Error in Big number with threadline chart if No data with filters defined by jinja template in complex CTE SQL query

How to reproduce the bug

  1. Define complex dataset with CTE and impletement existed filters as jinja template image

  2. Create chart Big number with threadline. Set rolling function "cumsum" image

  3. Place chart on a dashboard with different related (by jinja) filters.

  4. Select values in related (by jinja) filters to get No data

  5. Result image

Screenshots/recordings

No response

Superset version

4.0.2

Python version

Not applicable

Node version

Not applicable

Browser

Chrome

Additional context

No response

Checklist

dosubot[bot] commented 2 months ago

I found a similar open issue that might be relevant to your problem: Error: Referenced columns not available in DataFrame in Mixed Charts when one query has only nulls. This issue was last updated on August 13, 2024.

To resolve the "Referenced columns not available in DataFrame" error when using Jinja templating for filters in a Big number with trendline chart in Superset version 4.0.2, you need to ensure that the columns referenced in your Jinja template are actually present in the DataFrame being queried. Here are some steps to troubleshoot and resolve the issue:

  1. Verify Column Names: Ensure that the column names used in your Jinja template match exactly with the column names in your DataFrame.

  2. Check Query Result: Make sure that the columns you are referencing in the Jinja template are included in the query result. You can do this by printing or logging the DataFrame columns before applying the template.

  3. Update Jinja Template: Modify your Jinja template to correctly reference the available columns.

Additionally, Superset provides several Jinja context functions that can be used to dynamically access and manipulate data. These functions include url_param, current_user_id, current_username, current_user_email, filter_values, and get_filters. These functions can be used to ensure that your template references the correct columns and values.

Here is an example of how you can modify your Jinja template to ensure it references the correct columns:

from jinja2 import Template

# Example DataFrame
data = {
    'column1': [1, 2, 3],
    'column2': [4, 5, 6]
}
df = pd.DataFrame(data)

# Jinja template
template_str = """
{% if 'column1' in df.columns %}
    {{ df['column1'].sum() }}
{% else %}
    Column 'column1' not found in DataFrame
{% endif %}
"""

# Create a Jinja template
template = Template(template_str)

# Render the template with the DataFrame
result = template.render(df=df)
print(result)

In this example, the Jinja template checks if 'column1' is present in the DataFrame columns before attempting to sum its values. This prevents the error by ensuring that the column exists [1][2].

To continue talking to Dosu, mention @dosu.