# How to calculate the date of max/min value in a number of days?

**URL:** <https://forum.kx.com/t/how-to-calculate-the-date-of-max-min-value-in-a-number-of-days/9244>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [November 16, 2014, 6:28am UTC](https://forum.kx.com/t/how-to-calculate-the-date-of-max-min-value-in-a-number-of-days/9244 "2014-11-16T06:28:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francisco1](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@Francisco1](https://forum.kx.com/u/Francisco1)\
**Post date:** [November 16, 2014, 6:28am UTC](https://forum.kx.com/t/how-to-calculate-the-date-of-max-min-value-in-a-number-of-days/9244/1 "2014-11-16T06:28:00Z")

</div>

Hello, I have a table t with historical financial data:

D1Bar | OpenAsk

----------| --------

2013.07.26| 1680.4

2013.07.29| 1686.5

2013.07.30| 1690.5

2013.07.31| 1693.9

2013.08.01| 1703.8

2013.08.02| 1703.7

2013.08.04| 1706.1

2013.08.05| 1707.1

2013.08.06| 1696.2

2013.08.07| 1690.2

2013.08.08| 1698.9

2013.08.09| 1692.9

2013.08.11| 1687.6

2013.08.12| 1684.3

2013.08.13| 1691.3

And I need to add a column containing the date when OpenAsk was the maximum/minimum value in the last n days.

An example with n=5 (calculated by hand);

D1Bar | OpenAsk Max5Date Min5Date

----------| ------------------------------

2013.07.26| 1680.4 2013.07.26 2013.07.26

2013.07.29| 1686.5 2013.07.29 2013.07.26

2013.07.30| 1690.5 2013.07.30 2013.07.26

2013.07.31| 1693.9 2013.07.31 2013.07.26

2013.08.01| 1703.8 2013.08.01 2013.07.26

2013.08.02| 1703.7 2013.08.01 2013.07.29

2013.08.04| 1706.1 2013.08.04 2013.07.30

2013.08.05| 1707.1 2013.08.05 2013.07.31

2013.08.06| 1696.2 2013.08.05 2013.08.06

2013.08.07| 1690.2 2013.08.05 2013.08.07

2013.08.08| 1698.9 2013.08.05 2013.08.07

2013.08.09| 1692.9 2013.08.05 2013.08.07

2013.08.11| 1687.6 2013.08.08 2013.08.11

2013.08.12| 1684.3 2013.08.08 2013.08.12

2013.08.13| 1691.3 2013.08.08 2013.08.12

How can it be done?

Thanks in advance

Francisco

---

<div class="post-metadata">

**Author:** ![kuentang](https://avatars.discourse-cdn.com/v4/letter/k/bc8723/32.png) [@kuentang](https://forum.kx.com/u/kuentang)\
**Post date:** [November 16, 2014, 7:20am UTC](https://forum.kx.com/t/how-to-calculate-the-date-of-max-min-value-in-a-number-of-days/9244/2 "2014-11-16T07:20:00Z")

</div>

q)D1Bar:2013.07.26 2013.07.29 2013.07.30 2013.07.31 2013.08.01 2013.08.02 2013.08.04 2013.08.05 2013.08.06 2013.08.07 2013.08.08 2013.08.09 2013.08.11 2013.08.12 2013.08.13\<o:p\>\</o:p\>

q)OpenAsk:1680.4 1686.5 1690.5 1693.9 1703.8 1703.7 1706.1 1707.1 1696.2 1690.2 1698.9 1692.9 1687.6 1684.3 1691.3\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

q)t:(D1Bar;OpenAsk)\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

q)f:{l@'(first z@) @/:y l:neg#/:,[til count y]}\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

q)update max5Date:D1Bar f[5;OpenAsk;idesc],Min5Date:D1Bar f[5;OpenAsk;iasc] from t\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

HTH,\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

Kim\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

**Von:** [personal-kdbplus@googlegroups.com](mailto:personal-kdbplus@googlegroups.com) [[mailto:personal-kdbplus@googlegroups.com](mailto:personal-kdbplus@googlegroups.com)] **Im Auftrag von** Francisco  
**Gesendet:** Sonntag, 16. November 2014 14:29  
**An:** [personal-kdbplus@googlegroups.com](mailto:personal-kdbplus@googlegroups.com)  
**Betreff:** [personal kdb+] How to calculate the date of max/min value in a number of days?\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

Hello, I have a table t with historical financial data:\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

D1Bar &nbsp; &nbsp; | OpenAsk\<o:p\>\</o:p\>

----------| --------\<o:p\>\</o:p\>

2013.07.26| 1680.4\<o:p\>\</o:p\>

2013.07.29| 1686.5\<o:p\>\</o:p\>

2013.07.30| 1690.5&nbsp;\<o:p\>\</o:p\>

2013.07.31| 1693.9\<o:p\>\</o:p\>

2013.08.01| 1703.8&nbsp;\<o:p\>\</o:p\>

2013.08.02| 1703.7\<o:p\>\</o:p\>

2013.08.04| 1706.1\<o:p\>\</o:p\>

2013.08.05| 1707.1&nbsp;\<o:p\>\</o:p\>

2013.08.06| 1696.2&nbsp;\<o:p\>\</o:p\>

2013.08.07| 1690.2\<o:p\>\</o:p\>

2013.08.08| 1698.9\<o:p\>\</o:p\>

2013.08.09| 1692.9\<o:p\>\</o:p\>

2013.08.11| 1687.6\<o:p\>\</o:p\>

2013.08.12| 1684.3\<o:p\>\</o:p\>

2013.08.13| 1691.3\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

And I need to add a column containing the date when OpenAsk was the maximum/minimum value in the last n days.\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

An example with n=5 (calculated by hand);\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

D1Bar &nbsp; &nbsp; | OpenAsk &nbsp;Max5Date &nbsp; &nbsp;Min5Date&nbsp;\<o:p\>\</o:p\>

----------| ------------------------------\<o:p\>\</o:p\>

2013.07.26| 1680.4 &nbsp;2013.07.26 &nbsp;2013.07.26\<o:p\>\</o:p\>

2013.07.29| 1686.5 &nbsp;2013.07.29 &nbsp;2013.07.26\<o:p\>\</o:p\>

2013.07.30| 1690.5 &nbsp;2013.07.30 &nbsp;2013.07.26&nbsp;\<o:p\>\</o:p\>

2013.07.31| 1693.9 &nbsp;2013.07.31 &nbsp;2013.07.26\<o:p\>\</o:p\>

2013.08.01| 1703.8 &nbsp;2013.08.01 &nbsp;2013.07.26\<o:p\>\</o:p\>

2013.08.02| 1703.7 &nbsp;2013.08.01 &nbsp;2013.07.29\<o:p\>\</o:p\>

2013.08.04| 1706.1 &nbsp;2013.08.04 &nbsp;2013.07.30\<o:p\>\</o:p\>

2013.08.05| 1707.1 &nbsp;2013.08.05 &nbsp;2013.07.31\<o:p\>\</o:p\>

2013.08.06| 1696.2 &nbsp;2013.08.05 &nbsp;2013.08.06\<o:p\>\</o:p\>

2013.08.07| 1690.2 &nbsp;2013.08.05 &nbsp;2013.08.07\<o:p\>\</o:p\>

2013.08.08| 1698.9 &nbsp;2013.08.05 &nbsp;2013.08.07\<o:p\>\</o:p\>

2013.08.09| 1692.9 &nbsp;2013.08.05 &nbsp;2013.08.07\<o:p\>\</o:p\>

2013.08.11| 1687.6 &nbsp;2013.08.08 &nbsp;2013.08.11\<o:p\>\</o:p\>

2013.08.12| 1684.3 &nbsp;2013.08.08 &nbsp;2013.08.12\<o:p\>\</o:p\>

2013.08.13| 1691.3 &nbsp;2013.08.08 &nbsp;2013.08.12\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

How can it be done?\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

Thanks in advance\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

Francisco\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

–  
Submitted via Google Groups

---

<div class="post-metadata">

**Author:** ![Francisco1](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@Francisco1](https://forum.kx.com/u/Francisco1)\
**Post date:** [November 16, 2014, 12:12pm UTC](https://forum.kx.com/t/how-to-calculate-the-date-of-max-min-value-in-a-number-of-days/9244/3 "2014-11-16T12:12:00Z")

</div>

Thanks a lot!

Francisco
