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
CloudMonkey
Post Prodigy
Post Prodigy

How to control axis when using a moving average?

Hi,

Please can you tell me how to control the axis of a graph when using a 3-day moving average? My problem here is that at the start of the date range the first two dates are a 1 and 2 day average respectively (highlighted). And at the end of the date range the last two dates are two and one day averages respectively (also highlighted). Please find the pbix file here -> https://filebin.net/apmzbk8wwjvi16fh

 

screenshot moving average.JPG

Thank you for your help,

CM

2 ACCEPTED SOLUTIONS
CNENFRNL
Community Champion
Community Champion

Hi, @CloudMonkey , Here Autodate messed things up, like always; that's why I barely use this feature. A measure can be authored like this WITHOUT autodate,

Volume rolling average (no Autodate) =
VAR __LAST_DATE =
    LASTDATE ( 'Sales'[Date] )
VAR __DATE_RNG =
    DATESBETWEEN ( 'Sales'[Date], DATEADD ( __LAST_DATE, -2, DAY ), __LAST_DATE )
RETURN
    IF (
        COUNTROWS ( __DATE_RNG ) = 3,
        AVERAGEX ( __DATE_RNG, CALCULATE ( SUM ( 'Sales'[Volume] ) ) )
    )

Screenshot 2020-09-13 235628.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

CNENFRNL
Community Champion
Community Champion

If you intend to keep Autodate with some other measures depending on it, this measure can be changed like this to remove those 4 highlighted values in the viz,

 

 

Volume rolling average =
IF (
    ISFILTERED ( 'Sales'[Date] ),
    ERROR ( "Time intelligence quick measures can only be grouped or filtered by the Power BI-provided date hierarchy or primary date column." ),
    VAR __LAST_DATE =
        LASTDATE ( 'Sales'[Date].[Date] )
    VAR __DATE_RNG =
        DATESBETWEEN (
            'Sales'[Date].[Date],
            DATEADD ( __LAST_DATE, -2, DAY ),
            __LAST_DATE
        )
    RETURN
        IF (
            COUNTROWS ( __DATE_RNG ) = 3
                && CALCULATE ( MAX ( Sales[Volume] ), __LAST_DATE ) > 0,
            AVERAGEX ( __DATE_RNG, CALCULATE ( SUM ( 'Sales'[Volume] ) ) )
        )
)

 

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

5 REPLIES 5
Icey
Community Support
Community Support

Hi @CloudMonkey ,

 

Please let us know whether @CNENFRNL 's answer is what you want.

 

 

Best Regards,

Icey

amitchandak
Super User
Super User

@CloudMonkey , Try like

 

Rolling 3 day =
var _max = minx(allselected(Date),[Date[Date])
var _min = minx(allselected(Date),[Date[Date])
return
CALCULATE(sum(Sales[Sales Amount]),DATESINPERIOD('Date'[Date],MAX(Sales[Sales Date]),-3,Day) filter(Date,'Date'[Date] <=_min && 'Date'[Date]>=_max)

 

or

CALCULATE(sum(Sales[Sales Amount]),DATESINPERIOD('Date'[Date],MAX(Sales[Sales Date]),-3,Day))

CNENFRNL
Community Champion
Community Champion

If you intend to keep Autodate with some other measures depending on it, this measure can be changed like this to remove those 4 highlighted values in the viz,

 

 

Volume rolling average =
IF (
    ISFILTERED ( 'Sales'[Date] ),
    ERROR ( "Time intelligence quick measures can only be grouped or filtered by the Power BI-provided date hierarchy or primary date column." ),
    VAR __LAST_DATE =
        LASTDATE ( 'Sales'[Date].[Date] )
    VAR __DATE_RNG =
        DATESBETWEEN (
            'Sales'[Date].[Date],
            DATEADD ( __LAST_DATE, -2, DAY ),
            __LAST_DATE
        )
    RETURN
        IF (
            COUNTROWS ( __DATE_RNG ) = 3
                && CALCULATE ( MAX ( Sales[Volume] ), __LAST_DATE ) > 0,
            AVERAGEX ( __DATE_RNG, CALCULATE ( SUM ( 'Sales'[Volume] ) ) )
        )
)

 

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

CNENFRNL
Community Champion
Community Champion

Hi, @CloudMonkey , Here Autodate messed things up, like always; that's why I barely use this feature. A measure can be authored like this WITHOUT autodate,

Volume rolling average (no Autodate) =
VAR __LAST_DATE =
    LASTDATE ( 'Sales'[Date] )
VAR __DATE_RNG =
    DATESBETWEEN ( 'Sales'[Date], DATEADD ( __LAST_DATE, -2, DAY ), __LAST_DATE )
RETURN
    IF (
        COUNTROWS ( __DATE_RNG ) = 3,
        AVERAGEX ( __DATE_RNG, CALCULATE ( SUM ( 'Sales'[Volume] ) ) )
    )

Screenshot 2020-09-13 235628.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Thank you CNENFRNL. It was autodate that was the problem. I coudn't quite understand averagex (a battle for another day!) so I used the formula below but it seems to be working. Thanks

 

Volume (3 day trailing total) (auto number of days) = 
VAR
    CurrentDate = max(Sales[Date])
VAR
    Date2DaysAgo = CurrentDate - 2
VAR
    DaysInMovingAverage = CurrentDate - Date2DaysAgo + 1
Return
    CALCULATE(sum(Sales[Volume]),filter(All(Sales[Date]),
        Sales[Date] >= Date2DaysAgo &&
        Sales[Date] <= CurrentDate
    )) / DaysInMovingAverage

 

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.