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

Rolling Average for past 6 months

Dear All,

 

I have this simple scenario where i need to calculate a rolling past 6 months Average for a given data set, below details.

 

Data Set Example:

The Year_Month column, in data source only had year and month, and i converted it to Data Type Date/Time n Power BI, hence shows Date as 1st date of that month.

I don't want to go the date level in the data source as it will be millions of rows of data.

 

Calendar Year_MonthMonthQueryDistinct User
1/12/201912DSA10
1/01/20201DSA1
1/02/20202DSA20
1/03/20203DSA10
1/04/20204DSA30
1/05/20205DSA40
1/12/201912Sales23
1/01/20201Sales21
1/02/20202Sales20
1/03/20203Sales9
1/04/20204Sales8
1/05/20205Sales10
1/12/201912Stock33
1/01/20201Stock41
1/02/20202Stock39
1/03/20203Stock21
1/04/20204Stock20
1/05/20205Stock11

 


DAX Formula used:

I have created a Date table in Power BI and referring that in the DATESINPERIOD function below.

 

DIVIDE(CALCULATE (
SUM(Rolling_Average_Example[Distinct User]),
DATESINPERIOD ( 'Date Table'[Date], MAX(Rolling_Average_Example[Calendar Year_Month]) , -6, month )
),6,0)

 

Expected Result:

For month of May, the rolling 6 month Average should be Sum of users in past 6 months/6 = 306/6 = 51.
But the DAX above shows 10, which is SUM of users in MAY divided by 6 (61/6=10), which is wrong.

 

If you can guide me where i am wrong and how to fix this in given scenario.

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

Hi,

 

Please create a What If parameter first.

Then try this measure:

Measure = 
AVERAGEX (
    DISTINCT ( 'Table'[Calendar Year_Month] ),
    CALCULATE (
        IF (
            YEAR ( MAX ( 'Table'[Calendar Year_Month] ) ) = YEAR ( TODAY () )
                && MONTH ( MAX ( 'Table'[Calendar Year_Month] ) ) = MONTH ( TODAY () ),
            0,
            IF (
                MONTH ( TODAY () ) - Previous[Previous Value] <= 0,
                CALCULATE (
                    SUM ( 'Table'[Distinct User] ),
                    FILTER (
                        'Table',
                        'Table'[Calendar Year_Month] < DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ), 1 )
                            && 'Table'[Calendar Year_Month]
                                >= DATE ( YEAR ( TODAY () ) - 1, 12
                                    - ABS ( MONTH ( TODAY () ) - Previous[Previous Value] ), 1 )
                    )
                ),
                CALCULATE (
                    SUM ( 'Table'[Distinct User] ),
                    FILTER (
                        'Table',
                        'Table'[Calendar Year_Month] < DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ), 1 )
                            && 'Table'[Calendar Year_Month]
                                >= DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ) - Previous[Previous Value], 1 )
                    )
                )
            )
        )
    )
)

When select one value in slicer, the result shows:

15.PNG

See my attached pbix file.

 

Best Regards,

Giotto

View solution in original post

5 REPLIES 5
v-gizhi-msft
Community Support
Community Support

Hi,

 

Please create a What If parameter first.

Then try this measure:

Measure = 
AVERAGEX (
    DISTINCT ( 'Table'[Calendar Year_Month] ),
    CALCULATE (
        IF (
            YEAR ( MAX ( 'Table'[Calendar Year_Month] ) ) = YEAR ( TODAY () )
                && MONTH ( MAX ( 'Table'[Calendar Year_Month] ) ) = MONTH ( TODAY () ),
            0,
            IF (
                MONTH ( TODAY () ) - Previous[Previous Value] <= 0,
                CALCULATE (
                    SUM ( 'Table'[Distinct User] ),
                    FILTER (
                        'Table',
                        'Table'[Calendar Year_Month] < DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ), 1 )
                            && 'Table'[Calendar Year_Month]
                                >= DATE ( YEAR ( TODAY () ) - 1, 12
                                    - ABS ( MONTH ( TODAY () ) - Previous[Previous Value] ), 1 )
                    )
                ),
                CALCULATE (
                    SUM ( 'Table'[Distinct User] ),
                    FILTER (
                        'Table',
                        'Table'[Calendar Year_Month] < DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ), 1 )
                            && 'Table'[Calendar Year_Month]
                                >= DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ) - Previous[Previous Value], 1 )
                    )
                )
            )
        )
    )
)

When select one value in slicer, the result shows:

15.PNG

See my attached pbix file.

 

Best Regards,

Giotto

amitchandak
Super User
Super User

@Anonymous , please find the attached pbix after the signature. I am getting 50.

Anonymous
Not applicable

@amitchandak  Thanks for your response, the sample data i posted is in format dd/mm/yyyy, so it is actually user counts for different months of the year 2019 and 2020, sorry i should have been clear with format.

 

Attached screenshot comparing how the data looks on my side and what i could see in the pbix file shared by you (left hand side is data on my side)

 

Rolling Average_Comparison 

@Anonymous , now I am getting 60. What should not be there in 6 months?

File attached after signature

Anonymous
Not applicable

@amitchandak , Based on the dataset i shared, i want to calculate the rolling 6 months average, i took May as example, in initial post what is the expected value of rolling 6 months average as compared to what is happening now.

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.