# How can we calculate day of year using Math Aggregation in Kibana 6.5?

**URL:** https://discuss.elastic.co/t/how-can-we-calculate-day-of-year-using-math-aggregation-in-kibana-6-5/177102
**Category:** Kibana
**Created:** [April 16, 2019, 2:12pm UTC](https://discuss.elastic.co/t/how-can-we-calculate-day-of-year-using-math-aggregation-in-kibana-6-5/177102 "2019-04-16T14:12:44Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Sonam\_Verma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sonam_verma/32/43505_2.png) [@Sonam\_Verma](https://discuss.elastic.co/u/Sonam_Verma)
#### Post date: [April 16, 2019, 2:12pm UTC](https://discuss.elastic.co/t/how-can-we-calculate-day-of-year-using-math-aggregation-in-kibana-6-5/177102/1 "2019-04-16T14:12:44Z")

</div>

Hi,  
I want the output of final metric to be displayed under Kibana Metric Visual Builder. I am stucked in calculating dayOfYear() using Math Aggregation in Kibana. Please let me know how can we use expressions like dayOfYear() in Visual Builder Math Aggregation. If not possible, then what are the other options?

I am attaching the snip of my calculation. Thanks in Advance!

 ![Kibana_Issue](https://us1.discourse-cdn.com/elastic/original/3X/b/d/bd23076acb1b4fb1be8cf0dcd218068783976a43.png)

---

<div class="post-metadata">

### Author: ![tiagocosta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tiagocosta/32/40045_2.png) [@tiagocosta](https://discuss.elastic.co/u/tiagocosta)
#### Post date: [April 16, 2019, 4:45pm UTC](https://discuss.elastic.co/t/how-can-we-calculate-day-of-year-using-math-aggregation-in-kibana-6-5/177102/2 "2019-04-16T16:45:39Z")

</div>

@Sonam_Verma do you just wanna get the current day of the year? Maybe we can try something like `Calendar.getInstance().get(Calendar.DAY_OF_YEAR)`?

---

<div class="post-metadata">

### Author: ![Sonam\_Verma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sonam_verma/32/43505_2.png) [@Sonam\_Verma](https://discuss.elastic.co/u/Sonam_Verma)
#### Post date: [April 17, 2019, 7:26am UTC](https://discuss.elastic.co/t/how-can-we-calculate-day-of-year-using-math-aggregation-in-kibana-6-5/177102/3 "2019-04-17T07:26:54Z")

</div>

> [@tiagocosta](#):
>
> `Calendar.getInstance().get(Calendar.DAY_OF_YEAR)`

@tiagocosta The Expression field under Math Aggregation is not taking any function and throwing error.

---

<div class="post-metadata">

### Author: ![tiagocosta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tiagocosta/32/40045_2.png) [@tiagocosta](https://discuss.elastic.co/u/tiagocosta)
#### Post date: [April 17, 2019, 3:52pm UTC](https://discuss.elastic.co/t/how-can-we-calculate-day-of-year-using-math-aggregation-in-kibana-6-5/177102/4 "2019-04-17T15:52:48Z")

</div>

@Sonam_Verma what about creating a scripting field with that expression?

---

<div class="post-metadata">

### Author: ![Sonam\_Verma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sonam_verma/32/43505_2.png) [@Sonam\_Verma](https://discuss.elastic.co/u/Sonam_Verma)
#### Post date: [April 19, 2019, 6:04am UTC](https://discuss.elastic.co/t/how-can-we-calculate-day-of-year-using-math-aggregation-in-kibana-6-5/177102/5 "2019-04-19T06:04:29Z")

</div>

@tiagocosta OK so u mean that I should create a script field first. Got it.. but how can I access that scripted field here?? Can you please elaborate with an example?

---

<div class="post-metadata">

### Author: ![tiagocosta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tiagocosta/32/40045_2.png) [@tiagocosta](https://discuss.elastic.co/u/tiagocosta)
#### Post date: [April 19, 2019, 4:03pm UTC](https://discuss.elastic.co/t/how-can-we-calculate-day-of-year-using-math-aggregation-in-kibana-6-5/177102/6 "2019-04-19T16:03:29Z")

</div>

@Sonam_Verma I have checked this question with the respective team and I'm afraid we would not be able to do it with a Math aggregation. The math aggregation computes everything on client side and you can only use the tinymath functions. You have an interesting parameter that is `params._timestamp` that represent the UTC in millis but you don’t have any date library to handle that unfortunately. Bucket script on the other hands let’s you use the full painless script capabilities but I’m not sure if you can use that for what you want to achieve.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [May 17, 2019, 4:03pm UTC](https://discuss.elastic.co/t/how-can-we-calculate-day-of-year-using-math-aggregation-in-kibana-6-5/177102/7 "2019-05-17T16:03:37Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
