Forum Discussion
Create new column with data from another column previous row - using groups of data
- 8 years ago
I would do it in Power Query. :smileyhappy:
In general, the trick to get data from the previous row on the current row, is to add indices (starting with 0 and 1) and then merging the table with itself, like in the query below.
In this case, also the records with null percentages are temporarily removed and later added back.
Notice that #"Added Index" continues with "RemovedNulls"; "RemovedRecords" contains the records that will later be added back.
let Source = Securities, AddedOriginalSort = Table.AddIndexColumn(Source, "OriginalSort", 1, 1), RemovedNulls = Table.SelectRows(AddedOriginalSort, each ([InterestRate] <> null)), RemovedRecords = Table.SelectRows(AddedOriginalSort, each ([InterestRate] = null)), #"Added Index" = Table.AddIndexColumn(RemovedNulls, "Index", 0, 1), #"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 1, 1), #"Merged Queries" = Table.NestedJoin(#"Added Index1",{"Index"},#"Added Index1",{"Index.1"},"Previous",JoinKind.LeftOuter), #"Expanded Previous" = Table.ExpandTableColumn(#"Merged Queries", "Previous", {"Security", "ToDate"}, {"Previous.Security", "Previous.ToDate"}), #"Sorted Rows" = Table.Sort(#"Expanded Previous",{{"Index", Order.Ascending}}), #"Added Custom" = Table.AddColumn(#"Sorted Rows", "FromDate", each if [Previous.Security] <> [Security] then [IssuedDate] else Date.AddDays([Previous.ToDate],1), type date), #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index", "Index.1", "Previous.Security", "Previous.ToDate"}), #"Appended Query" = Table.Combine({#"Removed Columns", RemovedRecords}), #"Sorted Rows1" = Table.Sort(#"Appended Query",{{"OriginalSort", Order.Ascending}}), #"Removed Columns1" = Table.RemoveColumns(#"Sorted Rows1",{"OriginalSort"}) in #"Removed Columns1"
I would do it in Power Query. :smileyhappy:
In general, the trick to get data from the previous row on the current row, is to add indices (starting with 0 and 1) and then merging the table with itself, like in the query below.
In this case, also the records with null percentages are temporarily removed and later added back.
Notice that #"Added Index" continues with "RemovedNulls"; "RemovedRecords" contains the records that will later be added back.
let
Source = Securities,
AddedOriginalSort = Table.AddIndexColumn(Source, "OriginalSort", 1, 1),
RemovedNulls = Table.SelectRows(AddedOriginalSort, each ([InterestRate] <> null)),
RemovedRecords = Table.SelectRows(AddedOriginalSort, each ([InterestRate] = null)),
#"Added Index" = Table.AddIndexColumn(RemovedNulls, "Index", 0, 1),
#"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 1, 1),
#"Merged Queries" = Table.NestedJoin(#"Added Index1",{"Index"},#"Added Index1",{"Index.1"},"Previous",JoinKind.LeftOuter),
#"Expanded Previous" = Table.ExpandTableColumn(#"Merged Queries", "Previous", {"Security", "ToDate"}, {"Previous.Security", "Previous.ToDate"}),
#"Sorted Rows" = Table.Sort(#"Expanded Previous",{{"Index", Order.Ascending}}),
#"Added Custom" = Table.AddColumn(#"Sorted Rows", "FromDate", each if [Previous.Security] <> [Security] then [IssuedDate] else Date.AddDays([Previous.ToDate],1), type date),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index", "Index.1", "Previous.Security", "Previous.ToDate"}),
#"Appended Query" = Table.Combine({#"Removed Columns", RemovedRecords}),
#"Sorted Rows1" = Table.Sort(#"Appended Query",{{"OriginalSort", Order.Ascending}}),
#"Removed Columns1" = Table.RemoveColumns(#"Sorted Rows1",{"OriginalSort"})
in
#"Removed Columns1"
- jaco19518 years agoHelper III
Thank you very much!!!
Excellent solution. I have spend some time studying it now, making it fit into my data load, and it seems to work very well.