# Power BI & DAX Tutorial: How to Calculate Rolling (Moving) Average on Non-Contiguous Dates

Rolling Average calculations are easy as long as you don’t have to worry about gaps in your dates for which you do not have any transactions. In this tutorial, I boldly take on this challenge and successfully complete the calculation that is able to find the last five days on which the selected product had sales, and then average those sales out to get the rolling (or moving) average.

The calculation is below:

Rolling 5 Last Days Sales Average =

var lastFiveDaysOfSales = CALCULATETABLE(

TOPN(5,

SUMMARIZE( Sales,

Sales[Date],

“Sales”,

SUM(Sales[Amount])

), Sales[Date], DESC

)

,

FILTER( ALL(‘Date'[Date]),

‘Date'[Date] <= SELECTEDVALUE(‘Date'[Date])

)

)

return DIVIDE(SUMX(lastFiveDaysOfSales, [Sales]), COUNTROWS(lastFiveDaysOfSales))

## 2 thoughts on “Power BI & DAX Tutorial: How to Calculate Rolling (Moving) Average on Non-Contiguous Dates”

1. Sergio Quintanar says:

This is very useful, but how would this be written for a moving average of 12 months?

2. Prakash says:

Hello, I am struggling to covert windowstdv Tableau code to power BI.

Basically looking to plot lower bound and upper bound using moving standard deviation.