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
ZonkyDFS
Frequent Visitor

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
mahoneypat
Employee
Employee

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
wdx223_Daniel
Super User
Super User

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

mahoneypat
Employee
Employee

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


ZonkyDFS
Frequent Visitor

@mahoneypat PMd you the file. 

mahoneypat
Employee
Employee

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?

ZonkyDFS
Frequent Visitor

@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
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