Forum Discussion
Moving Average Using an Index Not Working
- 4 years ago
Hi Anonymous ,
According to your new sample, here's my solution.
Create two measures.
Avg of Price = CALCULATE ( AVERAGE ( 'Fruit Data'[Price] ), FILTER ( ALL ( 'Fruit Data' ), 'Fruit Data'[Purchase Date] = MAX ( 'Fruit Data'[Purchase Date] ) && 'Fruit Data'[Product] = MAX ( 'Fruit Data'[Product] ) && 'Fruit Data'[Delivery FY] = MAX ( 'Fruit Data'[Delivery FY] ) ) )Moving Avg = VAR Current_Date = MAX ( 'Fruit Data'[Purchase Date] ) VAR Number_Of_Days = SELECTEDVALUE ( 'Average Days'[Average Days] ) VAR Offset_Date = FILTER ( ALL ( 'Calendar'[Date] ), RANKX ( FILTER ( ALL ( 'Calendar' ), 'Calendar'[Date] < ( Current_Date ) ), 'Calendar'[Date], , DESC ) = Number_Of_Days ) VAR avrg = AVERAGEX ( FILTER ( ALL ( 'Fruit Data' ), 'Fruit Data'[Delivery FY] = MAX ( 'Fruit Data'[Delivery FY] ) && 'Fruit Data'[Product] = MAX ( 'Fruit Data'[Product] ) && 'Fruit Data'[Purchase Date] < Current_Date && 'Fruit Data'[Purchase Date] >= Offset_Date ), [Avg of Price] ) RETURN avrgGet the expected result.
I attach my sample below for reference.
Best Regards,
Community Support Team _ kalyjIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Please provide sanitized sample data that fully covers your issue. Paste the data into a table in your post or use one of the file services. Please show the expected outcome.
- Anonymous4 years agoNot applicable
Thanks for your help here is the raw data and an example of how I want the calculation to work:
In terms of how I'd like it to look in PowerBI, I would need to plot the data in both a table and on a graph where for each Purchase Data, and each Delivery Date there would be a dynamically calculated rolling average that could be 5,10,15,20 days (etc). I should also be able to aggregate up the delivery date to a delivery month or delivery year and the average should still calculate. In the example in the sheet the delivery period is Summer 2024.
As long as the average calculates correctly in a table I can figure out the rest from there though.
Fruit Dataset