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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

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
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.