Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
HollyPludeTFA
Frequent Visitor

DAX Code for Previous Year Rolling Averages

I have read numerous posts on issues surround Rolling Averages, but I have not found one that answers my specific problem.

 

I have a dashboard for the 2022 Fiscal Year. I need to write a formula that creates a 4 month (and then a separate formula for a 12 month) rolling average, but for the SPLY in 2022. I have used PREVIOUSYEAR, DATESBETWEEN, SAMEPERIODLASTYEAR all to no avail. Either these are the wrong expressions or I am really not understanding how to write this code.

 

This is my current code. It works exactly as it should for the current year, but even when utilizing date filters on the page, correct results are not returned.

 

Starts units R4M =
VAR NumOfMonths = 4
VAR LastCurrentDate =
    MAX ( Dates[Date] )
VAR Period =
    DATESINPERIOD ( Dates[Date], LastCurrentDate, - NumOfMonths, MONTH )
VAR Result =
    CALCULATE (
        AVERAGEX (
            VALUES ( Dates[Month] ),
            'Key Measures'[Frame]
        ),
        Period
    )
VAR FirstDateInPeriod = MINX ( Period, Dates[Date])
VAR LastDateWithSales = MAX ( SCHEDULE_SACTIVITY[SCH_START_DATE])
RETURN
    IF ( FirstDateInPeriod <= LastDateWithSales, Result )
 
This is the code for the 'Key Measures' [Frame] portion of the code:
 
Frame =
CALCULATE(COUNT(SCHEDULE_SACTIVITY[SACTIVITY_ID]),
USERELATIONSHIP(Dates[Date],SCHEDULE_SACTIVITY[ACTUAL_START_DATE]),
SCHEDULE_SACTIVITY[SACTIVITY_ID] = 82
)
 
Thank you in advance for any assistance you might be able to lend me.
10 REPLIES 10
HollyPludeTFA
Frequent Visitor

SS Sales Summary SANDBOX dummy.pbix

 

I moved the PBIX from Google Drive to OneDrive. I tested the link and it does work now.

 

Holly

Hi,

Share a small easy to understand dataset and show the expected result in a simple table format on that dummy dataset.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Hi @HollyPludeTFA ,

Can you please share a pbix or some dummy data that keep the raw data structure with expected results? It should help us clarify your scenario and test to coding formula.

How to Get Your Question Answered Quickly  

Regards,

Xiaoxin Sheng

Xiaoxin,

 

Thank you - I am scrubbing a subset of data and will upload it to a pbix and share.

 

Holly

 

Access Denied message.  Share data in a format that can be pasted in an MS Excel file and show the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Is this what you are looking for?

 

Start DateFrame CountStarts Avg R4MStarts Avg R12M
01/01/2210.000.00
02/01/2240.000.00
03/01/2210.000.00
04/01/2211.750.00
05/01/2263.000.00
06/01/2212.250.00
07/01/2273.750.00
08/01/2244.500.00
09/01/2213.250.00
10/01/2213.250.00
11/01/2222.002.64
12/01/2231.752.82
01/01/2363.003.00
02/01/2323.253.09
03/01/2333.503.27
04/01/2323.252.91
05/01/2312.002.91
06/01/2311.752.36
07/01/2331.752.27
Anonymous
Not applicable

Hi @HollyPludeTFA ,

Did Ashish_Mathur 's suggestions help with your scenario? If that is the case, you can consider Kudo or accept his suggestions to help others who faced similar requirements.

If that also doesn't help, please share more detailed information to help us clarify your scenario and test.

How to Get Your Question Answered Quickly 

Regards,

Xiaoxin Sheng

Unfortunately the solution did not work. At this time, my client decided to simply remove the graphic from the dashboard.

 

Thank you! I appreciate the attention given to this issue.

 

Holly

Hi,

Please find attached the PBI file.  DIY for 12 months based on my measure for 4 monts.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.