I am working on a dataset consolidating information from different APIs (SCR, SPC, MARGEM_CONSIGNAVEL, OPEN_FINANCE) and trying to calculate accumulated totals and percent variation directly in QuickSight.
Dataset Structure / Estrutura do Dataset:
I have a view in Athena/SQL structured as follows:
-
Daily counts CTEs / CTEs de contagem diária:
scr_counts,spc_counts,margemconsignavel_cte, andopenfinance_ctecalculate the distinct number of CPFs (total_dia) per day (data) for each API. -
Unified dataset / Dataset unificado:
all_countsmerges all daily counts. -
Monthly total / Acumulado mensal:
monthly_totalsumstotal_diaby API per month (total_mes). -
Cost calculation / Cálculo de custo: calculated directly using
CASE WHENin the view to assigncusto_por_registroandcusto_total_diabased on API and monthly totals.
Example of resulting dataset / Exemplo do dataset resultante:
| # | data | api | total_dia | total_mes | custo_por_registro | custo_total_dia | variacao_percentual |
|---|---|---|---|---|---|---|---|
| 1 | 2025-08-12 | MARGEM_CONSIGNAVEL | 1 | 37859 | 0.08 | 0.08 | |
| 2 | 2025-08-13 | MARGEM_CONSIGNAVEL | 419 | 37859 | 0.08 | 33.52 | 41800 |
| 3 | 2025-08-14 | MARGEM_CONSIGNAVEL | 109 | 37859 | 0.08 | 8.72 | 0 |
| 4 | 2025-08-18 | MARGEM_CONSIGNAVEL | 163 | 37859 | 0.08 | 13.04 | 0 |
| … | … | … | … | … | … | … | … |
Fields Already Created in QuickSight / Campos já criados no QuickSight:
Filtered Period Total / Acumulado do período filtrado:
totalAcumuladoPeriodoFiltrado =
sumIf(
{total_dia},
truncDate('DD', {data}) >= truncDate('DD', ${pDiaInicio})
AND truncDate('DD', {data}) <= truncDate('DD', ${pDiaFim})
)
Previous Period Dates / Datas do período anterior:
dataInicioPeriodoAnterior =
addDateTime(-duracaoPeriodoDias, 'DD', truncDate('DD', ${pDiaInicio}))
dataFimPeriodoAnterior =
addDateTime(-1, 'DD', truncDate('DD', ${pDiaInicio}))
duracaoPeriodoDias =
abs(
dateDiff(truncDate('DD', ${pDiaFim}), truncDate('DD', ${pDiaInicio}), 'DD')
) + 1
Percent Variation / Variação percentual:
variacaoPercentualCalculada =
ifelse(
${totalAcumuladoPeriodoAnterior} != 0,
(${totalAcumuladoPeriodoFiltrado} - ${totalAcumuladoPeriodoAnterior}) / ${totalAcumuladoPeriodoAnterior} * 100,
NULL
)
Problem Encountered / Problema Encontrado:
When trying to calculate the previous period total / Ao tentar criar o total acumulado do período anterior:
totalAcumuladoPeriodoAnterior =
sumIf(
{total_dia},
truncDate('DD', {data}) >= addDateTime(-${duracaoPeriodoDias}, 'DD', truncDate('DD', ${pDiaInicio}))
AND truncDate('DD', {data}) <= addDateTime(-1, 'DD', truncDate('DD', ${pDiaInicio}))
)
-
The result is always empty (zero) / sempre vazio (zero).
-
Likely because QuickSight cannot resolve other calculated fields like
duracaoPeriodoDiasinsidesumIf. -
Everything needs to be inline within the
sumIfto work correctly / Tudo precisa estar inline dentro dosumIfpara funcionar corretamente.
What I Still Need / O que ainda preciso criar:
-
A working
totalAcumuladoPeriodoAnteriorcalculation in QuickSight that sumstotal_diafor the corresponding previous period / Um cálculo funcional detotalAcumuladoPeriodoAnteriorsomandototal_diano período anterior correspondente. -
After that,
variacaoPercentualCalculadashould compute correctly / Depois disso,variacaoPercentualCalculadapoderá ser calculada corretamente. -
The solution should avoid relying on intermediate calculated fields inside
sumIfand use inline date calculations / A solução ideal deve evitar campos calculados intermediários dentro dosumIfe usar cálculos de data inline.
Goal / Objetivo:
-
Compare current vs previous period totals per API / Comparar totais do período atual vs período anterior por API.
-
Display percent variation as KPI or chart in QuickSight / Exibir a variação percentual como KPI ou gráfico no QuickSight.