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.
It is difficult to understand your question with just text and images. Share the download link of the PBI file. Clearly show the problem there and the expected result.
Ah apologies Ashish_Mathur. I can get a bit wordy. Had to create a sample dataset, but here is the PBIX:
https://drive.google.com/file/d/1TNqbJIIH9PVoxMDrtUy2DEjZMbGKRjsi/view?usp=drivesdk [drive.google.com]
I'd like for the aggregated value to append to each individual line, even with the presence of date. This way I can subtract an individual value from the moving average and decide whether variance is material.
From the sample PBIX, instead of 42.84 and 43.89 for the MA 30 (La2O3) value, I'd like to see the average 43.37 for both instances.
Does that help clarify? Also, pretty new, so I'm probably doing something goofy.
- Ashish_Mathur2 years agoSuper User
Access Denied message.
- djkoenig2 years agoHelper II
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 - Ashish_Mathur2 years agoSuper User
I cannot understand what you want. In another tab of the PBI file, show the expected result very clearly.