# How To Perform Certain Operations If Filter Is Not Exist in Vega Visualisation

**URL:** <https://discuss.elastic.co/t/how-to-perform-certain-operations-if-filter-is-not-exist-in-vega-visualisation/279866>\
**Category:** Kibana\
**Tags:** vega\
**Created:** [July 28, 2021, 1:29pm UTC](https://discuss.elastic.co/t/how-to-perform-certain-operations-if-filter-is-not-exist-in-vega-visualisation/279866 "2021-07-28T13:29:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankitdevnalkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankitdevnalkar/32/46158_2.png) [@ankitdevnalkar](https://discuss.elastic.co/u/ankitdevnalkar)\
**Post date:** [July 28, 2021, 1:29pm UTC](https://discuss.elastic.co/t/how-to-perform-certain-operations-if-filter-is-not-exist-in-vega-visualisation/279866/1 "2021-07-28T13:29:57Z")

</div>

Is it possible to code Vega in such a way as to perform certain operations `if filter is not applied`?

**objective: if the filter is applied, then perform code given below, if filter is not applied then perform sum of a different field value**

Here is my Vega code for reference :

```auto
{
  $schema: https://vega.github.io/schema/vega/v3.0.json
  data: [
    {
      name: latestTotalBalance
      url: {
        %context%: true
        %timefield%: @timestamp
        index: wallet-info*
        body: {
          aggs: {
            label: {
              terms: {
                field: info.displayName.keyword
                size: 30
                order: {_key: "desc"}
              }
              aggs: {
                balance: {
                  top_hits: {
                    _source: confirmedBalanceUnitAmount
                    size: 2
                    sort: [
                      {
                        @timestamp: {order: "desc"}
                      }
                    ]
                  }
                }
              }
            }
          }
          size: 0
        }
      }
      format: {property: "aggregations.label.buckets"}
      transform: [
        {
          type: aggregate
          ops: ["sum"]
          fields: ["balance.hits.hits[0]._source.confirmedBalanceUnitAmount"]
          as: ["totalUnitAmount"]
        }
      ]
    }
  ]
  marks: [
    {
      type: text
      from: {data: "latestTotalBalance"}
      encode: {
        update: {
          text: {signal: "format(datum.totalUnitAmount, ',')"}
          align: {value: "center"}
          baseline: {value: "middle"}
          xc: {signal: "width/2"}
          yc: {signal: "height/2"}
          fontSize: {signal: "33"}
          "fill": {"value": "white"}
          "fontWeight":{"value": "bold"}
          text: {signal: "format(datum.totalUnitAmount, ',.5f')"}
        }
      }
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![ankitdevnalkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankitdevnalkar/32/46158_2.png) [@ankitdevnalkar](https://discuss.elastic.co/u/ankitdevnalkar)\
**Post date:** [July 29, 2021, 4:59am UTC](https://discuss.elastic.co/t/how-to-perform-certain-operations-if-filter-is-not-exist-in-vega-visualisation/279866/2 "2021-07-29T04:59:06Z")

</div>

it would be a great assistance if you could give your inputs on this @wylie !!

---

<div class="post-metadata">

**Author:** ![Midas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/midas/32/50275_2.png) [@Midas](https://discuss.elastic.co/u/Midas)\
**Post date:** [August 19, 2021, 4:40pm UTC](https://discuss.elastic.co/t/how-to-perform-certain-operations-if-filter-is-not-exist-in-vega-visualisation/279866/3 "2021-08-19T16:40:54Z")

</div>

If I am understanding you right, then you are looking at having valueFiltered show if filter is set or just value if filter is not set. If that is the case, then you would need two data calls; one with your end result that listens to filters and one that does not. So let's have data set name valuesFiltered (listens to %context%) with whatever your value is named as "value" and data set name values with whatever your value is named as "value" (does NOT listen to %context%). Then, in your text mark, you would have "text": {"signal": "data('valuesFiltered')[0].value == data('values')[0].value ? data('values')[0].value : data('valuesFiltered')[0].value"}. In other words, if the filtered value and the non-filtered value equal, then show the non-filtered value. If they differ, then show the filtered value.

In your current code, you have totalUnitAmount as "value" which would be fine, just make sure both data calls are the same input/output, just one has %context% and one does not. And then you update that text code above to .totalUnitAmount instead of .value.

Does that help?

---

<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:** [September 16, 2021, 4:41pm UTC](https://discuss.elastic.co/t/how-to-perform-certain-operations-if-filter-is-not-exist-in-vega-visualisation/279866/4 "2021-09-16T16:41:46Z")

</div>

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