# Is it possible to make arithmetic operation on kibana dashboard?

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-make-arithmetic-operation-on-kibana-dashboard/330033>\
**Category:** Kibana\
**Tags:** kql-kibana-query-language, dashboard\
**Created:** [April 14, 2023, 5:10pm UTC](https://discuss.elastic.co/t/is-it-possible-to-make-arithmetic-operation-on-kibana-dashboard/330033 "2023-04-14T17:10:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![FTOR](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@FTOR](https://discuss.elastic.co/u/FTOR)\
**Post date:** [April 14, 2023, 5:10pm UTC](https://discuss.elastic.co/t/is-it-possible-to-make-arithmetic-operation-on-kibana-dashboard/330033/1 "2023-04-14T17:10:46Z")

</div>

I would like to visualise the average of a processing time of a file according to its size over time.  
I receive the fields as follows:  
start\_date  
end\_date  
length

So I need to calculate length / (end\_date - start\_date) for each file and calculate the average

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [April 14, 2023, 8:34pm UTC](https://discuss.elastic.co/t/is-it-possible-to-make-arithmetic-operation-on-kibana-dashboard/330033/2 "2023-04-14T20:34:09Z")

</div>

Hi @FTOR

Assuming all those fields are in a single document.

You can use Lens with Formulas .. .make a table or graph pretty easy

Or you could use a Runtime Field .... and create the field based on the calculation and the you could use in visualizations etc.

---

<div class="post-metadata">

**Author:** ![FTOR](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@FTOR](https://discuss.elastic.co/u/FTOR)\
**Post date:** [April 17, 2023, 4:20pm UTC](https://discuss.elastic.co/t/is-it-possible-to-make-arithmetic-operation-on-kibana-dashboard/330033/3 "2023-04-17T16:20:00Z")

</div>

thank you for your response !  
I tried the formula function with Lenz. The Math functions doesn't work with fields. This is an example of the error with divide function. It's the same error with all Math functions  
 ![divide](https://us1.discourse-cdn.com/elastic/original/3X/4/0/4096109f50311407aabf10909fc76deecb33e039.png). And subscribe is only available with numeric fields. Is there a function available with dates ?

Concerning run time field, I add one to an index, but i could not see the result when I do search docs and I could not find the field on dashboard

```auto
*********creation of index*********
PUT /index_example/
{
  "mappings": 
  {
    "runtime": 
    {
      "result_dureeTraitement": {
        "type": "double",
        "script": {
          "source": "emit(doc['count']/(doc['end']-doc['start']))"
        }
      }
    },
    "properties": {
      "@timestamp": {
        "type": "date"
      },
      "count": {
        "type": "integer"
      },
      "end": {
        "type": "date"
      },
      "start": {
        "type": "date"
      }
    }
 }
}

*********post one doc************ 
POST /index_example/_doc
{
	"@timestamp": "2023-04-17T17:53:30",
	"count" : 105500,
	"end" : "2023-04-17T17:53:30",
	"start": "2023-04-17T17:52:30"
}
********result*************

{
  "took" : 518,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "index_example",
        "_id" : "7Cr7j4cB_IrF2OdwhxSj",
        "_score" : 1.0,
        "_source" : {
          "@timestamp" : "2023-04-17T17:53:30",
          "count" : 105500,
          "end" : "2023-04-17T17:53:30",
          "start" : "2023-04-17T17:52:30"
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [April 17, 2023, 5:51pm UTC](https://discuss.elastic.co/t/is-it-possible-to-make-arithmetic-operation-on-kibana-dashboard/330033/4 "2023-04-17T17:51:37Z")

</div>

Hi @FTOR

You are close but missing a few key concepts ...

1 runtime fields are fields not `_source` so if you had run your search

```auto
GET index_example/_search
{
 "fields": ["*"] 
}

```

The result would be

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "emit(doc['count']/(doc['end']-doc['start']))",
          " ^---- HERE"
        ],
        "script": "emit(doc['count']/(doc['end']-doc['start']))",
        "lang": "painless",
        "position": {
          "offset": 33,
          "start": 0,
          "end": 44
        }
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
.....

```

Bad Definition

So now on to the solution / Suggestions

First when you are new to runtime fields use the Data View / Interactive UI to do it.

Remove the index, remove the bad runtime mapping and start over.

Put your mapping in without the runtime field  
Add the Doc  
Create A Data View and Add a Runtime Field

 ![Screenshot 2023-04-17 at 10.33.17 AM](https://us1.discourse-cdn.com/elastic/original/3X/6/5/65a939b20f4e938fd430b93729623ffa4bfc222e.png)

Now you can work on it interactively

Next you need to look at the [painless](https://www.elastic.co/guide/en/elasticsearch/painless/8.7/painless-datetime.html) docs ... [especially around dates](https://www.elastic.co/guide/en/elasticsearch/painless/8.7/painless-api-reference-shared-java-time.html#painless-api-reference-shared-ZonedDateTime) .. it is basically Java

`date`s are `ZonedDateTimes` types

So not sure if you have seconds or millis... there is a couple ways to do it

Here is my code there are several ways, also this does not protect against missing value etc...

```auto
long differenceInMillis = ChronoUnit.MILLIS.between(doc['start'].value, doc['end'].value);
double average = (double) doc['count'].value / differenceInMillis;
emit (average);

```

Now that that works ... you can add it to the actual mapping of you like

 ![Screenshot 2023-04-17 at 10.49.15 AM](https://us1.discourse-cdn.com/elastic/original/3X/e/8/e8720b35b4e43ede3d38576fcbe6bb9230fcbf7b.png)

---

<div class="post-metadata">

**Author:** ![FTOR](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@FTOR](https://discuss.elastic.co/u/FTOR)\
**Post date:** [April 18, 2023, 11:16am UTC](https://discuss.elastic.co/t/is-it-possible-to-make-arithmetic-operation-on-kibana-dashboard/330033/5 "2023-04-18T11:16:42Z")

</div>

Hi @stephenb, thank you very much ! It works great !

---

<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, 11:17am UTC](https://discuss.elastic.co/t/is-it-possible-to-make-arithmetic-operation-on-kibana-dashboard/330033/6 "2023-05-16T11:17:31Z")

</div>

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