Forum Discussion

TuqueLogic's avatar
TuqueLogic
Icon for Helper I rankHelper I
1 year ago
Solved

Power Query Recursion: Simple reference of same row

Just looking to do a simple recursive reference but Power Query doesn't seem to be simple with recursion. Table with 3 columns:  Index,  Date,  AdjustedDate Date is a datetime var, imported da...
  • MarkLaf's avatar
    MarkLaf
    1 year ago

    Maybe this is what you are looking for. Just a note, though, that recursions usually perform very badly in Power Query and there is usually a way more performant, non-recursive method to get the same results. This is partly why lots of people are giving you non-recursive solutions.

     

    SourceData:

    IndexDate
    05/1/2025
    15/12/2025
    24/1/2025
    34/1/2025
    44/1/2025
    55/23/2025
    65/19/2025
    76/1/2025
    85/28/2025

     

     

     

    let
        Source = SourceData,
        
        AddPreviousHigher = 
        let 
            srcBuf = Table.Buffer( Source )
        in 
            Table.AddColumn(
                srcBuf, 
                "PreviousHigherDate", 
                each let 
                    recF = ( x as number ) => 
                    let thisDate =  srcBuf{x}[Date] in
                    if x = 0 
                        then thisDate 
                        else let prevDate = srcBuf{x-1}[Date] in 
                            if thisDate > prevDate
                                then thisDate 
                                else @recF( x - 1) //recursion
                in 
                    recF([Index]),
                type date
            )
    in
        AddPreviousHigher

     

     

    Output:

     

    Edit: realized I got the recursive logic wrong, tweaked to reflect OP's clarificaiton