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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Alternative to DATESINPERIOD for Rolling Average

hi all

 

Can someone suggest an alternative to the DATESINPERIOD function that allows me to go back and grab the last three months? Basically something to replace the below highlighted section.

 

My data is not on a day level and thus I can not use a date table and standard functions. I thus need a custom function.

 

nikilouwgmail_0-1675708466033.png

 

1 ACCEPTED SOLUTION
v-tangjie-msft
Community Support
Community Support

Hi @Anonymous ,

 

Please try this measure. 

Average Rolling 3 months2 =
VAR a =
    SUMMARIZE (
        TOPN (
            3,
            FILTER ( ALL ( DIM ), [YearMonth] <= MAX ( 'DIM'[YearMonth] ) ),
            [YearMonth], DESC
        ),
        [PeriodNoKey]
    )
RETURN
    IF (
        COUNTROWS ( a ) >= 3,
        CALCULATE ( SUM ( Facts[Amount] ) / 3, Facts[PeriodNoKey] IN a ),
        BLANK ()
    )

Then the result is as follows.

vtangjiemsft_0-1675819262972.png

Best Regards,

Neeko Tang

If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

View solution in original post

15 REPLIES 15
v-tangjie-msft
Community Support
Community Support

Hi @Anonymous ,

 

Please try this measure. 

Average Rolling 3 months2 =
VAR a =
    SUMMARIZE (
        TOPN (
            3,
            FILTER ( ALL ( DIM ), [YearMonth] <= MAX ( 'DIM'[YearMonth] ) ),
            [YearMonth], DESC
        ),
        [PeriodNoKey]
    )
RETURN
    IF (
        COUNTROWS ( a ) >= 3,
        CALCULATE ( SUM ( Facts[Amount] ) / 3, Facts[PeriodNoKey] IN a ),
        BLANK ()
    )

Then the result is as follows.

vtangjiemsft_0-1675819262972.png

Best Regards,

Neeko Tang

If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

Anonymous
Not applicable

Thank you very much @v-tangjie-msft 
I have some technical issues wrt my specific date/time table, but will try to sort that out on my own.

jonathanbergen
Frequent Visitor

have a look at this article, in particular the WINDOW function, it might work for your needs.

 

edit: forgot link!

Introducing window functions in DAX - SQLBI

Anonymous
Not applicable

hi @jonathanbergen 
the WINDOW function does not seem to work in tabular model?

nikilouwgmail_0-1675776891208.png

 

Padycosmos
Solution Sage
Solution Sage
Anonymous
Not applicable

hi @Padycosmos 

The WINDOW function does not seem to work in tabular model?

nikilouwgmail_1-1675776966532.png

 

Ashish_Mathur
Super User
Super User

Hi,

Share some data, explain the question and show the expected result.


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

hi @Ashish_Mathur 

 

Thank you for your response.

 

See below. I only have the amount values and also the reporting periods at month level. Thus it needs to shift in months and not days.


Facts:


nikilouwgmail_0-1675750659578.png

 

DIM:

nikilouwgmail_1-1675750689974.png


RESULT EXPECTED:

nikilouwgmail_2-1675750718286.png

 

 

Hi,

Share the first 2 tables in a format that can be pasted in an MS Excel file.


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

hi @Ashish_Mathur 

please see the response 4 of @v-tangjie-msft  - there is a pbix file with the data

I am looking for the solution as a measure.

Thank you for responding!

I misinterpreted your question.  Sorry but i will not be able to help here.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-tangjie-msft
Community Support
Community Support

Hi @Anonymous ,

 

Please try replacing it with the datesbetween() function:

DATESBETWEEN(Date[Date],
    DATEADD(LASTDATE(Date[Date]),-3,MONTH)
    ,LASTDATE(Date[Date])
)

 

 Please refer to the following document for more information.

Rolling Average 12 Months | Power BI Exchange (pbiusergroup.com)

DatesInPeriod vs DatesBetween; DAX Time Intelligence for Power BI - RADACAD

Rolling average last 3 months - Microsoft Power BI Community

 

If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. Thank you.

 

Best Regards,

Neeko Tang

If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

Anonymous
Not applicable

hi @v-tangjie-msft 

I dont have days in the model. Amounts and periods at month level only.

Hi @Anonymous ,

 

According to your description, here are my steps you can follow as a solution.

(1) My test data is the same as yours.

(2) We can create a calculated column.

Average Rolling 3 months = var a=SUMMARIZE(TOPN(3,FILTER(DIM,[YearMonth]<=EARLIER(DIM[YearMonth])),[YearMonth],DESC),[PeriodNoKey])
return IF(COUNTROWS(a)>=3,CALCULATE(SUM(Facts[Amount])/3,Facts[PeriodNoKey] in a),BLANK())

(3) Then the result is as follows.

vtangjiemsft_0-1675752698608.png

Best Regards,

Neeko Tang

If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

Anonymous
Not applicable

hi @v-tangjie-msft 

Many thanks for this solution.

How do we convert this to a measure? I fear this calculated column might be costly in my very large fact table..

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.