Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
djkoenig
Helper II
Helper II

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. 

 

  1. Sum of La2O3 g/L
  2. Moving Average of La2O3 g/L at different durations (ex: 30 Day MA, 60 Day MA, etc.)

Output = (Correct) Variance between 1 & 2. 


Code Used for Moving Average: 

 

 

MA 30 (La2O3) = 
//Selecting the date in the range
VAR LastestDate =
MAX ( 'Date'[Date] )
//Defining the duration to be considered for average calculation(k)
VAR Dura = 30
//Filtering the Calendar Table for the defined range
VAR CalculationPeriod =
FILTER (
ALL ( 'Date' ), -- Removing any external filters context applied --
AND (
'Date'[Date] > LastestDate - Dura, -- the range start date --
'Date'[Date] <= LastestDate -- the range end date --
)
)
//Calculating the Moving Average
VAR MovingAverage =
IF (
COUNTROWS ( CalculationPeriod ) >= Dura, -- Condition to check minimum number of days for the calculation --
CALCULATE (
AVERAGEX('SXDTable',[^La2O3(g/L)]), -- Calculating average of total amount --
CalculationPeriod
)
)
RETURN
MovingAverage

 

 

Same Code, nicer display. 
djkoenig_0-1703727802876.png

 

When I look at my two outputs individually I get the correct values. Let's use C101 as an example.

 

djkoenig_3-1703729562795.png

 

  • 30 Days = Easy two values on Dec. 5th and 7th, 42.84 and 43.89 respectively. This averages to ~43.37. Check!
  • 60 Days = Four values. 11/13. 11/29. 12/5. 12/7. Averages to 30.32. Check! 
  • 90 Days = Eight values. 10/3 - Current. Averages to 26.79. Check!

Now, what'd I like to do is evaluate the Moving Average vs. Individual Dates. I thought I'd just be able to subtract the two. However, each return is 0, regardless if I do it via Calculated Column or Measure minus Measure. 

 

Calculated Column Output = 

djkoenig_4-1703730116796.png

 

Measure Output =

djkoenig_5-1703730218972.png
This is the 30 day MA so the last two entries evaluate against themselves and give an output of 0 and the rest evaluate against 0 and return themselves. 

 

Instead what I would like is for the first two entries to evaluate against the 30 day moving average. In this case, we'd have 43.89 (output) vs. 43.37 (MA) and 42.84 (output) vs. 43.37 (MA). There's some rounding going on, but essentially since n=2, I'd expect an output of +0.53 and -0.53 for Output - MA. 

 

So, I just re-read this, and, ultimately, it appears I need help.... subtracting....? Hopefully, someone over a 1st grade math level can help me 🙃

 

Thanks in advance and even just for the time to read it! I really appreciate it. 

 

 

 

 

 

1 ACCEPTED SOLUTION
v-nuoc-msft
Community Support
Community Support

Hi @djkoenig 

 

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

 

I used the data you provided

 

“data”

vnuocmsft_0-1703843002373.png

 

“Date”

vnuocmsft_1-1703843015890.png

 

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

vnuocmsft_2-1703843028959.png

 

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).

vnuocmsft_3-1703843057533.png

 

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

vnuocmsft_4-1703843137478.png

 

Here is the result:

vnuocmsft_5-1703843146549.png

 

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.

 

View solution in original post

7 REPLIES 7
v-nuoc-msft
Community Support
Community Support

Hi @djkoenig 

 

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

 

I used the data you provided

 

“data”

vnuocmsft_0-1703843002373.png

 

“Date”

vnuocmsft_1-1703843015890.png

 

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

vnuocmsft_2-1703843028959.png

 

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).

vnuocmsft_3-1703843057533.png

 

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

vnuocmsft_4-1703843137478.png

 

Here is the result:

vnuocmsft_5-1703843146549.png

 

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!

Ashish_Mathur
Super User
Super User

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.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

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.co...

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. 

 

djkoenig_0-1703734035421.png

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.

Access Denied message.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

@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

 

djkoenig_0-1703782299295.png

If for some reason you still can't access, it's a whole 8 lines of data for the test. 

 

CellCell PathDateLa2O3 (g/L)
C101Extraction12/7/202343.89
C101Extraction12/5/202342.84
C101Extraction11/29/202325.27
C101Extraction11/13/20239.27
C101Extraction10/15/20239.75
C101Extraction10/8/202314.6
C101Extraction10/3/202341.88

 

 

 

 

I cannot understand what you want.  In another tab of the PBI file, show the expected result very clearly.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.