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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
dantheram
Helper II
Helper II

Moving Average - not date based

hi all

 

i'm really struggling to understand how to compile what should be a very simple MAA. The issue is that i do not use date, i use period numbers; 1 to 13, as below -

 

dantheram_0-1668448235587.png

 

all i want is a moving average (for incidents) across the last 13 periods, so for P5 above the value would be the average of periods; 6,7,8,9,10,11,12,13,1,2,3,4,5. 

 

i just cannot get it to work, any help much appreciated

 

 

 

1 ACCEPTED SOLUTION

@dantheram 

Create a fiscal year period column in both tables the use it in the code. It is also advised to use this column to create the relationship. 
Fiscal YearPeriod =
VALUE ( SUBSTITUTE ( 'SAFs Actuals'[Fiscal Year], "/", "" ) ) * 100 + 'SAFs Actuals'[Period]

View solution in original post

28 REPLIES 28
Greg_Deckler
Community Champion
Community Champion

@dantheram Try adding an index so that you have something to define "before"



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Jihwan_Kim
Super User
Super User

Hi,

Please check the below measure and the attached pbix file.

I tried to create a sample pbix file like the attached file.

 

Moving avg 13 periods: =
VAR _currentperiod =
    MAX ( Data[Period] )
VAR _currentFYone =
    LEFT ( MAX ( Data[Fiscal Year] ), 4 ) * 1
VAR _newtableone =
    FILTER (
        ALL ( Data ),
        Data[Fiscal Year] = MAX ( Data[Fiscal Year] )
            && Data[Period] <= _currentperiod
    )
VAR _newtabletwo =
    FILTER (
        ALL ( Data ),
        LEFT ( Data[Fiscal Year], 4 ) * 1 = _currentFYone - 1
            && Data[Period] > _currentperiod
    )
VAR _unionnewtables =
    UNION ( _newtableone, _newtabletwo )
RETURN
    IF (
        HASONEVALUE ( Data[Period] ),
        SUMX ( _unionnewtables, Data[Incident Count] ) / 13
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

hi 

 

this one works fine but does not recalculate when i add in some of the lower level categories.

 

For example, my 'incidents' field can be split by geography or a sub category - how do i make the calculation dynamic?

 

thanks 

Hi,

Please provide sample pbix file's link and then I can try to look into it to come up with a solution.

Thanks.


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

i cant see an option to upload the file, here is a link -

 

Derby SAFs.pbix

Hi,

I am not sure but I think it is asking to login. If it is OK with you, could you please share it via onedrive, googledrive, or dropbox?

Thanks.


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.
tamerj1
Super User
Super User

Hi @dantheram 

you can create a calculated column for fiscal year rank

Fiscal Period Rank =
RANKX (
    'Date',
    VALUE ( LEFT ( 'Date'[Fiscal Year], 4 ) ) * 100 + 'Date'[Period],
    ,
    asc,
    DENSE
)

then the measure would be

Incident Count MA =
VAR CurrentRank =
    MAX ( 'Date'[Fiscal Period Rank] )
VAR T1 =
    FILTER (
        ALLSELECTED ( 'Date'[Fiscal Period Rank] ),
        'Date'[Fiscal Period Rank] <= CurrentRank
            && 'Date'[Fiscal Period Rank] >= CurrentRank - 12
    )
RETURN
    AVERAGEX (
        T1,
        VAR CurrentRank2 = 'Date'[Fiscal Period Rank]
        RETURN
            CALCULATE (
                [Incident Count],
                REMOVEFILTERS ( 'Date' ),
                'Date'[Fiscal Period Rank] = CurrentRank2
            )
    )

 

hi 

 

this one fails at the rank stage -

 

"a single value for column 'financial year in table 'SAFs Actuals' cannot be determined. this can happen when a measure formula refers to...."

 

any ideas?

@dantheram 

This is supposed to be a calculated column not a measure. Please read my answer above carefully 

hi 

 

apologies - i've added to the file as a calc'd column and it works but the same problem observed with the other solution offered here - no recalcualtion when i add in other categories; i.e. if i split the incidents counts down by geography the MAA stays based on the total

 

thanks

Dan 

@dantheram 
Also please note that a Date table is required for this solution to work properly.

so is the rank added to the 'date' table or the 'incident count' data table?

@dantheram 
The Date table

done - but its simply returing the period value >>>>

 

dantheram_0-1670278071062.png

 

@dantheram 
The [Financial Year] and [Period] are from which table?

correct - i had used the wrong date table

 

just need to tidy up the output now; how do i stop it starting at 600 and running on past period 5 (the last period with data, so the year to date in effect)?

 

dantheram_0-1670323077394.png

 

  

@dantheram 
Please clarify further

hi Tamer

 

the main issue is the calc running on past the year to date row - period 5, so it's picking up all the 0's and reducing the MA value, i need it to stop at the max value for period in the incident count dataset - so period 5

@dantheram 
Please try

Incident Count MA =
IF (
    NOT ISEMPTY ( 'Table' ),
    VAR CurrentRank =
        MAX ( 'Date'[Fiscal Period Rank] )
    VAR T1 =
        FILTER (
            ALLSELECTED ( 'Date'[Fiscal Period Rank] ),
            'Date'[Fiscal Period Rank] <= CurrentRank
                && 'Date'[Fiscal Period Rank] >= CurrentRank - 12
        )
    RETURN
        AVERAGEX (
            T1,
            VAR CurrentRank2 = 'Date'[Fiscal Period Rank]
            RETURN
                CALCULATE (
                    [Incident Count],
                    REMOVEFILTERS ( 'Date' ),
                    'Date'[Fiscal Period Rank] = CurrentRank2
                )
        )
)

this is so nearly there, 1 issue, when there are 0 incidents in a period this happens

 

dantheram_0-1670332407696.png

 

so we need the calc to process the '0' rows as '0' unless greater than period 5 - where we have no data as its in the future

 

the above is ignoring legitmate '0s' as blanks

@dantheram 
Please try

Incident Count MA =
VAR LastDateWithData =
    CALCULATE ( MAX ( 'Table'[Date] ), REMOVEFILTERS () )
VAR CurrentDate =
    MAX ( 'Date'[Date] )
RETURN
    IF (
        CurrentDate <= LastDateWithData,
        VAR CurrentRank =
            MAX ( 'Date'[Fiscal Period Rank] )
        VAR T1 =
            FILTER (
                ALLSELECTED ( 'Date'[Fiscal Period Rank] ),
                'Date'[Fiscal Period Rank] <= CurrentRank
                    && 'Date'[Fiscal Period Rank] >= CurrentRank - 12
            )
        RETURN
            AVERAGEX (
                T1,
                VAR CurrentRank2 = 'Date'[Fiscal Period Rank]
                RETURN
                    CALCULATE (
                        [Incident Count],
                        REMOVEFILTERS ( 'Date' ),
                        'Date'[Fiscal Period Rank] = CurrentRank2
                    )
            )
    )

the issue with the above is that the 'lastdatewithdata' and max current date lines will always be '13', as i have to use the period numbers and they reoccur each year - so there's always a 13.

 

so, it is working but still running on past period 5

 

dantheram_0-1670335313173.png

 

to be honest i can live without it stopping at year to date - so thanks very much ๐Ÿ™‚

@dantheram

No. Please look carefully at the code. The last date is calculated from the fact table not the date table. It should work 

but the fact table has periods 1 to 13 for previous years - i might be making an error but here is my version of your solution -

 

Incident Count MA2 = 
VAR LastDateWithData =
    CALCULATE ( MAX ( 'SAFs Actuals'[Period] ), REMOVEFILTERS () )
VAR CurrentDate =
    MAX ( 'Calendar'[Period2] )
RETURN
    IF (
        CurrentDate <= LastDateWithData,
        VAR CurrentRank =
            MAX ( 'Calendar'[Fiscal Period Rank] )
        VAR T1 =
            FILTER (
                ALLSELECTED ( 'Calendar'[Fiscal Period Rank] ),
                'Calendar'[Fiscal Period Rank] <= CurrentRank
                    && 'Calendar'[Fiscal Period Rank] >= CurrentRank - 12
            )
        RETURN
            AVERAGEX (
                T1,
                VAR CurrentRank2 = 'Calendar'[Fiscal Period Rank]
                RETURN
                    CALCULATE (
                        [Incident Count 2],
                        REMOVEFILTERS ( 'Calendar' ),
                        'Calendar'[Fiscal Period Rank] = CurrentRank2
                    )
            )
    )

@dantheram 

Create a fiscal year period column in both tables the use it in the code. It is also advised to use this column to create the relationship. 
Fiscal YearPeriod =
VALUE ( SUBSTITUTE ( 'SAFs Actuals'[Fiscal Year], "/", "" ) ) * 100 + 'SAFs Actuals'[Period]

you sir are a hero ๐Ÿ™‚

 

dantheram_0-1670343134665.png

thanks so much!

dantheram
Helper II
Helper II

thanks all - i'll be trying some of these tomrrow

 

Dan

Helpful resources

Announcements
November Fabric Update Carousel

Fabric Monthly Update - November 2025

Check out the November 2025 Fabric update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Users online (27)