Forum Discussion

djkoenig's avatar
djkoenig
Helper II
2 years ago
Solved

DAX for Calculating Moving Average

Hello Experts,    I think this is an easy question and I've just overcomplicated it. I have two measures and need one output.    Sum of La2O3 g/L Moving Average of La2O3 g/L at different durati...
  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi djkoenig 

     

    For the questions you raised, here are the solutions I offered:

     

    I used the data you provided

     

    “data”

     

    “Date”

     

    Create a measure to find La2O3(g/L) within 30 days

     

    MA 30(La2O3) = var max_date = MAX('Date'[Date])
    var _la2o3 = CALCULATE(max('data'[La2O3 (g/L)]), FILTER(ALL('data'), [Cell] = MAX('data'[Cell]) && 'data'[La2O3 (g/L)] = MAX('data'[La2O3 (g/L)]) && [Date] > EDATE(max_date, -1) ))
    RETURN _la2o3 
    

     

     

    Calculate the average of MA 30 (La2O3).

     

    MA 30 (La2O3)_avg = var max_date = MAX('Date'[Date])
    var _la2o3_avg = CALCULATE(AVERAGE('data'[La2O3 (g/L)]), FILTER(ALL('data'), [Cell] = MAX('data'[Cell]) && [Date] > EDATE(max_date, -1) ))
    RETURN  _la2o3_avg

     

     

    If you want to remove blank rows from MA 30 (La2O3), you can do the following: you can apply "is not blank" to MA 30 (La2O3) in the Filters

     

    Here is the result:

     

    result = var max_date = MAX('Date'[Date])
    var _la2o3 = CALCULATE(max('data'[La2O3 (g/L)]), FILTER(ALL('data'), [Cell] = MAX('data'[Cell]) && 'data'[La2O3 (g/L)] = MAX('data'[La2O3 (g/L)]) && [Date] > EDATE(max_date, -1) ))
    var _la2o3_avg = CALCULATE(AVERAGE('data'[La2O3 (g/L)]), FILTER(ALL('data'), [Cell] = MAX('data'[Cell]) && [Date] > EDATE(max_date, -1) ))
    RETURN  _la2o3 - _la2o3_avg
    

     

     

    Best Regards,

    Nono Chen

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