Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
tomekm
Helper III
Helper III

Yearly rolling window over multiple years, depending on which Year is selected in slicer

Hello,

 

I'm trying to come up with a formula to calculate a 4 year rolling window, so that when the slicer selects 2025, it computes the rolling average (variant 1 divided by variant 2) over: 2025, 2024, 2023, 2022. And if slicer = 2024, it computes the average for 2024,2023,2022,2021, etc. etc. For 2025 it equals: 109/29.5=3.69. Thank you.

 

Here's my data:

 

variant 1variant 2Year
1112017
222018
52.22019
61.82020
97.22021
3232022
20.52023
67222024
842025

 

2 ACCEPTED SOLUTIONS
BITomS
Solution Supplier
Solution Supplier

RollingAverage =
VAR SelectedYear = SELECTEDVALUE('Table'[Year])
VAR StartYear = SelectedYear - 3

VAR SumVar1 =
CALCULATE(
[Variant 1 Measure],
FILTER(ALL('Table'), 'Table'[Year] >= StartYear && 'Table'[Year] <= SelectedYear)
)

VAR SumVar2 =
CALCULATE(
[Variant 2 Measure],
FILTER(ALL('Table'), 'Table'[Year] >= StartYear && 'Table'[Year] <= SelectedYear)
)

RETURN
IF(SumVar2 <> 0, SumVar1 / SumVar2, BLANK())

View solution in original post

BITomS
Solution Supplier
Solution Supplier

SELECTEDVALUE('Table'[Year]) only applies if you are selecting a single year i.e. from a slicer, which it sounded like you were. If this isn't the case try this:

RollingAverage =
VAR SelectedYear = MAX('Table'[Year]) -- Gets the year in the current row context
VAR StartYear = SelectedYear - 3

VAR SumVar1 =
CALCULATE(
[Variant 1 Measure], -- Uses the existing measure
'Table'[Year] >= StartYear && 'Table'[Year] <= SelectedYear
)

VAR SumVar2 =
CALCULATE(
[Variant 2 Measure], -- Uses the existing measure
'Table'[Year] >= StartYear && 'Table'[Year] <= SelectedYear
)

RETURN
IF(SumVar2 <> 0, SumVar1 / SumVar2, BLANK())

View solution in original post

5 REPLIES 5
BITomS
Solution Supplier
Solution Supplier

Hi @tomekm ,

 

RollingAverage =
VAR SelectedYear = SELECTEDVALUE('Table'[Year])
VAR StartYear = SelectedYear - 3
VAR SumVar1 = CALCULATE(
SUM('Table'[variant 1]),
'Table'[Year] >= StartYear && 'Table'[Year] <= SelectedYear
)
VAR SumVar2 = CALCULATE(
SUM('Table'[variant 2]),
'Table'[Year] >= StartYear && 'Table'[Year] <= SelectedYear
)
RETURN
IF(SumVar2 <> 0, SumVar1 / SumVar2, BLANK())

 

Hope this helps!

Thank you, but my variant 1 and variant 2 are actually measures, and not calculated columns, so this approach won't work I believe.

BITomS
Solution Supplier
Solution Supplier

RollingAverage =
VAR SelectedYear = SELECTEDVALUE('Table'[Year])
VAR StartYear = SelectedYear - 3

VAR SumVar1 =
CALCULATE(
[Variant 1 Measure],
FILTER(ALL('Table'), 'Table'[Year] >= StartYear && 'Table'[Year] <= SelectedYear)
)

VAR SumVar2 =
CALCULATE(
[Variant 2 Measure],
FILTER(ALL('Table'), 'Table'[Year] >= StartYear && 'Table'[Year] <= SelectedYear)
)

RETURN
IF(SumVar2 <> 0, SumVar1 / SumVar2, BLANK())

Thanks but when I add this measure to my table and charts, it is showing blank for all the yearly values; something is wrong. Not sure what..

BITomS
Solution Supplier
Solution Supplier

SELECTEDVALUE('Table'[Year]) only applies if you are selecting a single year i.e. from a slicer, which it sounded like you were. If this isn't the case try this:

RollingAverage =
VAR SelectedYear = MAX('Table'[Year]) -- Gets the year in the current row context
VAR StartYear = SelectedYear - 3

VAR SumVar1 =
CALCULATE(
[Variant 1 Measure], -- Uses the existing measure
'Table'[Year] >= StartYear && 'Table'[Year] <= SelectedYear
)

VAR SumVar2 =
CALCULATE(
[Variant 2 Measure], -- Uses the existing measure
'Table'[Year] >= StartYear && 'Table'[Year] <= SelectedYear
)

RETURN
IF(SumVar2 <> 0, SumVar1 / SumVar2, BLANK())

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors