Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

SOLVED.DAX Measure previous 3 months average

Hi Everyone,

 

I'm having an issue on my DAX measure code that getting the average of previous 3 months. presently i'm getting incorrect result in my Visualization. The Correct values For April, the avg value of (Jan-Mar) should be 84.14%, May, the avg value (Feb-Apr) should be 85.45%.  for Nov, the avg value  of (Aug-Oct) is 84.99%. I'm using a Date Table. May I Know what is the problem with my DAX Code? Is there any other approach to get the average 3 mos.? any help is very much appreciated. Thank you.

 

DAX Code:

AvgPrv3Months =
var avprev3mos = if(ISBLANK([FG %]),BLANK(),IF(COUNTROWS(DATESBETWEEN(DateTable[Date],EDATE(MIN(DateTable[Date]),-3),MIN(DateTable[Date])-1))>61,CALCULATE(AVERAGEX(values(DateTable[Date]),[FG %]),DATESBETWEEN(DateTable[Date],EDATE(MIN(DateTable[Date]),-3),MIN(DateTable[Date])-1)),BLANK()))
return avprev3mos
 
Incorrect Value:
  • Anonymous's avatar
    Anonymous
    5 years ago

    amitchandak Thank you very much. I modified the code and its alerady working.

    CALCULATE (
    AVERAGEX ( VALUES ( DateTable[MonthYear] ), [FG %] ),
    DATESINPERIOD ( DateTable[Date], ENDOFMONTH(DATEADD(DateTable[Date],-1, MONTH )), -3,MONTH))

9 Replies

  • Anonymous , Try like this example

    Rolling 3 till last month = CALCULATE(sum(Sales[Sales Amount]),DATESINPERIOD('Date'[Date],ENDOFMONTH(dateadd(Sales[Sales Date],-1,month)),-3,MONTH))

    • Anonymous's avatar
      Anonymous
      Not applicable

      amitchandak Thank you very much. I modified the code and its alerady working.

      CALCULATE (
      AVERAGEX ( VALUES ( DateTable[MonthYear] ), [FG %] ),
      DATESINPERIOD ( DateTable[Date], ENDOFMONTH(DATEADD(DateTable[Date],-1, MONTH )), -3,MONTH))
      • amitchandak's avatar
        amitchandak
        Icon for Super User rankSuper User

        Anonymous , Can you accept the appropriate solution? 

  • Anonymous , try like the given example. Prefer a date table

     

    Rolling 3 = divide( CALCULATE(sum(Sales[Sales]),DATESINPERIOD('Date'[Date ],MAX('Date'[Date]),-12,MONTH)) ,
    CALCULATE(distinctCOUNT('Date'[Month Year]),DATESINPERIOD('Date'[Date],MAX('Date'[Date]),-12,MONTH), not(isblank((Sales[Sales])))))

     

    Dividing by no of months. Also using a column from the fact table to make sure it does not count month from date table without data.

     

    To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :radacad sqlbi My Video Series Appreciate your Kudos.

    • Anonymous's avatar
      Anonymous
      Not applicable

      I tried this code and its working. the problem is the current month was included plus the previous 2 month.  How can i exclude the current month and extend the calculation to previous 3 month.

       

      Average of previous 3 months =

      CALCULATE (
      AVERAGEX ( VALUES ( DateTable[MonthYear] ), [FG %] ),
      DATESINPERIOD ( DateTable[Date], MAX ( DateTable[Date] ), -3, MONTH )
      )
       
  • Anonymous , Try like

    AvgPrv3Months =
    divide( CALCULATE([FG %],DATESINPERIOD('DateTable'[Date ],MAX('DateTable'[Date]),-12,MONTH)) ,
    CALCULATE(distinctCOUNT('DateTable'[Month Year]),DATESINPERIOD('DateTable'[Date],MAX('DateTable'[Date]),-12,MONTH), not(isblank([FG %]))))

  • Anonymous's avatar
    Anonymous
    Not applicable
    [3M Avg] =
    // __monthCount tells you how many months
    // you want to use to get the average;
    // you can change it to any number you want;
    // to get a 6M average you just change to 6
    var __monthCount = 3
    var __anchorDate = MAX( DateTable[Date] )
    var __periodToAvgOver =
        DATESINPERIOD(
            DateTable[Date],
            EOMONTH( __anchorDate, -1 ),
            (-1) * __monthCount,
            MONTH
        )
    var __canCalculate =
        // check if there are enough months
        // to go back; works only if DateTable
        // is marked as a Date table in the model
        CALCULATE(
            DISTINCTCOUNT( DateTable[MonthYear] ) = __monthCount,
            __periodToAvgOver
        )
    var __output = 
        if( __canCalculate,
            CALCULATE(
                AVERAGEX(
                    VALUES( DateTable[MonthYear] ),
                    [FG %]
                ),
                __periodToAvgOver
            )
        )
    return
        __output
    • Anonymous's avatar
      Anonymous
      Not applicable

       Thank you for the reply. May i know if this will work with filter?