Hi everyone,
I’m working on a QuickSight pivot table and running into an issue with the LAG function. I’d appreciate any guidance!
Setup
- Columns: Aggregated by Week (e.g., 2026-02, 2026-03)
- Rows: Region and State
- Metrics: Current Week Metric 1, Last Week Metric 1
Goal
I want Last Week Metric 1 to grab the Current Week Metric 1 value from the previous week for the same Region and State.
For example:
- Washington’s “Last Week Metric 1” for 2026-03 should equal Washington’s “Current Week Metric 1” from 2026-02 (-8.01%)
- Instead, it’s currently grabbing values from different states within the same week(refer to the blue, yellow, green highlights for reference)
Current Calculations
Current Week Metric 1:
RAP - sumOver(
sum({Yes Response}),
[{Response Week}]
)
/
sumOver(
sum({Yes Response}) + sum({No Response}),
[{Response Week}]
)
Last Week Metric 1 (using LAG):
lag(RAP, [{Response Week} ASC], 1) -
lag(
sumOver(
sum({Yes Response}),
[{Response Week}]
)
/
sumOver(
sum({Yes Response}) + sum({No Response}),
[{Response Week}]
),
[{Response Week} ASC],
1
)
The Problem
The LAG function is grabbing values within the same week across different Region/State rows instead of grabbing values across weeks for the same Region/State.
Concern — Dynamic Partitioning
I understand that adding partition fields (Region, State) would work for this specific visual:
lag(
your_metric,
[{Response Week} ASC],
1,
[{Region}, {State}]
)
However, my dashboard has multiple visualizations with different row aggregations:
- Some visuals use Region + State
- Some use other dimension combinations
- Some are controlled by parameters that change the row aggregation dynamically
If I hardcode partition fields, I would need to create a customized calculation for each combination, which is not scalable or maintainable.
Questions
- Is there a way to create one universal LAG calculation that dynamically adapts to whatever dimensions are used in the row aggregation?
- Can QuickSight automatically detect the row-level grouping and apply LAG across columns (weeks) accordingly?
- Is there a workaround using
PRE_AGG,PRE_FILTER, or other level-aware functions?
Any suggestions or best practices for handling this scenario would be greatly appreciated!
Thanks in advance!
