| Products | Versions |
|---|---|
| Spotfire Analyst | 14.6.0 |
When using custom query parameterization in Spotfire, parameters are passed to the data source as bind values (literal values), not as substitutions into the SQL text as column names or other identifiers.
If a parameter, such as ?LineByField, is used directly in a PARTITION BY clause (e.g., PARTITION BY ?LineByField), the database interprets the parameter's value as a constant string. For example, if ?LineByField has the value 'location', the database receives PARTITION BY 'location'. This results in all rows falling into a single partition because every row has the same constant partition value, so calculations run across the entire table rather than by the intended column. This behavior is expected and documented.
In contrast, parameterization works as expected in a WHERE clause (e.g., location = ?LineByField) because the parameter is compared to a column as a value, which is a supported use case for bind values.
General Workaround: Using a CASE Expression
A parameter cannot be used directly as a column name in the PARTITION BY clause. Instead, use the parameter's value inside a CASE expression to return the desired column for partitioning.
Example Custom Query:sql select quarter, agein_cobm, agein_amt, location, business_unit, sum(agein_cobm) over (partition by location order by quarter) as agein_cobm_runsum_loc, sum(agein_amt) over (partition by location order by quarter) as agein_amt_runsum_loc, sum(agein_cobm) over (partition by case when cast(?LineByField as varchar(50)) = 'location' then location when cast(?LineByField as varchar(50)) = 'business_unit' then business_unit end order by quarter) as agein_cobm_runsum_parm, sum(agein_amt) over (partition by case when cast(?LineByField as varchar(50)) = 'location' then location when cast(?LineByField as varchar(50)) = 'business_unit' then business_unit end order by quarter) as agein_amt_runsum_parm from over_custom_expression_parameterized
Please note:
PostgreSQL-Specific Workaround: Using JSON Functions
For PostgreSQL, you can avoid a lengthy CASE statement by looking up the column value using JSON functions.
Example Custom Query (PostgreSQL):sql select quarter, agein_cobm, agein_amt, location, business_unit, sum(agein_cobm) over (partition by to_jsonb(t) ->> cast(?LineByField as text) order by quarter) as agein_cobm_runsum_parm, sum(agein_amt) over (partition by to_jsonb(t) ->> cast(?LineByField as text) order by quarter) as agein_amt_runsum_parm from over_custom_expression_parameterized t
How it works: to_jsonb(t) converts each row into a JSON object with column names as keys. The ->> operator then returns the value of the column whose name is passed in ?LineByField, and that value is used for partitioning. Every column in the table becomes selectable without listing it in the query.
Please note:
Custom query parameters in Spotfire are passed as literal bind values to the data source, not as SQL identifiers. This prevents their direct use as column names in the PARTITION BY clause, leading to incorrect partitioning. This article explains the expected behavior and provides workarounds.
Feedback submitted!