# Weighted Average in a DataTable of Kibana

**URL:** <https://discuss.elastic.co/t/weighted-average-in-a-datatable-of-kibana/185909>\
**Category:** Kibana\
**Created:** [June 14, 2019, 8:47pm UTC](https://discuss.elastic.co/t/weighted-average-in-a-datatable-of-kibana/185909 "2019-06-14T20:47:41Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![cjcenizal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjcenizal/32/11216_2.png) [@cjcenizal](https://discuss.elastic.co/u/cjcenizal)\
**Post date:** [June 18, 2019, 7:16pm UTC](https://discuss.elastic.co/t/weighted-average-in-a-datatable-of-kibana/185909/2 "2019-06-18T19:16:54Z")

</div>

Hi Henrique, I think solving your problem will be a two-step process.

1. You'll need to create a per-document field for `cost * volume`, e.g. a field called `volumeCost`. You could create this field two ways, either by [reindexing your data](https://www.elastic.co/guide/en/elasticsearch/reference/6.0/docs-reindex.html#docs-reindex) and adding the field with a Painless script, or by creating a [scripted field](https://www.elastic.co/guide/en/kibana/current/scripted-fields.html) that calculates this value on the fly. The first option is a little more performant when querying your data but the second option might be easier.

2. I think you can create a Timeseries visualization in the Visualize app to generate the table you want. In the screenshot below, imagine the first aggregation for `products.base_price` is actually a reference to the `volumeCost` field and the second aggregation for `products.quantity` is actually a reference to the `volume` field. This way you can define a bucket script that performs the calculation you want, and order the results by product ID or name in a table.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/8/4/845b6886d3f4966095da3ddf94e75688c16f8bca.png)

---

_[View the full topic](https://discuss.elastic.co/t/weighted-average-in-a-datatable-of-kibana/185909)._
