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
SivaMani
Resident Rockstar
Resident Rockstar

Rolling 12 month average of Activity Count in Direct Query mode

Hello,

I'm trying to calculate rolling 12 month average of Activity count. Here is the sample data,

 

Act #Date
A101-01-2019
A201-01-2019
A301-01-2019
A401-01-2019
A501-01-2019
A601-01-2019
A701-01-2019
A801-01-2019
A901-01-2019
A1001-01-2019
A1101-01-2019
A1201-01-2019
A1301-01-2019
A1401-01-2019
A1501-01-2019
A1601-02-2019
A1702-02-2019
A1803-02-2019
A1904-02-2019
A2005-02-2019
A2106-02-2019
A2207-02-2019
A2308-02-2019
A2409-02-2019
A2510-02-2019

 

The output that I'm looking for is,

MonthRolling 12M Avg
Jan-1915
Feb-1912.5

 

Please assist me.

1 ACCEPTED SOLUTION

Hello,

 

It works. I done the below change in the calculation,

 

 VAR __LAST_DATE = MAX(DM_DAY_Activity_D[CLDR_DATE])
    VAR __DATE_PERIOD =
        FILTER(
            DM_DAY_Activity_D, DM_DAY_Activity_D[CLDR_DATE] >=
            __LAST_DATE-365,
            DM_DAY_Activity_D[CLDR_DATE] <=__LAST_DATE
        )

View solution in original post

8 REPLIES 8
v-xicai
Community Support
Community Support

Hi @SivaMani ,

 

you can try to create measures like DAX below.

 

Period End = LASTDATE(Table1[Date])

 

Period Start= FIRSTDATE( DATESINPERIOD(Table1[Date], [Period End], -12, MONTH))

 

Rolling 12M Avg = CALCULATE(AVERAGE(Table1[Act #]),DATESBETWEEN ( Table1[Date], [Period Start], [Period End] ))

 

Best Regards,

Amy

 

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

 

@v-xicai ,

 

Thanks for your response.

 

I need a measure to calculate rolling 12 Months averge of Actvity Count.

Hi @SivaMani ,

 

Then you can change the last measure in DAX below.

 

Rolling 12M Avg = CALCULATE(AVERAGE(Table1[Actvity Count]),DATESBETWEEN ( Table1[Date], [Period Start], [Period End] ))

 

Best Regards,

Amy

 

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

Averge DAX fuction only takes a column as a reference. We can't use Average(CountMeasure), right?

Hi @SivaMani ,

 

Yes, you are right, while AVERAGEX(Table1,[Actvity Count]) will be ok.

 You can learn more: https://docs.microsoft.com/zh-cn/dax/averagex-function-dax

 

Best regards

Amy Cai

 

 

@v-xicai ,

 

Here is the measure that I tried,

Count of Activity # rolling average =
    VAR __LAST_DATE = ENDOFMONTH(DM_DAY_Activity_D[CLDR_DATE])
    VAR __DATE_PERIOD =
        DATESBETWEEN(
            DM_DAY_Activity_D[CLDR_DATE],
            STARTOFMONTH(DATEADD(__LAST_DATE, -12, MONTH)),
            __LAST_DATE
        )
VAR __RESULT = AVERAGEX(
            CALCULATETABLE(
                SUMMARIZE(
                    VALUES(DM_DAY_Activity_D),
                    DM_DAY_Activity_D[CAL_YEAR],
                    DM_DAY_Activity_D[CAL_QTR],
                    DM_DAY_Activity_D[CAL_MONTH_ID],
                    DM_DAY_Activity_D[CAL_MONTH_DESC]
                ),
                __DATE_PERIOD
            ),
            CALCULATE(
                COUNTA(Activity[Activity Number]),
                ALL(DM_DAY_Activity_D[DAY_NUM_CAL_MONTH])
            )
        )
    RETURN
__RESULT
 
 
I'm getting this error - Error Message:
MdxScript(Model) (9, 3) Calculation error in measure 'Activity'[Count of Activity # rolling average]: An invalid numeric representation of a date value was encountered.

Hello,

 

It works. I done the below change in the calculation,

 

 VAR __LAST_DATE = MAX(DM_DAY_Activity_D[CLDR_DATE])
    VAR __DATE_PERIOD =
        FILTER(
            DM_DAY_Activity_D, DM_DAY_Activity_D[CLDR_DATE] >=
            __LAST_DATE-365,
            DM_DAY_Activity_D[CLDR_DATE] <=__LAST_DATE
        )

Hi SivaMani,

 

Do you perhaps have an example of this solution. If not I would just like to know on your below code, is this column DM_DAY_Activity_D[CLDR_DATE]  an actual date column? Because when I try this i get an error 

 

Dax comparison operations do not support comparing values or FORMAT function to convert one of the values.

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.

Top Solution Authors