Forum Discussion
Abi_W_981
3 years agoFrequent Visitor
Calculating Difference from Previous Row
Hi, we have a table of data and results for each half term for a unique ID. Example table below - what I want to be able to find is the half termly changes in grade value between half terms (excl...
ronrsnfld
Super User
3 years agoYou can do this all in Power Query
- Group by Unique ID
- For each subgroup
- Add an shifted (offset) Grade value column
- (more efficient subtraction than using an Index column)
- Subtract the Shifted Grade Value from the Gr ade Value
- Add an shifted (offset) Grade value column
- Re-expand the table
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNlHSUQJhx9KS0tw8BUOlWB24uDlC3AhZ3BSIgwuKMvPSoepNzcwtgGKGRugGwSRM0E1ClkAxytLQwNAQ1W6cEkYoEhYoRsUCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Unique ID" = _t, #"Grade Value" = _t, #"Half Term" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Unique ID", Int64.Type}, {"Grade Value", Int64.Type}, {"Half Term", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Unique ID"}, {
{"Half Termly Changes", (t)=>
let
//add shifted grade value column
//Then subtract from grade value
#"Shifted Grade Value" = Table.FromColumns(
Table.ToColumns(t)
& {{null} & List.RemoveLastN(t[Grade Value])},
type table[Unique ID=nullable number, Grade Value=nullable number, Half Term=nullable text, Shifted Grade Value=nullable number]),
#"Change" = Table.AddColumn(#"Shifted Grade Value","Half Termly Change", each [Grade Value] - [Shifted Grade Value], type nullable number),
#"Remove Shifted" = Table.RemoveColumns(#"Change",{"Shifted Grade Value"})
in
#"Remove Shifted",
type table [Unique ID=nullable number, Grade Value=nullable number, Half Term=nullable text, Half Termly Changes=nullable number]}}),
#"Expanded Half Termly Changes" = Table.ExpandTableColumn(#"Grouped Rows", "Half Termly Changes", {"Grade Value", "Half Term", "Half Termly Change"})
in
#"Expanded Half Termly Changes"Results
ManoukUBN
1 year agoFrequent Visitor
I found this post while looking for a solution for a problem in my own data. Thanks so much, it helped a lot!