Search code examples
kdb

Get start and end date of previous quarter in q kdb


We have a requirement where we need start and end date of the previous quarter.
To get the startData(sd) and endDate(ed), we can achieve this by below code but is there any better/cleaner/efficient way to do this.

sd:"d"$("m"$3 xbar "m"$ .z.d)-3;
ed:("d"$3 xbar "m"$ .z.d)-1;

Solution

  • I think your solution is good enough. You can get rid of extra month casting from the start date and rearrange the code to get rid of the brackets.

    q) sd:"d"$ -3+3 xbar "m"$ .z.d
    q) ed: -1 + "d"$3 xbar "m"$ .z.d
    

    Or you can calculate both in one line:

    q) 0 -1 + "d"$-3 0 + 3 xbar "m"$.z.d
    q) 2019.01.01 2019.03.31