How to get a value from the previous row?

Yeah, like I said, I appreciate your effort, but the solution has become too complex to justify. I’ll just wait for a native function. Thanks again!

1 Like

Hi there! I’ve struggled with this same thing for a while and my view is:

  1. We need to mark criteria(s) by which to identify $Previous. It seems that Grist doesn’t see rows/records sequence in a way we see before our very eyes, so we are to tell what is “previous”.
    The most reasonable criteria i think of (currently, but i believe it could be other field like date…) is $id.
    So, in my case i first created another column to numerate rows/recs which are dynamically filtered, yet (obviously) their ids not always consistent or sequential

Numeration column (in the same table):

filtered = MyTable.lookupRecords(Field=$Field).id
ids = sorted(filtered)
inc = 0
for i in range(len(ids)):
  if ids[i] == $id:
    inc += i+1

return inc

Attempt to find previous (based on rec.ids order):

prev = 0
for i in range(len(ids)):
  if i > 0:
    if ids[i] == rec.id:
      prev = (ids[i - 1])

return prev

Then, pin that row by $id:

MyTable.all.MyField[prev]

the above worked for me ))

Sorry, must admit this (above) is not a bulletproof solution, just came across another more elegant one below:
check-value-with-the-previous-one:
Live & learn :))

Hey there!

We just implemented three cumulative functions. Check them out here: Function reference - Grist Help Center

Thanks,
Natalie

1 Like