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
Krish89
Helper II
Helper II

12 months rolling average

Hi,

I have a sales table with the last 5years data and the table look like below.

I would like to calculate the rolling average of last 12 months based on the selected date from date table in the slicer and PeriodTypeId = 2.

For Example: If I select 2020-05 in the slicer, I should get the rolling average of last 12 months (2020-04 to 2019-05)

The rolling average should be calculated only for the PeriodTypeId=2

Krish89_0-1624357257541.png

 

Thanks in advance!

 

 

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

Hi, @Krish89 

 

According to your description, I think you need to create a single list and use the date column as slicer. Then create a measure to display the results.

Like this:

 

Table = CALCULATETABLE(DISTINCT(Table1[Date]),Table1[PeriodTypeld]=2)
Rolling Average = 
VAR last =
    DATE ( YEAR ( SELECTEDVALUE ( 'Table'[Date] ) ) - 1, MONTH ( SELECTEDVALUE ( 'Table'[Date] ) ), DAY ( SELECTEDVALUE ( 'Table'[Date] ) ) )
VAR s =
    SUMX (
        FILTER (
            ALL ( Table1 ),
            [PeriodTypeld] = 2
                && [Date] < SELECTEDVALUE ( 'Table'[Date] )
                && [Date] >= last
        ),
        [Amount]
    )
RETURN
    DIVIDE ( s, 12 )

 

vjaneygmsft_0-1624526710105.png

My sample file is below. Hope it helps.

 

Best Regards

Janey Guo

 

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

2 REPLIES 2
v-janeyg-msft
Community Support
Community Support

Hi, @Krish89 

 

According to your description, I think you need to create a single list and use the date column as slicer. Then create a measure to display the results.

Like this:

 

Table = CALCULATETABLE(DISTINCT(Table1[Date]),Table1[PeriodTypeld]=2)
Rolling Average = 
VAR last =
    DATE ( YEAR ( SELECTEDVALUE ( 'Table'[Date] ) ) - 1, MONTH ( SELECTEDVALUE ( 'Table'[Date] ) ), DAY ( SELECTEDVALUE ( 'Table'[Date] ) ) )
VAR s =
    SUMX (
        FILTER (
            ALL ( Table1 ),
            [PeriodTypeld] = 2
                && [Date] < SELECTEDVALUE ( 'Table'[Date] )
                && [Date] >= last
        ),
        [Amount]
    )
RETURN
    DIVIDE ( s, 12 )

 

vjaneygmsft_0-1624526710105.png

My sample file is below. Hope it helps.

 

Best Regards

Janey Guo

 

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

selimovd
Super User
Super User

Hey @Krish89 ,

 

try the following measure:

Rolling Average =
CALCULATE(
    AVERAGE( myTable[Amount] ),
    myTable[PeriodTypeId] = 2,
    DATESINPERIOD(
        dateTable[Date],
        MAX( dateTable[Date] ),
        -1,
        YEAR
    )
)

 

Be aware that you need a proper date table in order for the time intelligence functions to work.

Take a look on how to create a date table:

https://softcrylic.com/blogs/power-bi-for-beginners-how-to-create-a-date-table-in-power-bi/

 

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
 
Best regards
Denis
 

 

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.