Forum Discussion
DAX for Calculating Moving Average
- Anonymous2 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 _la2o3Calculate 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_avgIf 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_avgBest Regards,
Nono Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
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.
I really appreciate the effort on this! That does in fact work. I can now do some additional analysis.
I liked your edate (max date - 1) to gate time as well. Answer accepted!