# Vega: Handle a situation where data-set returned is empty

**URL:** https://discuss.elastic.co/t/vega-handle-a-situation-where-data-set-returned-is-empty/262210
**Category:** Kibana
**Tags:** vega
**Created:** [January 26, 2021, 9:22am UTC](https://discuss.elastic.co/t/vega-handle-a-situation-where-data-set-returned-is-empty/262210 "2021-01-26T09:22:03Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![ege](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ege/32/80438_2.png) [@ege](https://discuss.elastic.co/u/ege)
#### Post date: [January 26, 2021, 9:22am UTC](https://discuss.elastic.co/t/vega-handle-a-situation-where-data-set-returned-is-empty/262210/1 "2021-01-26T09:22:03Z")

</div>

When querying for data, there are situations where the returned data-set is empty for certain time ranges. Such a situation occurred in **Vega** (returned an empty array) and I tried to create a condition using **expressions** to check if the data-set is empty but it doesn't seem to work.

`length(data('table')) == 0 ? '-' : datum.sum`

Adding the above test condition in a **formula transform** says `Undefined data set name: "table"`. Nothing seems to change when it is used inside a **mark** _as a signal for a field_.

How to check if the returned data-set is empty so that a condition can be applied to do a specific task accordingly? Your kind help would be appreciated.

**Note:** Vega v 4.3.0

---

<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: [January 26, 2021, 5:18pm UTC](https://discuss.elastic.co/t/vega-handle-a-situation-where-data-set-returned-is-empty/262210/2 "2021-01-26T17:18:34Z")

</div>

It seems like you don't have the right name for your dataset. Are you sure that you named it `table`?

---

<div class="post-metadata">

### Author: ![ege](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ege/32/80438_2.png) [@ege](https://discuss.elastic.co/u/ege)
#### Post date: [January 26, 2021, 10:11pm UTC](https://discuss.elastic.co/t/vega-handle-a-situation-where-data-set-returned-is-empty/262210/3 "2021-01-26T22:11:30Z")

</div>

Thank you for replying @wylie!

Yes, the name of the data-set is `table`. I can still debug it using the name `table` to find the results are empty. The message above,

> [@ege](#):
>
> Undefined data set name: "table"

comes when I add the condition inside **formula transform** only.

---

<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: [January 27, 2021, 12:21am UTC](https://discuss.elastic.co/t/vega-handle-a-situation-where-data-set-returned-is-empty/262210/4 "2021-01-27T00:21:43Z")

</div>

Interesting, would you mind sharing the full spec you're working with when you have missing data? [https://www.elastic.co/guide/en/kibana/master/vega.html#asking-for-help-with-a-vega-spec](https://www.elastic.co/guide/en/kibana/master/vega.html#asking-for-help-with-a-vega-spec)

---

<div class="post-metadata">

### Author: ![ege](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ege/32/80438_2.png) [@ege](https://discuss.elastic.co/u/ege)
#### Post date: [January 27, 2021, 5:58am UTC](https://discuss.elastic.co/t/vega-handle-a-situation-where-data-set-returned-is-empty/262210/5 "2021-01-27T05:58:19Z")

</div>

The `Inspect` feature is disabled in the editor. It says `This visualization doesn't support any inspectors`. Could it be because it is the free version I'm using?

---

<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: [January 27, 2021, 3:17pm UTC](https://discuss.elastic.co/t/vega-handle-a-situation-where-data-set-returned-is-empty/262210/6 "2021-01-27T15:17:49Z")

</div>

I see, that just means you're not using 7.10: we made Vega GA in 7.10 and added the Inspect feature.

In older versions, you can open the browser console and type `VEGA_DEBUG.vegalite_spec`. Copy that.

---

<div class="post-metadata">

### Author: ![ege](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ege/32/80438_2.png) [@ege](https://discuss.elastic.co/u/ege)
#### Post date: [January 27, 2021, 5:43pm UTC](https://discuss.elastic.co/t/vega-handle-a-situation-where-data-set-returned-is-empty/262210/7 "2021-01-27T17:43:20Z")

</div>

Alright, I used the `VEGA_DEBUG.vega_spec` and mentioned below is the full spec it shows. I hope I did it right. 😅

```
VEGA_DEBUG.vega_spec
{$schema: "https://vega.github.io/schema/vega/v5.json", width: 300, height: 300, autosize: "none", background: "#646464", …}$schema: "https://vega.github.io/schema/vega/v5.json"autosize: "none"background: "#646464"config: arc: {fill: "#54B399"}area: {fill: "#54B399"}line: {stroke: "#54B399"}path: {stroke: "#54B399"}range: category: {scheme: "elastic"}scheme: "elastic" __proto__ : Object __proto__ : Objectrect: {fill: "#54B399"}rule: {stroke: "#54B399"}shape: {stroke: "#54B399"}symbol: {fill: "#54B399"}trail: {fill: "#54B399"} __proto__ : Objectdata: Array(1)0: format: {property: "hits.hits"}name: "table"transform: Array(4)0: {type: "project", fields: Array(1), as: Array(1)}1: {type: "formula", as: "elapsedTime", expr: "datum.elapsedTime/1000"}2: {type: "aggregate", fields: Array(1), ops: Array(1), as: Array(1)}3: {type: "formula", as: "sum", expr: "format(datum.sum, ',.3f')"}length: 4 __proto__ : Array(0)values: hits: hits: Array(0)length: 0 __proto__ : Array(0)max_score: nulltotal: 0 __proto__ : Objecttimed_out: falsetook: 2_shards: {total: 2, successful: 2, skipped: 1, failed: 0} __proto__ : Object __proto__ : Objectlength: 1 __proto__ : Array(0)description: "Metric"height: 300marks: Array(1)0: encode: enter: align: {value: "center"}baseline: {value: "middle"}fill: {value: "black"}fontSize: {value: 30}fontWeight: {value: "bold"}text: {signal: "length(data('table')) == 1 ? datum.sum : '-'"}x: {signal: "width/2"}y: {signal: "height*0.45"} __proto__ : Object __proto__ : Objectfrom: {data: "table"}name: "metric"type: "text" __proto__ : Objectlength: 1 __proto__ : Array(0)width: 300 __proto__ : Object

```

**Note:** This spec is extracted by re-writing the same use-case using Elastic v 7.9.3 where the Vega version used is v 5.13.0. But still I **cannot** use this version either to handle a situation where there are no documents in the query results. However, it doesn't show the previous

> [@ege](#):
>
> Undefined data set name: "table"

message when `length(data('table')) == 0` condition is used in a **formula transform**.

---

<div class="post-metadata">

### Author: ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)
#### Post date: [January 27, 2021, 6:51pm UTC](https://discuss.elastic.co/t/vega-handle-a-situation-where-data-set-returned-is-empty/262210/8 "2021-01-27T18:51:38Z")

</div>

Use `JSON.stringify(VEGA_DEBUG.vega_spec)` and copy/paste the results.

---

<div class="post-metadata">

### Author: ![ege](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ege/32/80438_2.png) [@ege](https://discuss.elastic.co/u/ege)
#### Post date: [January 28, 2021, 12:39am UTC](https://discuss.elastic.co/t/vega-handle-a-situation-where-data-set-returned-is-empty/262210/9 "2021-01-28T00:39:25Z")

</div>

Yup, didn't know that. 😅

`"{"$schema":"https://vega.github.io/schema/vega/v5.json","width":300,"height":300,"autosize":"none","background":"#646464","description":"Metric","data":[{"name":"table","format":{"property":"hits.hits"},"transform":[{"type":"project","fields":["fields.elapsedTime"],"as":["elapsedTime"]},{"type":"formula","as":"elapsedTime","expr":"datum.elapsedTime/1000"},{"type":"aggregate","fields":["elapsedTime"],"ops":["sum"],"as":["sum"]},{"type":"formula","as":"sum","expr":"format(datum.sum, ',.3f')"}],"values":{"took":3,"timed_out":false,"_shards":{"total":2,"successful":2,"skipped":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}}],"marks":[{"name":"metric","type":"text","from":{"data":"table"},"encode":{"enter":{"x":{"signal":"width/2"},"y":{"signal":"height*0.45"},"align":{"value":"center"},"baseline":{"value":"middle"},"text":{"signal":"length(data('table')) == 1 ? datum.sum : '-'"},"fontSize":{"value":30},"fontWeight":{"value":"bold"},"fill":{"value":"black"}}}}],"config":{"range":{"category":{"scheme":"elastic"}},"arc":{"fill":"#54B399"},"area":{"fill":"#54B399"},"line":{"stroke":"#54B399"},"path":{"stroke":"#54B399"},"rect":{"fill":"#54B399"},"rule":{"stroke":"#54B399"},"shape":{"stroke":"#54B399"},"symbol":{"fill":"#54B399"},"trail":{"fill":"#54B399"}}}"`

---

<div class="post-metadata">

### Author: ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)
#### Post date: [January 28, 2021, 12:44am UTC](https://discuss.elastic.co/t/vega-handle-a-situation-where-data-set-returned-is-empty/262210/10 "2021-01-28T00:44:32Z")

</div>

Unless I am missing something, I don't see the data in this. Your data request looks like it's empty. You might have to update it again to ensure you are getting data back first.

```auto
      "values": {
        "took": 3,
        "timed_out": false,
        "_shards": {"total": 2, "successful": 2, "skipped": 1, "failed": 0},
        "hits": {"total": 0, "max_score": null, "hits": []}
      }

```

---

<div class="post-metadata">

### Author: ![ege](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ege/32/80438_2.png) [@ege](https://discuss.elastic.co/u/ege)
#### Post date: [January 28, 2021, 12:52am UTC](https://discuss.elastic.co/t/vega-handle-a-situation-where-data-set-returned-is-empty/262210/11 "2021-01-28T00:52:20Z")

</div>

That's the problem @aaron-nimocks. **I want to figure out a way to check if there is no data is returned from the query**. That's why I tried to use,

> [@ege](#):
>
> length(data('table')) == 1 ? datum.sum : '-'

in the **text mark** and display a dash when there are no results returned in the query. But, it doesn't seem to work.

---

<div class="post-metadata">

### Author: ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)
#### Post date: [January 28, 2021, 1:10am UTC](https://discuss.elastic.co/t/vega-handle-a-situation-where-data-set-returned-is-empty/262210/12 "2021-01-28T01:10:51Z")

</div>

Sorry I didn't even read the initial question. 🙂

The way I understand it is you can't do it from where you are testing.

```auto
    {
      "name": "metric",
      "type": "text",
      "from": {"data": "table"},
      "encode": {
        "enter": {
          "x": {"signal": "width/2"},
          "y": {"signal": "height*0.45"},
          "align": {"value": "center"},
          "baseline": {"value": "middle"},
          "text": {"signal": "length(data('table')) == 1 ? datum.sum : '-'"},
          "fontSize": {"value": 30},
          "fontWeight": {"value": "bold"},
          "fill": {"value": "blue"}
        }
      }
    }

```

The from data table line means loop through every row of the data table and do the below. If the data table is empty then it will never process any of those marks to even check. Maybe I'm wrong but that's always how I thought it works.

If so, the solution would be to create the data table so it always exists and have your query that gets your real data add to it.

---

<div class="post-metadata">

### Author: ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)
#### Post date: [January 28, 2021, 1:36am UTC](https://discuss.elastic.co/t/vega-handle-a-situation-where-data-set-returned-is-empty/262210/13 "2021-01-28T01:36:04Z")

</div>

Try this @ege

Create a signal that will check to see if data is in the table. It will be an empty array if empty.

`"signals": [{"name": "$tableLoaded", "init": "data('table')"}],`

Then just duplicate your text mark. Have one loop through the data and the other not. Put a conditional on the one that doesn't use the data to see if the signal `$tableLoaded` is empty or not and put your `-` mark if false.

```auto
    {
      "name": "metric",
      "type": "text",
      "from": {"data": "table"},
      "encode": {
        "enter": {
          "x": {"signal": "width/2"},
          "y": {"signal": "height*0.45"},
          "align": {"value": "center"},
          "baseline": {"value": "middle"},
          "text": {"signal": "datum.sum //or whatever the real value is"},
          "fontSize": {"value": 30},
          "fontWeight": {"value": "bold"},
          "fill": {"value": "blue"}
        }
      }
    },
    {
      "type": "text",
      "encode": {
        "enter": {
          "x": {"signal": "width/2"},
          "y": {"signal": "height*0.45"},
          "align": {"value": "center"},
          "baseline": {"value": "middle"},
          "text": {"signal": "$tableLoaded == '[]' ? '' : '-'"},
          "fontSize": {"value": 30},
          "fontWeight": {"value": "bold"},
          "fill": {"value": "blue"}
        }
      }
    }

```

---

<div class="post-metadata">

### Author: ![ege](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ege/32/80438_2.png) [@ege](https://discuss.elastic.co/u/ege)
#### Post date: [January 28, 2021, 2:20am UTC](https://discuss.elastic.co/t/vega-handle-a-situation-where-data-set-returned-is-empty/262210/14 "2021-01-28T02:20:01Z")

</div>

**WOohOOOOOO! IT WORKED!!!** 🥳 🥳 🥳

Thank you so much @aaron-nimocks! I had to adjust it a little bit, but, your approach worked! 😃

Eventhough the length of the queried data-set cannot be checked using `length(data('table'))` for some reason, the length of the array generated through the signal can be checked. So, here's what I did:

```
  "signals": [{"name": "$tableLoaded", "init": "data('table')"}],
  "marks": [
    {
      "name": "metric",
      "type": "text",
      "from": {"data": "table"},
      "encode": {
        "enter": {
          "x": {"signal": "width/2"},
          "y": {"signal": "height*0.45"},
          "align": {"value": "center"},
          "baseline": {"value": "middle"},
          "text": {"signal": "datum.sum"},
          "fontSize": {"value": 30},
          "fontWeight": {"value": "bold"},
          "fill": {"value": "black"}
        }
      }
    },
    {
      "type": "text",
      "encode": {
        "enter": {
          "x": {"signal": "width/2"},
          "y": {"signal": "height*0.45"},
          "align": {"value": "center"},
          "baseline": {"value": "middle"},
          "text": {"signal": "length($tableLoaded) == 0 ? '-' : ''"},
          
          "fontSize": {"value": 30},
          "fontWeight": {"value": "bold"},
          "fill": {"value": "blue"}
        }
      }
    }
  ]

```

I think you are right about this

> [@aaron-nimocks](#):
>
> If the data table is empty then it will never process any of those marks to even check.

and that's why `length(data('table'))` is not working.

That solves a problem I had my head burning on for the past coupe of days. 😁

Thank you again @wylie and @aaron-nimocks for your great support! 😍 😍 😍

---

<div class="post-metadata">

### Author: ![ege](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ege/32/80438_2.png) [@ege](https://discuss.elastic.co/u/ege)
#### Post date: [January 30, 2021, 3:10pm UTC](https://discuss.elastic.co/t/vega-handle-a-situation-where-data-set-returned-is-empty/262210/15 "2021-01-30T15:10:06Z")

</div>

Getting back to the Vega version where the problem occured, **Vega v 4.3.0** , the above solution should be adjusted a little bit in order to work. Since `init` property in signals is only available after **Vega v 4.4** , I had to change the code as below, by adding `update` property instead of `init`.

```
  "signals": [{"name": "$tableLoaded", "update": "data('table')"}],
  "marks": [
    {
      "name": "metric",
      "type": "text",
      "from": {"data": "table"},
      "encode": {
        "enter": {
          "x": {"signal": "width/2"},
          "y": {"signal": "height*0.45"},
          "align": {"value": "center"},
          "baseline": {"value": "middle"},
          "text": {"signal": "datum.sum"},
          "fontSize": {"value": 30},
          "fontWeight": {"value": "bold"},
          "fill": {"value": "black"}
        }
      }
    },
    {
      "type": "text",
      "encode": {
        "enter": {
          "x": {"signal": "width/2"},
          "y": {"signal": "height*0.45"},
          "align": {"value": "center"},
          "baseline": {"value": "middle"},
          "text": {"signal": "length($tableLoaded) == 0 ? '-' : ''"},
          
          "fontSize": {"value": 30},
          "fontWeight": {"value": "bold"},
          "fill": {"value": "blue"}
        }
      }
    }
  ]

```

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: [February 27, 2021, 3:10pm UTC](https://discuss.elastic.co/t/vega-handle-a-situation-where-data-set-returned-is-empty/262210/16 "2021-02-27T15:10:34Z")

</div>

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