# Problem with Top N in TSVB

**URL:** https://discuss.elastic.co/t/problem-with-top-n-in-tsvb/278433
**Category:** Kibana
**Created:** [July 12, 2021, 2:06pm UTC](https://discuss.elastic.co/t/problem-with-top-n-in-tsvb/278433 "2021-07-12T14:06:26Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![LL\_FED](https://avatars.discourse-cdn.com/v4/letter/l/e47c2d/32.png) [@LL\_FED](https://discuss.elastic.co/u/LL_FED)
#### Post date: [July 12, 2021, 2:06pm UTC](https://discuss.elastic.co/t/problem-with-top-n-in-tsvb/278433/1 "2021-07-12T14:06:26Z")

</div>

Hi everyone,

i have two problems with the top N function in the TSVB.

Problem1: If I use "Group by" **Terms** and i set the value of **Top** to for example 50, but i only have 30 different terms, than the TopN function doesn't work.

Problem2: For example i use "top = 5" then i get data like this:

M05 30°C  
M01 28°C  
M03 26°C  
M04 20°C  
M02 18°C

perfekt! But when i decrease the top value to for example "top = 4", it should just "delete" the last entry (M02 18°C), but it deletes M04, which doesn't make sense.

Thanks for any kind of help!

---

<div class="post-metadata">

### Author: ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)
#### Post date: [July 12, 2021, 8:46pm UTC](https://discuss.elastic.co/t/problem-with-top-n-in-tsvb/278433/2 "2021-07-12T20:46:18Z")

</div>

I understand why you might not expect this behavior, it is different from how non-Elasticsearch products work. I will both explain why you are seeing these numbers and tell you two workarounds you can use. The workarounds won't make sense unless I provide the explanation first.

The way TSVB works is that it uses a _Terms_ aggregation _first_, before splitting into your _Date Histogram_ buckets. So when you are seeing "Top 5", it is ranked based on the Top 5 _for your entire time range_, which means that you'll see the same 5 series in each time bucket. Secondly, the [default behavior of the **Top N** in TSVB is to use **only the last bucket from your date histogram**](https://www.elastic.co/guide/en/kibana/current/tsvb.html#all-chart-types-except-time-series).

Here are the workarounds that I would suggest:

1. Don't use TSVB for this calculation. You can see in our [supported features by panel type](https://www.elastic.co/guide/en/kibana/current/aggregation-reference.html) a full comparison of all the other ways of achieving this visualization. The [unreleased 7.14 feature reference is more detailed](https://www.elastic.co/guide/en/kibana/7.14/aggregation-reference.html) because in the 7.14 release Lens will support most of the TSVB functionality.

2. In TSVB, you can switch to _Entire time range mode_ instead of _Last value mode_. This will make the Terms aggregation behave more like what you are describing.

---

<div class="post-metadata">

### Author: ![LL\_FED](https://avatars.discourse-cdn.com/v4/letter/l/e47c2d/32.png) [@LL\_FED](https://discuss.elastic.co/u/LL_FED)
#### Post date: [July 13, 2021, 6:15am UTC](https://discuss.elastic.co/t/problem-with-top-n-in-tsvb/278433/3 "2021-07-13T06:15:08Z")

</div>

Thanks for your answer.  
The reason i use the Top N in TSVB is because I need the Tiny-math function and as far as I know, only the tsvb allows to make simple calculations.

Here you can see my aggregations:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/8/58bf39e08976b22341c05c04218148c8acc78393.png)

I think another problem is that I can't **Order by** the tiny-math calculation.

I can only order by Aggregation 1 (Average) or Aggregation 2 (Average) but not with the Aggregation (Math)

---

<div class="post-metadata">

### Author: ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)
#### Post date: [July 13, 2021, 2:25pm UTC](https://discuss.elastic.co/t/problem-with-top-n-in-tsvb/278433/4 "2021-07-13T14:25:07Z")

</div>

I see, so you might be able to use the Entire Time Range workaround that I suggested.

If that doesn't work, you will have to read our reference documentation to choose the right editor. In 7.13, the right editor might be Vega. In 7.14, Lens will offer "Formulas" which are more powerful than TSVB.

See the [descriptions of each editor in 7.13](https://www.elastic.co/guide/en/kibana/7.13/create-panels-with-editors.html) and [in 7.14](https://www.elastic.co/guide/en/kibana/7.14/create-panels-with-editors.html), as well as the [list of features in 7.13](https://www.elastic.co/guide/en/kibana/7.13/aggregation-reference.html#other-features) and [the list of features in 7.14](https://www.elastic.co/guide/en/kibana/7.14/aggregation-reference.html#other-features).

---

<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: [August 10, 2021, 2:25pm UTC](https://discuss.elastic.co/t/problem-with-top-n-in-tsvb/278433/5 "2021-08-10T14:25:28Z")

</div>

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