cancel
Showing results for
Did you mean:

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a \$100 discount. Register Now

Helper III

## Calculate the average of Last 5 based on another column

Hi everyone!

I'm working with basketball data (basketball games) and I calculated the points scored by a player for the last game and the average excluding that game with measures. When new data is added, the calculation is refreshed, calculating again the values to be updated.

I would like to do the same but not for the last game, I would like to take the last 5 games and do the same calculation (that should be recalculated when entering new data). The logic should be the same (using Date column) to the calculations below (to interact with filters correctly). I tried with TopN in a measure changing the order but not working.

The calculations made (and working for the last game and all except last game):

Pts/G_Last =
VAR _date = CALCULATE( MAX(Hoja1[Date]), ALLSELECTED(Hoja1))
RETURN CALCULATE( [PtsScored/G], Hoja1[Date] = _date )

Pts/G_Avg =
VAR AvgGames = CALCULATE( MAX(Hoja1[Date]), ALLSELECTED(Hoja1))
RETURN CALCULATE( [PtsScored/G], Hoja1[Date] <> AvgGames)

1 ACCEPTED SOLUTION
Community Support

HI @sfernamer,

Perhaps you can take a look at the following link about similar requirement if its formula work on your report:

Solved: Calculate average on the last 10 dates/records in ... - Microsoft Power BI Community

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
2 REPLIES 2
Helper III

Thank you for your reply. I tested it on my pbix and, after editing a bit the formula, it worked. The final formula is the following one (I add the Date and Competition because I'm interested in recalculating depending on competition).

Last 10 Days =
VAR thisdate =
MAX ( Hoja1[Date] )
VAR last10days =
TOPN ( 10, FILTER ( ALLSELECTED( Hoja1[Date], Hoja1[Competition] ), Hoja1[Date] <= thisdate ), Hoja1[Date], DESC )
RETURN
CALCULATE([PtsScored/G], last10days)
Community Support

HI @sfernamer,

Perhaps you can take a look at the following link about similar requirement if its formula work on your report:

Solved: Calculate average on the last 10 dates/records in ... - Microsoft Power BI Community

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Announcements

#### Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

#### Power BI Monthly Update - February 2024

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

#### Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

#### Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors