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.
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
BackToTable
This 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.
- sprotson5 years agoHelper I
Thanks
I understand that the merge formula detaisl the names of the columns, but surely the execution of the join is on the contents of those columns - so strange you could not adjust the formula in this way, as I could in other tools
For a novice like myself, I think the best approiach is going to be the indexes - easy to understand, although technically what it is going to do is exclude any item that is at version 1, as there is no previous version to match against.
I think ultimately, the best option is going to make sure I get the data in the correct format before it reaches Power BI. I can easily join a table on itself in sql as I am tryign to do here.
I was hopeful I could easily do it in Power BI, in case I am not using a sql db, but the formula you provided is way above my knowledge currently
I can apply it as a new source easily, but to be hinest, I am not sure how I would apply it in a real world example. ie how do I apply it to a query I already have and specifiy what columns should move down a row etc
Thanks your help anyway