Forum Discussion

jaco1951's avatar
jaco1951
Helper III
8 years ago
Solved

Create new column with data from another column previous row - using groups of data

Dear all   I am trying to pull the previous row data from another column to create a new column called "FromDate".   The column I already got is called "ToDate", but I need to know from what date...
  • MarcelBeug's avatar
    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"