Forum Discussion
Subtract Columns based on Position during Transform Data Stage
- 4 years ago
Hi zia_ward ,
You can try this query:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VdBBCoAwDETRu7gWmiZNW88i3v8aimDnSzef8shiznNz81qet+0rU+32tYs4iIOESIBEiUWaSANpICmSIAnSRTpIBxkiA2SATJEJMkus/6MwU60r1bCdcTzDofqb9R3wum4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"2021/1/1" = _t, #"2021/1/15" = _t, #"2021/1/20" = _t, #"2021/2/1" = _t, #"2021/2/15" = _t, #"2021/2/20" = _t, #"2021/3/1" = _t, #"2021/3/15" = _t, #"2021/3/30" = _t, #"2021/4/1" = _t, #"2021/4/15" = _t, #"2021/4/30" = _t, #"2021/5/1" = _t, #"2021/5/15" = _t, #"2021/5/30" = _t, #"2021/6/1" = _t, #"2021/6/15" = _t, #"2021/6/30" = _t, #"2021/7/1" = _t, #"2021/7/15" = _t, #"2021/7/30" = _t, #"2021/8/1" = _t, #"2021/8/15" = _t, #"2021/8/31" = _t, #"2021/9/1" = _t, #"2021/9/15" = _t, #"2021/9/30" = _t, #"2021/10/1" = _t, #"2021/10/15" = _t, #"2021/10/31" = _t, #"2021/11/15" = _t, #"2021/12/15" = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"2021/1/1", type date}, {"2021/1/15", type date}, {"2021/1/20", type date}, {"2021/2/1", type date}, {"2021/2/15", type date}, {"2021/2/20", type date}, {"2021/3/1", type date}, {"2021/3/15", type date}, {"2021/3/30", type date}, {"2021/4/1", type date}, {"2021/4/15", type date}, {"2021/4/30", type date}, {"2021/5/1", type date}, {"2021/5/15", type date}, {"2021/5/30", type date}, {"2021/6/1", type date}, {"2021/6/15", type date}, {"2021/6/30", type date}, {"2021/7/1", type date}, {"2021/7/15", type date}, {"2021/7/30", type date}, {"2021/8/1", type date}, {"2021/8/15", type date}, {"2021/8/31", type date}, {"2021/9/1", type date}, {"2021/9/15", type date}, {"2021/9/30", type date}, {"2021/10/1", type date}, {"2021/10/15", type date}, {"2021/10/31", type date}, {"2021/11/15", type date}, {"2021/12/15", type date}}), ColumnNames = Table.ColumnNames(#"Changed Type"), #"Added column" = Table.AddColumn( #"Changed Type","Diff",each let Last30 = List.LastN(ColumnNames,31) in Number.From(Date.From(List.Last(Last30)) - Date.From(List.First(Last30))), type number ) in #"Added column"Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Ok, that actually simplifies it--you can change the final step to:
= let LastValue = ColumnCount, FirstValue = ColumnCount-30 in ColumnNames{LastValue} - ColumnNames{FirstValue}
--Nate
Hi Nate,
I get the following error when I add the last step:
Expression.Error: There weren't enough elements in the enumeration to complete the operation.
Any clue why this is happening? Here is what I did:
1- I right click on ColumnCount step and added "Insert Step After". I then added the exact formula
in the formula bar: = let LastValue = ColumnCount, FirstValue = ColumnCount-30 in ColumnNames{LastValue} - ColumnNames{FirstValue}
2 - I get the error message.
Another error that I get when I adjust the position of last column, (= let LastValue = ColumnCount-1, FirstValue = ColumnCount-30 in ColumnNames{LastValue} - ColumnNames{FirstValue} )
Expression.Error: We cannot apply operator - to types Text and Text.
Details:
Operator=-
Left=9/1/21
Right=8/3/21
Thanks,
Zia