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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.