# Last value in painless script

**URL:** <https://discuss.elastic.co/t/last-value-in-painless-script/329298>\
**Category:** Kibana\
**Tags:** painless, visualisation\
**Created:** [April 4, 2023, 9:19am UTC](https://discuss.elastic.co/t/last-value-in-painless-script/329298 "2023-04-04T09:19:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinsbleu](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@martinsbleu](https://discuss.elastic.co/u/martinsbleu)\
**Post date:** [April 4, 2023, 9:19am UTC](https://discuss.elastic.co/t/last-value-in-painless-script/329298/1 "2023-04-04T09:19:15Z")

</div>

Hello Team,

Like in the title, I am trying to get the **last\_value** of a field for the _Return On Investment_ calculation. Normally, I can do this in Lens Formula (rf. below) on **a monthly index**. But I don't know how for **a daily index**.

`ROI = sum(*sessions*) / last_value(*subscription_fee*)`

So the idea is to get the last _subscription\_fee_, I've been trying to play around with _@timestamp_ field but no hope.

How could I tackle that ?  
Thanks in advance,

---

<div class="post-metadata">

**Author:** ![drewdaemon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/drewdaemon/32/97779_2.png) [@drewdaemon](https://discuss.elastic.co/u/drewdaemon)\
**Post date:** [April 11, 2023, 5:49pm UTC](https://discuss.elastic.co/t/last-value-in-painless-script/329298/2 "2023-04-11T17:49:20Z")

</div>

Hi @martinsbleu !

A couple of questions:

1. the ROI formula is `(current value - initial value) / initial value`. Can you clarify how your data are supposed to fit that formula?

2. when you say

> [@martinsbleu](#):
>
> a daily index

do you mean that each document in the index represents the value of the investment on a particular day?

---

<div class="post-metadata">

**Author:** ![martinsbleu](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@martinsbleu](https://discuss.elastic.co/u/martinsbleu)\
**Post date:** [April 18, 2023, 3:32pm UTC](https://discuss.elastic.co/t/last-value-in-painless-script/329298/3 "2023-04-18T15:32:56Z")

</div>

Hello @drewdaemon !  
Thanks you for reply!!

1. Yes, pardon me, `current value` would the total number of sessions (`sessions` field).
2. A daily index is an index with data populated each day. So I think it is close to what you have described (:

---

<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 16, 2023, 3:33pm UTC](https://discuss.elastic.co/t/last-value-in-painless-script/329298/4 "2023-05-16T15:33:31Z")

</div>

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