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.
Ashish_Mathur I thought I had specified permissions. My mistake. You should be good now.
https://drive.google.com/file/d/1TNqbJIIH9PVoxMDrtUy2DEjZMbGKRjsi/view?usp=drive_link
If for some reason you still can't access, it's a whole 8 lines of data for the test.
| Cell | Cell Path | Date | La2O3 (g/L) |
| C101 | Extraction | 12/7/2023 | 43.89 |
| C101 | Extraction | 12/5/2023 | 42.84 |
| C101 | Extraction | 11/29/2023 | 25.27 |
| C101 | Extraction | 11/13/2023 | 9.27 |
| C101 | Extraction | 10/15/2023 | 9.75 |
| C101 | Extraction | 10/8/2023 | 14.6 |
| C101 | Extraction | 10/3/2023 | 41.88 |
I cannot understand what you want. In another tab of the PBI file, show the expected result very clearly.