# Bar chart - Aggregate by Field1 or Field2

**URL:** https://discuss.elastic.co/t/bar-chart-aggregate-by-field1-or-field2/264379
**Category:** Kibana
**Created:** [February 16, 2021, 4:12am UTC](https://discuss.elastic.co/t/bar-chart-aggregate-by-field1-or-field2/264379 "2021-02-16T04:12:19Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)
#### Post date: [February 16, 2021, 4:12am UTC](https://discuss.elastic.co/t/bar-chart-aggregate-by-field1-or-field2/264379/1 "2021-02-16T04:12:19Z")

</div>

Hello,  
I have documents with 2 fields that I would like to use for aggregation.  
The document will look something like

```auto

"_source" : {
          "name" : "name_value",
          "id" : "id_value",
         ...
		 ...
        }

```

But, some names might be missing/empty.

I would like to show a bar chart (For example) that shows count by name, but if the name is empty present the id instead.

Is this possible? How?  
If not, any alternatives?

Thanks!

---

<div class="post-metadata">

### Author: ![brianseeders](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brianseeders/32/84831_2.png) [@brianseeders](https://discuss.elastic.co/u/brianseeders)
#### Post date: [March 2, 2021, 10:39pm UTC](https://discuss.elastic.co/t/bar-chart-aggregate-by-field1-or-field2/264379/2 "2021-03-02T22:39:19Z")

</div>

Hey @AClerk,

Take a look at [Scripted Fields](https://www.elastic.co/guide/en/kibana/current/scripted-fields.html). I believe these will do what you want. You can do aggregations on them as well.

Note that these are computed on-the-fly, and are potentially more resource-intensive. The better option, if you can do it, is to add another field to the record at index time that contains the correct value. That way, it's already there and indexed by Elasticsearch.

Brian

---

<div class="post-metadata">

### Author: ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)
#### Post date: [March 2, 2021, 11:36pm UTC](https://discuss.elastic.co/t/bar-chart-aggregate-by-field1-or-field2/264379/3 "2021-03-02T23:36:44Z")

</div>

Hi @brianseeders  
Thanks for your reply.  
I am aware of this solution but thought maybe there is a better way to do this.  
Cheers!

---

<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: [March 30, 2021, 11:36pm UTC](https://discuss.elastic.co/t/bar-chart-aggregate-by-field1-or-field2/264379/4 "2021-03-30T23:36:56Z")

</div>

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