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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
CHill86
Frequent Visitor

Moving Average - Visual Level Filters Applied

Hi, I got the moving average working as below: 

 

MovingAverage2MonthAllRefs = CALCULATE (
    AVERAGEX ( ALLSELECTED(Referrals), Referrals[Total] ),
    DATESINPERIOD (
        Referrals[Date],
        LASTDATE ( Referrals[Date]),
        -2,
        MONTH
    )
)

 

However when I try to apply a Visual Level Filter the Moving Average no longer calculates correctly.

 

MmmYyCCGNameTotalMovingAverage2MonthAllRefs
Aug-16NHS EAST STAFFORDSHIRE CCG2127990.9376798
Sep-16NHS EAST STAFFORDSHIRE CCG23411121.31327
Oct-16NHS EAST STAFFORDSHIRE CCG21111016.064458
Nov-16NHS EAST STAFFORDSHIRE CCG23171056.797127
Dec-16NHS EAST STAFFORDSHIRE CCG1919976.8295129
Jan-17NHS EAST STAFFORDSHIRE CCG20551024.602256
Feb-17NHS EAST STAFFORDSHIRE CCG2140993.0767072
Mar-17NHS EAST STAFFORDSHIRE CCG26651206.418858
Apr-17NHS EAST STAFFORDSHIRE CCG19201043.4
May-17NHS EAST STAFFORDSHIRE CCG22971184.435986
Jun-17NHS EAST STAFFORDSHIRE CCG22031150.457726
Jul-17NHS EAST STAFFORDSHIRE CCG21311002.993357
8 REPLIES 8
v-chuncz-msft
Community Support
Community Support

@CHill86,

 

Please share us your simplified model and expected result.

Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

I just have 1 table (Referrals) which looks like the below

 

DateSpecialty NameRef SourcePriorityMmmYyVisitTypeE_ReferralCCGNameFYMonthTotal
01/04/2016Acute Internal MedicineGP REFERRALRoutineApr-16Cons0NHS EAST STAFFORDSHIRE CCG2016/17April2
01/04/2016Acute Internal MedicineGP REFERRALRoutineApr-16Cons0NHS SOUTH EAST STAFFORDSHIRE AND SEISDON PENINSULA CCG2016/17April1
01/04/2016AudiologyGP REFERRALRoutineApr-16NonCons0NHS CANNOCK CHASE CCG2016/17April1
01/04/2016AudiologyGP REFERRALRoutineApr-16NonCons0NHS EAST LEICESTERSHIRE AND RUTLAND CCG2016/17April1
01/04/2016AudiologyGP REFERRALRoutineApr-16NonCons0NHS EAST STAFFORDSHIRE CCG2016/17April28
01/04/2016AudiologyGP REFERRALUrgentApr-16NonCons0NHS SOUTH EAST STAFFORDSHIRE AND SEISDON PENINSULA CCG2016/17April1
01/04/2016AudiologyGP REFERRAL FROM CABRoutineApr-16NonCons1NHS SOUTH EAST STAFFORDSHIRE AND SEISDON PENINSULA CCG2016/17April1
01/04/2016AudiologyGP REFERRAL FROM CABRoutineApr-16NonCons13NHS SOUTHERN DERBYSHIRE CCG2016/17April13
01/04/2016AudiologyREFERRAL FROM CAB UNKNOWN GPRoutineApr-16NonCons1NHS SOUTH EAST STAFFORDSHIRE AND SEISDON PENINSULA CCG2016/17April1
01/04/2016AudiologyREFERRAL FROM CAB UNKNOWN GPRoutineApr-16NonCons2NHS SOUTHERN DERBYSHIRE CCG2016/17April2
01/04/2016AudiologyREFERRAL FROM CAB UNKNOWN GPUrgentApr-16NonCons1NHS SOUTH EAST STAFFORDSHIRE AND SEISDON PENINSULA CCG2016/17April1

 

I am trying to create a column chart which shows the Total Referrals per month - using the Total and MmmYy columns.

I have then created a calculated column using the code in my first message to calculate a moving 2 month average of Total.

However when I apply a Visual Level Filter to my column chart, e.g. where CCGName = ''NHS EAST STAFFORDSHIRE CCG",  the moving average is not calculating correctly.

The results I am getting when applying this visual level filter are as follows:

MmmYyTotalMovingAverage2MonthAllRefs
Aug-162127990.9376798
Sep-1623411121.31327
Oct-1621111016.064458
Nov-1623171056.797127
Dec-161919976.8295129
Jan-1720551024.1
Feb-172140992.6124357
Mar-1726651211.669687
Apr-1719201042.911517
May-1722971184.435986
Jun-1722031148.128031
Jul-1721311005.195519

 

However I would expect the following results (based on using the moving 2 month average in Excel)

MmmYyTotalMovingAverage2MonthAllRefs
Aug-162127 
Sep-1623412234
Oct-1621112226
Nov-1623172214
Dec-1619192118
Jan-1720551987
Feb-1721402097.5
Mar-1726652402.5
Apr-1719202292.5
May-1722972108.5
Jun-1722032250
Jul-1721312167

 

Perhaps my original script to calculate the moving average is incorrect, however it seems to be cprrect when the visual level filter isnt applied.

 

Thanks for your help, I am a total novice with Power BI.

Anonymous
Not applicable

Instead of one table you should have at least two.

Create a Date table. Link it to your Referral table.
Try the following measure after the above steps.

MovingAverage2MonthAllRefs = CALCULATE (
    AVERAGEX ( Referrals, Referrals[Total] ),
    DATESINPERIOD (
        Dates[Date],
        LASTDATE ( Dates[Date]),
        -2,
        MONTH
    )
)

I changed the columns in the DatesPeriod function.

thanks for your reply, but would you mind explaining why a dates table is needed when my source table already includes a date?

Hi @CHill86,

Could you please try this,

 

MovingAverage2MonthAllRefs = CALCULATE (
    AVERAGEX ( ALLSELECTED(Referrals), Referrals[Total] ),
    DATEADD(
        LASTDATE ( Referrals[Date]),
        -2,
        MONTH
    )
)
Anonymous
Not applicable

My answer was based on the assumption you were creating a measure.

 

In terms of the reason for the date table, you might want to read https://powerpivotpro.com/2011/11/the-ultimate-date-table/

@CHill86 Have you tried calculated measure instead of the calculated column?

@CHill86,

 

A simplied .pbix file would be better.

Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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 FABINSIDER for a $400 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

March2025 Carousel

Fabric Community Update - March 2025

Find out what's new and trending in the Fabric community.