I'm not asking how the BFL function works, I'm just asking what is the structure (columns) and what is the data you need in those input tables, based on that sample input table you mentioned above.
Based on that, we could propose a calc/analytic view that could represent that in a more performatic manner than your "cursor".