Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

fill nulls with previous months averages

Hello community,

 

I have a problem where I want to fill future months (nulls) with the last 3 months averagrs.

 

here is a screenshot of my editor page for illustration:

any thoughts on how to go around this problem?

 

Thank you,

M

  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi Anonymous ,

     

    Here's an example for the measure fill nulls with previous months average:

    Measure = var _maxdate=MAXX(FILTER(ALLSELECTED('Table'),[Value]<>BLANK()),[Date])
    var _start=EOMONTH(_maxdate,-3)
    var _end=EOMONTH(_maxdate,0)
    return IF(SUM('Table'[Value])=BLANK(),DIVIDE(CALCULATE(SUM('Table'[Value]),FILTER(ALLSELECTED('Table'),[Date]>_start&&[Date]<=_end)),3),SUM('Table'[Value]))

    The measure is calculated based on the maximum date in the 'Table' where the 'Value' column is not blank. The formula then calculates the start and end dates of a 3-month period ending with the maximum date. If the sum of the 'Value' column is blank, the formula calculates the average of the 'Value' column for the 3-month period. Otherwise, the formula returns the sum of the 'Value' column.

     

                                                                                                                                                             

    Best Regards,

    Stephen Tao

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.           

7 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

     

    Here's an example for the measure fill nulls with previous months average:

    Measure = var _maxdate=MAXX(FILTER(ALLSELECTED('Table'),[Value]<>BLANK()),[Date])
    var _start=EOMONTH(_maxdate,-3)
    var _end=EOMONTH(_maxdate,0)
    return IF(SUM('Table'[Value])=BLANK(),DIVIDE(CALCULATE(SUM('Table'[Value]),FILTER(ALLSELECTED('Table'),[Date]>_start&&[Date]<=_end)),3),SUM('Table'[Value]))

    The measure is calculated based on the maximum date in the 'Table' where the 'Value' column is not blank. The formula then calculates the start and end dates of a 3-month period ending with the maximum date. If the sum of the 'Value' column is blank, the formula calculates the average of the 'Value' column for the 3-month period. Otherwise, the formula returns the sum of the 'Value' column.

     

                                                                                                                                                             

    Best Regards,

    Stephen Tao

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.           

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Anonymous ,

       

      Much appreciate your help here this actually worked.

      One last step to make it perfect, I applied an IF statement to replace for values coming up as NULLs (future ones) while keeping actual as is.

      My formula went as the following; avg_forecast = if([Measure_actual] = NULL, [Measure], [Measure_Actual]).

       

      I was encountered with error stating 'NULL' is not supported in this context. Any input/suggestion to this?

       

      Thank you,

      MMB

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi Anonymous ,

         

        Try using BLANK() instead of NULL.

        avg_forecast = if([Measure_actual] = BLANK() , [Measure], [Measure_Actual]).

         

        Best Regards,

        Stephen Tao

         

        If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.           

    • Anonymous's avatar
      Anonymous
      Not applicable

      Could we do a measure then?

  • ppm1's avatar
    ppm1
    Icon for Solution Sage rankSolution Sage

    What do you plan to do with these columns once generated? Why not just calculate it with a measure so it is responsive to slicers and doesn't take up memory and/or increase refresh time?

     

    Pat

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Pat,

       

      Appreciate putting the option. I would be interested to see a DAX solution, could you provide that?

       

      Thanks,

      M