Help with moving average of a DAX measure

%3CLINGO-SUB%20id%3D%22lingo-sub-3085613%22%20slang%3D%22en-US%22%3EHelp%20with%20moving%20average%20of%20a%20DAX%20measure%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3085613%22%20slang%3D%22en-US%22%3E%3CP%3EGrateful%20for%20any%20help%20here.%20I%20have%20set%20up%20the%20following%20measure%20(Distinct%20Transaction)%20to%20calculate%20the%20number%20of%20distinct%20transactions%20under%20certain%20categories%20based%20on%20the%20table%20structure%20shown.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20been%20trying%20to%20make%20another%20measure%20to%20calculate%20a%2012-month%20moving%20average%20of%20this%20measure%2C%20but%20keep%20running%20into%20errors%20(I'm%20a%20relative%20newbie%20to%20DAX%2C%20following%20Youtube%20tutorials%20-%20but%20they%20do%20not%20seem%20to%20work%20against%20an%20existing%20measure).%26nbsp%3B%20I'm%20aiming%20to%20chart%20this%2012%20month%20moving%20average%20as%20a%20line%20chart%20on%20a%20dashboard.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESuggestions%20much%20appreciated.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDistinct%20Transaction%20%3D%20CALCULATE(DISTINCTCOUNT%20(ServiceDetail%5BActivity%20ID%5D)%2C%20ServiceDetail%5BTransaction%20Type%5D%20%3D%20%22New%20Sale%22%20%7C%7C%26nbsp%3BServiceDetail%5BTransaction%20Type%5D%20%3D%20%22Repeat%20Sale%22%20%7C%7C%26nbsp%3BServiceDetail%5BTransaction%20Type%5D%20%3D%20%22Site%20Repair%22%20)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%2245278E5E22D84C00BCEA4ABB009BE4DF%20(1).jpeg%22%20style%3D%22width%3A%20167px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F343562iEA1983FAA9A5637C%2Fimage-dimensions%2F167x177%3Fv%3Dv2%22%20width%3D%22167%22%20height%3D%22177%22%20role%3D%22button%22%20title%3D%2245278E5E22D84C00BCEA4ABB009BE4DF%20(1).jpeg%22%20alt%3D%2245278E5E22D84C00BCEA4ABB009BE4DF%20(1).jpeg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3085613%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBI%20%26amp%3B%20Data%20Analysis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPower%20BI%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3085920%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20moving%20average%20of%20a%20DAX%20measure%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3085920%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1290832%22%20target%3D%22_blank%22%3E%40JXHK_22%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPlease%20check%20this%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fwww.sqlbi.com%2Farticles%2Frolling-12-months-average-in-dax%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3ERolling%2012%20Months%20Average%20in%20DAX%20-%20SQLBI%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fwww.sqlbi.com%2Ftv%2Fcomputing-rolling-average-in-dax%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EComputing%20rolling%20average%20in%20DAX%20-%20SQLBI%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3091971%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20moving%20average%20of%20a%20DAX%20measure%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3091971%22%20slang%3D%22en-US%22%3EThanks%20for%20your%20help%20Sergei.%20I've%20tried%20to%20adapt%20the%20sample%20DAX%20from%20the%20site%20to%20my%20scenario%20(as%20below)%20.%20For%20some%20reason%2C%20I%20end%20up%20with%20constant%20values%20of%201.0%20as%20a%20result.%20Any%20idea%20what%20I've%20done%20wrong%3F%20I've%20not%20used%20some%20of%20these%20measures%20previously.%20Thank%20you.%3CBR%20%2F%3E%3CBR%20%2F%3E365%20Day%20Moving%20Average%20%3D%3CBR%20%2F%3EVar%20NumDays%20%3D%20365%3CBR%20%2F%3EVar%20LastCurrentDate%20%3D%20MAX(ServiceDetail%5BService%20Date%5D)%3CBR%20%2F%3EVar%20Period%20%3D%20DATESINPERIOD%20(ServiceDetail%5BService%20Date%5D)%2C%20LastCurrentDate%2C%20-NumDays%2C%20DAY)%3CBR%20%2F%3EVar%20Result%20%3D%3CBR%20%2F%3ECALCULATE(AVERAGEX(ServiceDetail%2C%20%5BDistinct%20Transaction%5D)%2C%20Period)%3CBR%20%2F%3EVAR%20FirstDateInPeriod%20%3D%20MINX(Period%2C%20ServiceDetail%5BService%20Date%5D)%3CBR%20%2F%3EVar%20LastDateInPeriod%20%3D%20MAX(ServiceDetail%5BService%20Date%5D)%3CBR%20%2F%3ERETURN%3CBR%20%2F%3EIF%20(FirstDateInPeriod%20%26lt%3B%3D%20LastDateInPeriod%2C%20Result)%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Grateful for any help here. I have set up the following measure (Distinct Transaction) to calculate the number of distinct transactions under certain categories based on the table structure shown. 

 

I've been trying to make another measure to calculate a 12-month moving average of this measure, but keep running into errors (I'm a relative newbie to DAX, following Youtube tutorials - but they do not seem to work against an existing measure).  I'm aiming to chart this 12 month moving average as a line chart on a dashboard. 

 

Suggestions much appreciated. 

 

Distinct Transaction = CALCULATE(DISTINCTCOUNT (ServiceDetail[Activity ID]), ServiceDetail[Transaction Type] = "New Sale" || ServiceDetail[Transaction Type] = "Repeat Sale" || ServiceDetail[Transaction Type] = "Site Repair" )

 

45278E5E22D84C00BCEA4ABB009BE4DF (1).jpeg

5 Replies
Thanks for your help Sergei. I've tried to adapt the sample DAX from the site to my scenario (as below) . For some reason, I end up with constant values of 1.0 as a result. Any idea what I've done wrong? I've not used some of these measures previously. Thank you.

365 Day Moving Average =
Var NumDays = 365
Var LastCurrentDate = MAX(ServiceDetail[Service Date])
Var Period = DATESINPERIOD (ServiceDetail[Service Date]), LastCurrentDate, -NumDays, DAY)
Var Result =
CALCULATE(AVERAGEX(ServiceDetail, [Distinct Transaction]), Period)
VAR FirstDateInPeriod = MINX(Period, ServiceDetail[Service Date])
Var LastDateInPeriod = MAX(ServiceDetail[Service Date])
RETURN
IF (FirstDateInPeriod <= LastDateInPeriod, Result)




@JXHK_22 

As a minimum, to use Time Intelligence function in DAX, like DATESINPERIOD(), you need to have table marked as Date Table. You may connect one from corporate server if you have such table on it (most preferrable way), or create by Power Query, or create Calendar table directly in Power Pivot (less preferable due to maintenance).

image.png

In any case be sure it's marked as Date Table.

Link ServiceDetail table with calendar on Date, hide [Service Date] field and use only Calendar dates in measures.

Perhaps something else, but that's better to discuss with sample file.

 

@Sergei Baklan 

 

Thanks Sergei. I do have [Service Date] column marked as a date field. 

I figured that I mistakenly copied the AVERAGEX measure from the example...in my source data, each row represents a record of service interaction, so actually I need to count the rows. I've now set up the moving average with a simpler DAX formula as below. 

 

But I'm wondering if there is an alternative measure to DATESINPERIOD that would apply to the 30 preceding dates with observations instead of the 30 preceding calendar days?

 

Mov Avg = 

VAR NumDays = 30
VAR RollingCount = CALCULATE(COUNT(‘ServiceDetail’[Activity ID]), DATESINPERIOD(‘ServiceDetail’[Service Date], LASTDATE(‘ServiceDetail’[Service Date]), -NumDays, DAY))
RETURN
RollingCount/NumDays
 
Many thanks again. 

@JXHK_22 

If days are not sequential (e.g. you have only dates with observations) the pattern could be

periodStartDate :=
VAR backDays = 30
VAR cDate =
    TODAY ()
RETURN
    CALCULATE (
        TOPN (
            1,
            TOPN ( backDays, VALUES ( Table1[Date] ), Table1[Date], DESC ),
            Table1[Date], ASC
        ),
        Table1[Date] <= cDate
    )