Forum Discussion
Calculate Moving range ( subtract previous row value from earlier row)
- 7 years ago
Hi Anonymous ,
1. Insert an index column in power query as below.
M code as below.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKjYwKDXIzDMyyErMU9JRMjZVitVBiBpCRE0MUESNoKJGSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [key = _t, result = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"key", type text}, {"result", Int64.Type}}), #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1) in #"Added Index"2. Create the measure to get the exceoted result.
Measure = var change = CALCULATE(SUM(Table1[result]),FILTER(ALL(Table1),Table1[Index]=MAX(Table1[Index])-1)) return IF(ISBLANK(change),BLANK(),MAX(Table1[result])-change)
Pbix as attached.
Regards,
Frank
I want to get the moving average of the data below.
| Melt | Serial | Length (mm) | Moving Range |
| 567110 | 5776 | 134.9629 | |
| 567110 | 5777 | 134.9604 | 0.0025 = abs(134.9629-134.9604) |
| 567110 | 5778 | 134.9629 | 0.0025 |
| 567110 | 5779 | 134.9654 | 0.0025 |
| 567110 | 5780 | 134.9604 | 0.0051 |
| 567110 | 5781 | 134.9527 | 0.0076 |
| 567110 | 5782 | 134.9477 | 0.0051 |
| 567110 | 5783 | 134.9400 | 0.0076 |
| 567110 | 5784 | 134.9756 | 0.0356 |
v-frfei-msft had a solution using M code. When I copied the code from here to my file I received an error (most likely from referencing columns that do not exist). I am not sure what part of the code should be changed to make it work.
Hope that is enough information. Let me know if you have any other questions. Thank you!
Hi,
This calculated column formula works
=if(ISBLANK(CALCULATE(MAX(Table1[Length (mm)]),FILTER(Table1,Table1[Melt]=EARLIER(Table1[Melt])&&Table1[Serial]=EARLIER(Table1[Serial])-1))),BLANK(),ABS(Table1[Length (mm)]-CALCULATE(MAX(Table1[Length (mm)]),FILTER(Table1,Table1[Melt]=EARLIER(Table1[Melt])&&Table1[Serial]=EARLIER(Table1[Serial])-1))))
Hope this helps.
- amlopez457 years agoFrequent Visitor
Thank you! That did the trick.
If I wanted to get the moving average by Year could I use the same calculated column formula you provided?
Shipdate Melt Serial Length (mm) Moving Range 12/01/2018 567110 5776 134.9629 12/01/2018 567110 5777 134.9604 0.0025 = abs(134.9629-134.9604)
12/01/2018 567110 5778 134.9629 0.0025 12/01/2018 567110 5779 134.9654 0.0025 12/01/2018 567110 5780 134.9604 0.0051 01/15/2019 567110 5781 134.9527 0.0076 01/15/2019 567110 5782 134.9477 0.0051 01/15/2019 567110 5783 134.9400 0.0076 01/15/2019 567110 5784 134.9756 0.0356 - Ashish_Mathur7 years ago
Super User
Hi,
If my previous reply helped, please Accept it as solution. I do not understand your next question. Show the expected result.
- amlopez457 years agoFrequent Visitor
I will mark your response as solution. Thank you again for helping me out.
Please disregard my last question. I was able to figure it out.
- MuhamadIlhamsya2 years agoFrequent Visitor
can you please help me? want to have similar value but grouped by the parameter, so it substract the previous row but based on the same parameter
thanks!
- Ashish_Mathur2 years ago
Super User
Hi,
Not clear about your requirement. Share data in a format that can be pasted in an MS Excel file and show the expected result.