cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
New Member

Average over Last 10 Records with Filters

I'm working with a set of basketball data with the following fields:
Name, PlayerID, Day(This is the date the game was played), FantasyPoints, Games(1 or 0 depending if they played or not)

I'm trying to show at any point in time the average number of points that a player has scored over their last 10 games that they played in. (Excluding games they did not play). 

I've tried a couple of other DAX solutions from this forum, and I found one that gets somewhat close, but it's off by decimal points.

AvgL10DKPoints =
VAR thisdate =
MAX(NBA_Player_Stats[Day])
 
VAR Last10Days =
TOPN(10, FILTER(ALL(NBA_Player_Stats[Day]), NBA_Player_Stats[Day] <= thisdate), NBA_Player_Stats[Day], DESC)
 
RETURN
CALCULATE(AVERAGE(NBA_Player_Stats[FantasyPointsDraftKings]), Last10Days)
1 ACCEPTED SOLUTION
Super User IV
Super User IV

Here is a measure that uses only the last 10 games in which they played.

 

Avg. Points Last 10 Games Played =
VAR thisdate =
    MAX ( NBA_Player_Stats[Day] )
VAR Last10Days =
    TOPN (
        10,
        CALCULATETABLE (
            DISTINCT ( NBA_Player_Stats[Day] ),
            ALL ( NBA_Player_Stats[Day] ),
            NBA_Player_Stats[Day] <= thisdate,
            NBA_Player_Stats[Games] = 1
        ),
        NBA_Player_Stats[Day], DESC
    )
RETURN
    CALCULATE (
        AVERAGE ( NBA_Player_Stats[FantasyPointsDraftKings] ),
        Last10Days
    )

 
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

6 REPLIES 6
Super User II
Super User II

@ZonkyDFS guess your current measure does not exclude the date when one player not played. ie games=0

Super User IV
Super User IV

Here is a measure that uses only the last 10 games in which they played.

 

Avg. Points Last 10 Games Played =
VAR thisdate =
    MAX ( NBA_Player_Stats[Day] )
VAR Last10Days =
    TOPN (
        10,
        CALCULATETABLE (
            DISTINCT ( NBA_Player_Stats[Day] ),
            ALL ( NBA_Player_Stats[Day] ),
            NBA_Player_Stats[Day] <= thisdate,
            NBA_Player_Stats[Games] = 1
        ),
        NBA_Player_Stats[Day], DESC
    )
RETURN
    CALCULATE (
        AVERAGE ( NBA_Player_Stats[FantasyPointsDraftKings] ),
        Last10Days
    )

 
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

New Member

@mahoneypat PMd you the file. 

Super User IV
Super User IV

Can you share your pbix?  When I try your measure with the data you've shown (I OCR'ed it), I get the expected 61.470 for the first one.  I suspect you are sometimes including dates not shown.  You mentioned the column with 1 or 0 to indicate if they played that day or not.  My guess is you are unintentionally including some 0 days (and effectively averaging only 9 or 8 games).  You probably need to update the measure to exclude dates with 0 in the Game column.  For example, when I changed the TOPN to 8, I get the same number you are seeing, so there are two extra dates without scores in the first one (they don't add in as zeros to the average though).

 

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


I haven't had to share a .PBIX here before, and it doesn't look like I can just drag and drop it in or attach it. I have a filter added to "all pages" that sets a condition of 1 for the Games Played field, but wouldn't the ALL function ignore that?

 

In the above DAX, how would I modify that TOPN function to include the condition where Games = 1?

New Member

@mahoneypat  I'm actually referencing the solution you offered here in my post if you could potentially offer any help. 
In the screenshot below from Power BI - if I had the first 10 records here for Lebron James, I get a total of 614.75 points, divided by 10 records = 61.475 average over the last 10. But the measure is < 1 point off at 62.19 points. 

Any idea why that may be?

ZonkyDFS_0-1606607965512.png

 

Helpful resources

Announcements
secondImage

Congratulations!

We are excited to announce the Power BI Super Users!

Microsoft Ignite

Microsoft Ignite with Arun Ulag

Featured Session: Drive Data Culture with Power BI- Vision, Strategy & Roadmap. Register here https://myignite.microsoft.com #MSIgnite #PowerPlatform #Power BI ​

Microsoft Ignite

Microsoft Ignite

Join digitally, March 2–4, 2021 to explore new tech that's ready to implement. Experience the keynote in mixed reality through AltspaceVR!

secondImage

The largest Power BI virtual conference

100+ sessions, 100+ speakers, Product managers, MVPs, and experts. All about Power BI. Attend online or watch the recordings.