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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
StephaneMA
Frequent Visitor

Moving average based on prior N rows

I am trying to calculate a moving average based on the prior N rows of a table. Something that would match the table below.

 
 

Rolling avg.png

 

I have tried the following but it does not provide the desired result:

 

MOVING Average submissions by N week =
 
AVERAGEX(
                FILTER(
                            TOPN(
                                        6,'Normalized Calendar', [Normalized Week],DESC), ('Normalized Calendar'[Normalized Week]
                                      )
                           ),
                  'Opportunity'[Opportunity Count])
 
Any help pointing me in the right direction would be greatly appreciated.
 
Thank you!
2 ACCEPTED SOLUTIONS

The comma goes before the All().  Try doing just All('Normalized Calendar') instead (remove the column reference).

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

@StephaneMA colum type is whole number/decimal. Can you copy the table which i shared above and create the same measure (not calculated column) without any  edits. Also share the snapshot.?


Regards,
Nandu Krishna

View solution in original post

12 REPLIES 12
nandukrishnavs
Super User
Super User

@StephaneMA 

 

Try this DAX measure.

Moving Avg =
VAR n = 5
VAR current =
    SELECTEDVALUE ( Table[weeknumber] )
VAR avg =
    CALCULATE (
        AVERAGE ( Table[Subs] ),
        FILTER (
            ALL ( Table[weeknumber] ),
            Table[weeknumber] <= current
                && Table[weeknumber] >= current - n
        )
    )
RETURN
    avg

 



Did I answer your question? Mark my post as a solution!
Appreciate with a kudos
🙂


Regards,
Nandu Krishna

Thank you nandukrishnavs,

 

I appreciate the suggestion. I get exactly the same result than I did with Pat's suggestion above.

 

error 3.png

@StephaneMA 

 

WeeknumberSubs
1282
2508
3540
4518
5717
6599
7622
8416
9603
10622
11730
12617
13618

 

 

Moving Avg = 
VAR n = 5
VAR _selectedweekno =
    SELECTEDVALUE ( 'Table'[Weeknumber] )
VAR result =
    CALCULATE (
        AVERAGE ( 'Table'[Subs] ),
        FILTER (
            ALL ( 'Table'[Weeknumber] ),
            'Table'[Weeknumber] <= _selectedweekno
                && 'Table'[Weeknumber] >= _selectedweekno - n
        )
    )
RETURN
   IF(_selectedweekno>=n,result,BLANK())

 

Capture.JPG



Did I answer your question? Mark my post as a solution!
Appreciate with a kudos
🙂


Regards,
Nandu Krishna

I appreciate the help but I do not get the same result at all. I created a new table with the same data to replicate your formula but I get bery different results....

 

error 4.png

 

I am not sure at this point what I am doing wrong to get this result.

@StephaneMA colum type is whole number/decimal. Can you copy the table which i shared above and create the same measure (not calculated column) without any  edits. Also share the snapshot.?


Regards,
Nandu Krishna

That did it.......!

 

Thank you nandukrishnavs.

 

There must be something wromng with the original dataset. I will fix that issue now that I have a working average working!

 

I really appreciate your assistance.

mahoneypat
Employee
Employee

Prior 5 Weeks =
VAR currentweeknumber =
SELECTEDVALUE ( Table[Normalized Week Number] )
RETURN
CALCULATE (
AVERAGE ( Table[Subs])
ALL ( Table[Normalized Week Number] ),
Table[Normalized Week Number] <= currentweeknumber,
Table[Normalized Week Number] >= currentweeknumber - 5
)

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Thank you Pat,

 

I appreciate the quick reply.

 

When I enter the code as sent, I get the following:

 

error.png

If I add a coma before the ALL function, the error disappears but the result is not what I expect (see below)

 

Subs.png

 

Can you think of what is creating the issue?

 

Again, I really appreciate your assistance.

 

SL

The comma goes before the All().  Try doing just All('Normalized Calendar') instead (remove the column reference).

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Pat,

 

thank you for the help......you had it all along....there must be something wrong with the original dataset which affected the result.

 

I appreciate your assistance!

 

Thank you again

Sorry Pat, 

 

Gave the same result

 

Prior 5 Weeks =
VAR currentweeknumber =
SELECTEDVALUE ( 'Normalized Calendar'[Normalized Week] )
RETURN
CALCULATE (
AVERAGE (Opportunity[SL@ Opportunity Count]), ALL ('Normalized Calendar'),
'Normalized Calendar'[Normalized Week] <= currentweeknumber,
'Normalized Calendar'[Normalized Week] >= currentweeknumber - 5
)
Output 2.png
Best
 
SL

Well that's unexpected (at least to me).  Numbers look right beyond Week 5.  Are there Opportunity data raws before Normalized Week 1?  I will take a long again tomorrow, if someone else doesn't solve it first.

 

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors