Forum Discussion
Moving Average
- 10 years ago
Firstly you need a column of date with full date format. Then you can use calculated measure to get the expected result. Please refer to following steps.
- Create a calculated column for the date.
FullDate = DATE ( 2016, 'Session'[Month of the Year], 1 )
- Create a measure for 3 months moving average. You can change the number of months if you want.
Moving_Average_3_Months = CALCULATE ( AVERAGEX ( 'Session', 'Session'[Sessions] ), DATESINPERIOD ( 'Session'[FullDate], LASTDATE ( 'Session'[FullDate] ), -3, MONTH ) ) - Drag the Line Chart into your canvas as below.
- Create a calculated column for the date.
There are a lot of foundational things you should do.
1. Set up a calendar table. Read my article about that here http://exceleratorbi.com.au/power-pivot-calendar-tables/
2. Join the data table to the calendar table. Read about data shape here http://exceleratorbi.com.au/the-optimal-shape-for-power-pivot-data/
A moving average would then normally use a measure (not calc column) something like this
Total Sales rolling quarter :=
CALCULATE (
Sum(data[qty]),
FILTER (
ALL ( Calendar ),
Calendar[MonthID] >= MAX (calendar[MonthID]) -2
&& Calendar[MonthID] <= MAX ( Calendar[MonthID] )
)
)
There is a lot to learn if you want to understand it :-)
btw, your data looks like a running total, not a monthly qty, so you may need something different.
- LsasS10 years agoFrequent Visitor
Dear Matt, i though that since time intelligence functions implemented in DAX calendar tables are not necessary anymore
- MattAllington10 years agoCommunity Champion
I'm not sure what you mean. You definitely need a calendars table if you want to use the inbuilt time intelligence functions.
- LsasS10 years agoFrequent Visitor
Dear MattAllington I'm sorry to disapoint You, but You don't need it anymore:
"When you create a model in Power Pivot or Analysis Services Tabular, you can apply the setting “Mark as Date Table” choosing a column of Date data type as the date in the table."
https://www.sqlbi.com/articles/time-intelligence-in-power-bi-desktop/
I'll be grateful if You'll shortly give a prove that i'm not right.
Sincerely Yours, Max
- laltripti228 years agoFrequent Visitor
Hi, how can this be calculated if there are duplicate dates?
I have a DateTime column which has values for different timestamps on the same date. All the values are to considered while calculating the Average.
- AlexH6 years agoRegular Visitor
Hi all,
Very useful. However I'm still getting incorrect results in my model. For convenience I have renamed my data in accordance with the exmple in this thread.
I have multiple values per each date so my rolling average is too low, I need to calculated it based on the total for each date not based on the each value within the date.
Any suggestions
many thanks