I am trying to create a calculated field in QuickSight that returns a value only for the last period (based on the user-selected DATE_BREAKDOWN: Daily / Weekly / Monthly / Hourly).
The following calculated field works correctly when the return value is a raw field (WINS_DOLLAR_SC).
This field allows me to sort the pivot by the latest period only:
ifelse(
{DATE_BREAKDOWN}= ‘Daily’ AND PERIOD = addDateTime(-1, ‘DD’, truncDate(‘DD’, now())),
{WINS_DOLLAR_SC},
ifelse(
{DATE_BREAKDOWN} = ‘Weekly’ AND truncDate(‘WK’,PERIOD) = truncDate(‘WK’, addDateTime(-1, ‘DD’, now())),
{WINS_DOLLAR_SC},
ifelse(
{DATE_BREAKDOWN} = ‘Monthly’ AND truncDate(‘MM’,PERIOD) = truncDate(‘MM’, addDateTime(-1, ‘DD’, now())),
{WINS_DOLLAR_SC},
ifelse(
{DATE_BREAKDOWN}= ‘Hourly’ AND PERIOD = addDateTime(-1, ‘HH’, truncDate(‘HH’, now())),
{WINS_DOLLAR_SC},
NULL
)
)
)
)
This calculated field works as expected inside a Pivot Table and allows me to sort rows by the “last period only” measure.
When I try to return an aggregated metric instead of a raw field, e.g.:
sum({WINS_DOLLAR_SC}) / sum({BETS_FOR_SOURCE})
Inside the same IFELSE structure, QuickSight shows the following error:
Mismatched aggregation. Custom aggregations can’t contain both aggregated and nonaggregated fields, in any combination.
Even when all conditions evaluate to a single period,
even when PERIOD is fully determined,
and even when the logic is identical except for returning an aggregated measure.
Request:
I would like to understand:
-
Why this IFELSE logic works when returning a raw field but fails when returning a simple aggregated measure (sum/sum) even though the IFELSE conditions themselves do not contain any aggregations.
-
What is the correct or recommended way to implement a “last period only” aggregated measure (e.g., sum(wins)/sum(bets)) that can be used for sorting in a Pivot Table, while respecting the
DATE_BREAKDOWNlogic. -
Whether there is any workaround or best practice for calculating this type of “last period metric” in QuickSight without moving the logic to the data warehouse.