Hi,
You can use this admittedly complex formula (perhaps someone will present an easier one) in a new column:
Eval(
Eval Expr(
:Val[(:Group << get data table) << get rows where(
Col Cumulative Sum( 1, :Group ) == 2 & :Group == Expr( :Group[Row()] )
)] / :Val
)
)
It is probably worth unpacking this at least a little.
The Eval( Eval Expr( )) construct is used to force evaluation of the expression Expr ( :Group[Row()] ). Without this, several values will be returned on each row... one for each group. Try it by removing the Eval ( Eval Expr ()) wrapper and the Expr() wrapper to see what I mean.
To determine the value we'll use in the ratio, we need to grab the 2nd row within the current group. To find this row, we must match on group (the second part of the get rows where clause) and must also match as the 2nd value within the group. An easy way to do this is to look at the cumulative sum of a column of 1s, grouped by :Group. When this column == 2, we have the 2nd value of some group. This happens in several rows, but the 2nd part of the clause restricts the result to the group matching the group of the current row. In this way, the table doesn't even need to be sorted by group or value.
Changing the 2 in the cumulative sum equivalence to k, allows you to use the kth value in the group instead of the 2nd.
Cheers,
Brady