Compare value with previous row and chache value

Hi! I really need help with the following challange.
In most cases I have this situation in the table in my database:

Column 1 Column 2 Custom expression for custom Column 2
OF OF replace(replace([Column 1], "KY", "700"),"DP","600")
DP 600 replace(replace([Column 1], "KY", "700"),"DP","600")
KY 700 replace(replace([Column 1], "KY", "700"),"DP","600")
DP 600 replace(replace([Column 1], "KY", "700"),"DP","600")
RK RK replace(replace([Column 1], "KY", "700"),"DP","600")

But sometimes i have dat situation in this table:

Column 1 Column 2 Custom expression for custom Column 2
OF OF replace(replace([Column 1], "KY", "700"),"DP","600")
KY 700 replace(replace([Column 1], "KY", "700"),"DP","600")
KY 700 replace(replace([Column 1], "KY", "700"),"DP","600")
KY 700 replace(replace([Column 1], "KY", "700"),"DP","600")
RK RK replace(replace([Column 1], "KY", "700"),"DP","600")

How can I change the value in the second column from 700 to 600 if a different value is specified before KY and at the end if another value is specified after KY?
If before first KY somthing different (not DP) -> KY = 600
If after last KY somthing different (not DP) -> KY = 600
Is it possible to do this through custom expressions?

Column 1 Column 2
TL TL
RB RB
OF OF
KY 600
KY 700
KY 700
KY 600
RK RK
NE NE
DP 600
KY 700
DP 600
TE TE

You need to do some work at the database side rather than in Metabase.
First read up on 'Window Functions', then create a view that does what you need (much easier than writing it in Metabase's query tool).

1 Like