Utsav
Utsav

Reputation: 5918

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;

Upvotes: 0

Views: 1189

Answers (2)

Jonathan
Jonathan

Reputation: 221

Your method is already quite efficient.

q)"d"$("m"$3 xbar "m"$ .z.d)-3
2019.01.01
q)\t:1000000 "d"$("m"$3 xbar "m"$ .z.d)-3
2611

You could however try the following to make it slightly more efficient and cleaner:

q)"d"$-3+3 xbar "m"$.z.d
2019.01.01
q)\t:1000000 "d"$-3+3 xbar "m"$.z.d
2281

Upvotes: 2

Rahul
Rahul

Reputation: 3969

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

Upvotes: 1

Related Questions