Forum Discussion
Merging query on itself using value -1
- 5 years ago
Hi sprotson The 2nd and 4th parameters of the join function are column names, which are text. You cannot do any math on them to increment rows.
Take a look at this code:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSAeNEpVgdCM8IiJPgPGMgTobzTIA4Bc4zBeJUpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [BCID = _t, Version = _t, Value = _t]), ShiftedTable = {null} & List.RemoveLastN(Table.Column(Source, "Version"), 1), OtherColumn = {null} & Table.SelectRows(Source, each List.Contains(ShiftedTable, [Version]))[Value], NestedLists = Table.ToColumns(Source) & {ShiftedTable} & {OtherColumn}, BackToTable = Table.FromColumns(NestedLists, Table.ColumnNames(Source) & {"Previous Row"} & {"Previous Value"}) in BackToTableThis will take this table:
and convert to this table:
THis is based somewhat on Imke's article here, but hers goes into much more detail using a custom function. Mine assume you only need to compare the Value to Previous Value. You can add more columns in the transformation by adding lists in the NestedLists step and adding those column names in the final step.
How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.The other way to do this is to add two indexes to your table. The first starting with 1, then the 2nd starting with 0, then merge the table with itself using the Index1 column in the first table with the Index0 in the 2nd table.