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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Anonymous
Not applicable

Help with "dynamic" moving average

Hi,

How can i calculate the moving average for the last 3 (group by id) events, dynamically in a calculated column in Tabular?

(The final objective is to create a new column Tendency and seeing if Measure > Moving Average)

 

The normal cases are for id's like 1 and 2; but for cases like id 3 and 4 admitting that there are no entries, i would calculate to calculate the Moving Average with the entries that i have (in this case, 0 entries for id 4, and 2 entries for id 3)

image.png

 

Thanks!

Regards

 

1 ACCEPTED SOLUTION

Hi @Anonymous,

 

Try this formula and the demo in the attachment, please. 

Column =
VAR currentDate2 = Table1[Date2]
VAR ids =
    CALCULATE (
        COUNT ( Table1[id] ),
        FILTER ( ALLEXCEPT ( Table1, Table1[id] ), Table1[Date2] <= currentDate2 )
    )
RETURN
    IF (
        ids < 4,
        BLANK (),
        CALCULATE (
            AVERAGEX (
                FILTER (
                    SUMMARIZE (
                        'Table1',
                        Table1[id],
                        Table1[Date2],
                        Table1[Value],
                        "ids2", CALCULATE (
                            COUNT ( Table1[id] ),
                            FILTER (
                                ALLEXCEPT ( Table1, Table1[id] ),
                                Table1[Date2] <= EARLIER ( Table1[Date2] )
                            )
                        )
                    ),
                    [ids2] < ids
                        && [ids2]
                        >= ids - 3
                ),
                [Value]
            ),
            ALLEXCEPT ( Table1, Table1[id] )
        )
    )

Help_with_dynamic_moving_average

 

Best Regards,

Dale

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

View solution in original post

6 REPLIES 6
anil
Helper III
Helper III

Hi Ricardo,

             I have worked on the similar requirement, please follow the link.

https://community.powerbi.com/t5/Desktop/Moving-Average-Last-3-days-per-category-ID/td-p/465310

Anonymous
Not applicable

Hi @anil, thank you very much for your help, unfortunately i now know that my source is a snapshot table, so i need to find a way to remove adjecent values for measures...

 

See [yellow] id 1 in particular:

 

x.png

Is there any way to do this?

 

regards

 

 

 

 

Hi @Anonymous,

 

Would all the adjacent values be removed? If so, should the 3 in blue be (3 + 0 + 0) / 3?

 

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Hi @anil and @v-jiascu-msft,

 

a small change on the original table... what i'm searching for is this:

 

xxxxx.PNG


Before 3 entries (for each id), no result (as we are calculating Mov Average "3 entries" before); after that, calculate Moving Average always for the 3 previous dates, for each id.
 

Thanks!

Regards

Hi @Anonymous,

 

Try this formula and the demo in the attachment, please. 

Column =
VAR currentDate2 = Table1[Date2]
VAR ids =
    CALCULATE (
        COUNT ( Table1[id] ),
        FILTER ( ALLEXCEPT ( Table1, Table1[id] ), Table1[Date2] <= currentDate2 )
    )
RETURN
    IF (
        ids < 4,
        BLANK (),
        CALCULATE (
            AVERAGEX (
                FILTER (
                    SUMMARIZE (
                        'Table1',
                        Table1[id],
                        Table1[Date2],
                        Table1[Value],
                        "ids2", CALCULATE (
                            COUNT ( Table1[id] ),
                            FILTER (
                                ALLEXCEPT ( Table1, Table1[id] ),
                                Table1[Date2] <= EARLIER ( Table1[Date2] )
                            )
                        )
                    ),
                    [ids2] < ids
                        && [ids2]
                        >= ids - 3
                ),
                [Value]
            ),
            ALLEXCEPT ( Table1, Table1[id] )
        )
    )

Help_with_dynamic_moving_average

 

Best Regards,

Dale

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

Hi,

 

Why do you need the Moving Average in a calculated column?  Why not in a measure?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors