Search code examples
kdb

Populate null value with preceding values for multiple columns without specifying column names


I have a table containing 30+ columns, each of which has quite a few null values for records. To fill null values with preceding values, I use fills function and specify each column name in update statement as follows at the moment. (The below example has 4 columns only for the purpose of explanation)

      trade:([]date:raze 3#enlist .z.d+til 10; ccy:30#`EUR`JPY`AUD; fx:30?(1.0 0.2 0.4 0n); mult:30?(10.1 0n 30.3 0n));
      update fills fx, fills mult by ccy from trade;

When it comes to a lot more columns, it gets cumbersome to specify each column name in the same way (fills col1, fills col2, fills col3...). Don't we have a way to fill the null values for all the columns of a table more easily?

Thank you very much for your help in advance!


Solution

  • You could try this functional update

    ![trade;();(enlist`ccy)!enlist`ccy;{x!fills,'x}cols trade]
    

    This will forward fill all columns in trade. To be more specific about which columns you want to forward fill, you could save these columns as a variable and use them like so

    q)cls:`fx`mult
    q)![trade;();(enlist`ccy)!enlist`ccy;{x!fills,'x}cls]
    date       ccy fx  mult
    -----------------------
    2020.10.25 EUR 1   10.1
    2020.10.26 JPY 1   10.1
    2020.10.27 AUD 0.2 30.3
    2020.10.28 EUR 0.4 10.1
    2020.10.29 JPY 0.2 30.3
    ..