Forum Discussion

Dicken's avatar
Dicken
Icon for Post Prodigy rankPost Prodigy
23 days ago

Cumulative average grain

HI,

I want to create a cumulative average    i have a colendar table, and a pivot table of 
Year / Month   ;  so if   month 1 = 10   and month 2 = 20   averate so far = 30  if
month 3 = 20   average =  16.66  

For  this i  have a avagerage measure ;  ;

AvgMonth:=AVERAGEX( ALL('Calendar'[MMM-YYYY]),[Tunits])       Tunits=   sum of units coliumn 

then 

AccAvg:=CALCULATE( [AvgMonth], FILTER(ALL('Calendar'[Date]),'Calendar'[Date] <= MAX( 'Calendar'[Date]) && YEAR('Calendar'[Date]) = YEAR(MAX('Calendar'[Date])))

this works but is there a better way of getting correct grain,   month count i know can help 
but assumes all months present,  any ideas about tackling this; 

Richard

15 Replies

  • ShahRukhSameer's avatar
    ShahRukhSameer
    Icon for Continued Contributor rankContinued Contributor

    Hi Dicken​,

    I think the main thing here is to count only the months where you actually have data, rather than simply counting the months in the calendar.

    You could calculate the cumulative units and then divide by the number of months that have a value.

    AccAvg :=
    VAR CurrentDate =
    MAX ( 'Calendar'[Date] )
    VAR CumUnits =
    CALCULATE (
    [Tunits],
    FILTER (
    ALL ( 'Calendar' ),
    'Calendar'[Date] <= CurrentDate
    && YEAR ( 'Calendar'[Date] ) = YEAR ( CurrentDate )
    )
    )
    VAR MonthCount =
    CALCULATE (
    DISTINCTCOUNT ( 'Calendar'[MMM-YYYY] ),
    FILTER (
    ALL ( 'Calendar' ),
    'Calendar'[Date] <= CurrentDate
    && YEAR ( 'Calendar'[Date] ) = YEAR ( CurrentDate )
    && CALCULATE ( [Tunits] ) <> BLANK ()
    )
    )
    RETURN
    DIVIDE ( CumUnits, MonthCount )

    For example, if you have:

    Jan = 10
    Feb = 20
    Mar = 20
    Apr = blank
    May = 30

    the result would be:

    Jan → 10
    Feb → 15
    Mar → 16.67
    Apr → 16.67
    May → 20

    So April doesn't increase the denominator because there is no data for that month.

    I would also recommend having a proper month-level column in the Calendar table rather than using MMM-YYYY as the calculation grain. Something like MonthStart = DATE(YEAR('Calendar'[Date]), MONTH('Calendar'[Date]), 1) would be safer, particularly when you're working across multiple years.

      • ShahRukhSameer's avatar
        ShahRukhSameer
        Icon for Continued Contributor rankContinued Contributor

        Hi Dicken​,

        If that worked, could you mark it as a solution so the others facing the same issue could find it easily. 

        Thanks.

  • Hi,

    Try this measure

    =AVERAGEX(datesbetween(calendar[date],minx(all(calendar),calendar[date]),max(calendar[date])),[Tunits])

    Hope this helps.

    • Dicken's avatar
      Dicken
      Icon for Post Prodigy rankPost Prodigy

      Thanks, re the method of  accumulation /   count of months,  I think this assumes all month present in  fact table as well as calendar, which they might not be. 

      • Ashish_Mathur's avatar
        Ashish_Mathur
        Icon for Super User rankSuper User

        Hi,

        Please share some data to work with and show the expected result.

  • Hi Dicken​ 

    AVERAGEX skips rows without data so they're not counted in the denominator. And if you want that average to be applied to the  whole visible rows, you can just use ALLSELECTED

    Average without Jan = AVERAGEX(     ALL(Dates[Month and Year]),     [Total Revenue without Jan] )
     Average without Jan - allselected = CALCULATE(     AVERAGEX(         ALL(Dates[Month and Year]),         [Total Revenue without Jan]     ),     ALLSELECTED(Dates) )

     

    Please see the attached pbix.

  • jamilqpr's avatar
    jamilqpr
    Frequent Visitor

    AccAvg = AVERAGEX( FILTER( VALUES('Calendar'[MMM-YYYY]), NOT ISBLANK([Tunits]) ), [Tunits] ) This averages only the months present in the current filter context with data, so missing months aren't assumed to exist.

  • Dicken​  if   month 1 = 10   and month 2 = 20   averate so far = 30  why here the average is 30?

    could you pls provide some sample data and expected output?

  • Dicken's avatar
    Dicken
    Icon for Post Prodigy rankPost Prodigy

    Surry it should be 15, BTW has this site become very unstable over the last week,
    i have difficulty using it 

    • ryan_mayu's avatar
      ryan_mayu
      Icon for Super User rankSuper User

      Dicken​ 

      yes , i feel the same. so far the new version is not as good as the original one. 

      pls provide some sample data and expected output. then we can provide you the proper solution

  • v-kathullac's avatar
    v-kathullac
    Icon for Community Support rankCommunity Support

    Hi Dicken​ ,

    As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?

    Regards,

    Chaithanya

    • Dicken's avatar
      Dicken
      Icon for Post Prodigy rankPost Prodigy

      I will when you put the 'accept as solution' back on the site, or tell me where it is. 

  • v-kathullac's avatar
    v-kathullac
    Icon for Community Support rankCommunity Support

    Hi Dicken​ ,

    The Community UI has recently been updated. For your reference, I am sharing a screenshot highlighting the option to accept the relevant answer that helped resolve your issue.

    Thanks,

    Chaithanya.