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
hxkresl
Helper V
Helper V

30day moving average - does it require summarized table?

Hello, I'm trying to get 30 day moving avg measure to work without having to first create a summarized table (count, group by date) of source data.  

 

 Measure that works with summarized table

30 Day Moving Avg =
CALCULATE(
AVERAGEX ('Summary Table (Case Count)', 'Summary Table (Case Count)'[CountofCasesperday]),
    DATESINPERIOD (
        'Summary Table (Case Count)'[EOWeekMonSun],
        LASTDATE ('Summary Table (Case Count)'[EOWeekMonSun]),
        -30,
        DAY
    )
)

 

Outcome - good

The data points on the line show the average of the last 30 day count of cases.  On Apr 9, moving avg is same as count, Apr 16 it is 4+37 / 2, Apr 23 4 + 37 + 56 / 3, etc.

graph on summarized tablegraph on summarized table

 

 

 

 

 

But essentially the same measure on unsummarized data table does not give needed outcome

MovingAvg =
CALCULATE(
AVERAGEX ('Ticket-Details', 'Ticket-Details'[CasesPerDay]),
    DATESINPERIOD (
        'Ticket-Details'[EOWeek],
        LASTDATE ('Ticket-Details'[EOWeek]),
        -30,
        DAY
    )
)

 

Outcome - not goodCapture1.PNG

 

 

 

Why is measure not working on unsummarized data. Is summarized table only way to go?  

1 ACCEPTED SOLUTION
Eric_Zhang
Employee
Employee


@hxkresl wrote:


The data points on the line show the average of the last 30 day count of cases.  On Apr 9, moving avg is same as count, Apr 16 it is 4+37 / 2, Apr 23 4 + 37 + 56 / 3, etc.


@hxkresl

Not clear about the underlying data in your table, however according to the description on what "average" is expected in your case, the measure below shall work.

MovingAvg =
DIVIDE (
    CALCULATE (
        SUM ( 'Ticket-Details'[CasesPerDay] ),
        FILTER (
            ALLSELECTED ( 'Ticket-Details' ),
            'Ticket-Details'[EOWeek] <= MAX ( 'Ticket-Details'[EOWeek] )
        )
    ),
    CALCULATE (
        DISTINCTCOUNT ( 'Ticket-Details'[EOWeek] ),
        FILTER (
            ALLSELECTED ( 'Ticket-Details' ),
            'Ticket-Details'[EOWeek] <= MAX ( 'Ticket-Details'[EOWeek] )
        )
    )
)

View solution in original post

2 REPLIES 2
Eric_Zhang
Employee
Employee


@hxkresl wrote:


The data points on the line show the average of the last 30 day count of cases.  On Apr 9, moving avg is same as count, Apr 16 it is 4+37 / 2, Apr 23 4 + 37 + 56 / 3, etc.


@hxkresl

Not clear about the underlying data in your table, however according to the description on what "average" is expected in your case, the measure below shall work.

MovingAvg =
DIVIDE (
    CALCULATE (
        SUM ( 'Ticket-Details'[CasesPerDay] ),
        FILTER (
            ALLSELECTED ( 'Ticket-Details' ),
            'Ticket-Details'[EOWeek] <= MAX ( 'Ticket-Details'[EOWeek] )
        )
    ),
    CALCULATE (
        DISTINCTCOUNT ( 'Ticket-Details'[EOWeek] ),
        FILTER (
            ALLSELECTED ( 'Ticket-Details' ),
            'Ticket-Details'[EOWeek] <= MAX ( 'Ticket-Details'[EOWeek] )
        )
    )
)

Thank you  @Eric_Zhang

 

 I understand you are dividing the total count of cases by the total number of week periods, which is giving me the desired average at the intersection of date and count.  Thank you for showing me.   

I added range to make so only 30 day avg and now it's working perfectly against source data.

 

30DayMovingAvg_CaseCount =
DIVIDE (
    CALCULATE (
        COUNTA( 'Ticket-Details'[Case #] ),
        FILTER (
            ALLSELECTED ( 'Ticket-Details' ),
            'Ticket-Details'[WeekEnd] <= MAX ( 'Ticket-Details'[WeekEnd] ) && 'Ticket-Details'[WeekEnd] >= MAX ( 'Ticket-Details'[WeekEnd] )-30
        )
    ),
    CALCULATE (
        DISTINCTCOUNT ( 'Ticket-Details'[WeekEnd] ),
        FILTER (
            ALLSELECTED ( 'Ticket-Details' ),
            'Ticket-Details'[WeekEnd] <= MAX ( 'Ticket-Details'[WeekEnd] ) && 'Ticket-Details'[WeekEnd] >= MAX ( 'Ticket-Details'[WeekEnd] )-30
        )
    )
)

 

Ta daah!!!Capture5.PNG

 

 

 

 

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.