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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
Anonymous
Not applicable

Weighted Moving Average

Hi all,

 

I have been trying to calculate an exponentially weighted moving average for a 7 day period and a 28 day, however I am struggling to pick out the row context in my DAX. 

I have listed my measures in the image attached.

I need to be able to identify the value for total distance within my DAX.

 

I appreciate any help on this.

Thanks in advance.

 

Sean

 

EWMA.jpg

4 REPLIES 4
v-zhenbw-msft
Community Support
Community Support

Hi @Anonymous ,

 

We can use the following steps to meet your requirement.

 

1. Create a Rolling Average 7 day column.

 

Rolling Average 7 day = 
var current_ = 'Table'[Date]
var last_7 = current_ - 7
return
IF(
    current_-MIN('Table'[Date])>=6,
CALCULATE(AVERAGE('Table'[Value]),FILTER(ALLSELECTED('Table'),'Table'[Date]<=current_&&'Table'[Date]>last_7)),BLANK())

 

weight 1.jpg

 

2. Then create a EWMA 7 day column, we can get the result.

 

EWMA 7 day column = 
VAR currentDate = 'Table'[Date]
VAR minDate =
    MIN ( 'Table'[Date] ) + 6
VAR result =
    SUMX (
        FILTER ( 'Table', 'Table'[Date] > minDate && 'Table'[Date] < currentDate ),
        [7 days weighting] * [Value]
            * POWER ( 1-[7 days weighting], DATEDIFF ( [Date], currentDate, DAY ) )
    )
        + CALCULATE (
            SUM ( 'Table'[Rolling Average 7 day] ),
            'Table',
            'Table'[Date] = minDate
        )
            * POWER ( 1-[7 days weighting], DATEDIFF ( minDate, currentDate, DAY ) ) + [Value] * [7 days weighting]
RETURN
IF (
        currentDate < minDate,
        BLANK (),
        IF ( currentDate = minDate, [Rolling Average 7 day], result )
)

 

weight 2.jpg

 

3. Or we also can create a measure to get the same result.

 

EWMA 7 day Measure = 
VAR currentDate = MAX('Table'[Date])
VAR minDate =
    CALCULATE(MIN ( 'Table'[Date] ),ALLSELECTED('Table')) + 6
VAR result =
    SUMX (
        FILTER ( ALLSELECTED('Table'), 'Table'[Date] > minDate && 'Table'[Date] < currentDate ),
        [7 days weighting] * 'Table'[Value]
            * POWER ( 1-[7 days weighting], DATEDIFF ( [Date], currentDate, DAY ) )
    )
        + CALCULATE (
            SUM ( 'Table'[Rolling Average 7 day] ),
            ALLSELECTED('Table'),
            'Table'[Date] = minDate
        )
            * POWER ( 1-[7 days weighting], DATEDIFF ( minDate, currentDate, DAY ) ) + CALCULATE(SUM('Table'[Value])) * [7 days weighting]
RETURN
IF (
        currentDate < minDate,
        BLANK (),
        IF ( currentDate = minDate, CALCULATE(SUM([Rolling Average 7 day])), result )
)

 

weight 3.jpg

 

If it doesn’t meet your requirement, could you please show the exact expected result based on the table that you have shared?

BTW, pbix as attached.

 

Best regards,

 

Community Support Team _ zhenbw

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

@v-zhenbw-msft Thanks so much for the reply.

 

Firstly on reviewing my example data, the results I am expecting were incorrect, so apologies.

Please see linked below;

Expected results & pbix 

 

I have already created measures for a 7 day rolling average. I have used your measure provided and manipulated the DAX to include the measure rather than column but I am not getting the expected result.

 

Thanks for your help

Sean

 

 

 

Greg_Deckler
Community Champion
Community Champion

Can you post sample data in text so that we can experiment on it. I'm also not clear on what you are expecting as a result. Please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

@Greg_Deckler 

 

I have provided a sample of my data table 'GPS Data Unpivoted'.

This would have multiple players across multiple dates and metrics.

 
Below is the exponentially weighted average calculation that I am trying to recreate.
EWMA(t) = a * x(t) + (1-a) * EWMA(t-1)

Where

  • EWMA(t) =  moving average at time t
  • a = degree of mixing parameter value between 0 and 1
  • x(t) = value of signal x at time t

I am trying to create this calculation over a 7 day period and a 28 day period.

 

Using the example of a 7 day EWMA;

a = 2/(7+1)

x(t) = would be the value for each day (including days where players have no data)

EWMA(t-1) = the EWMA rolling average for previous day

 

Within the sample data I have created an EWMA 7 day column which is my expceted results.

 

I hope that helps a bit more?

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

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

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.