# Subtracting two lines in vega chart

**URL:** <https://discuss.elastic.co/t/subtracting-two-lines-in-vega-chart/314847>\
**Category:** Kibana\
**Tags:** vega\
**Created:** [September 21, 2022, 9:29am UTC](https://discuss.elastic.co/t/subtracting-two-lines-in-vega-chart/314847 "2022-09-21T09:29:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![michalruszczyk](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@michalruszczyk](https://discuss.elastic.co/u/michalruszczyk)\
**Post date:** [September 21, 2022, 9:29am UTC](https://discuss.elastic.co/t/subtracting-two-lines-in-vega-chart/314847/1 "2022-09-21T09:29:43Z")

</div>

Hi, I am trying to create a chart which shows some kind of risk. The logic behind is that I have an index which has Dalue, startDate and endDate. I would like to show the line which is cumulative sum of value with startdate substracted by the same value but with enddate. I am only able to show value with only one of this date. Here is my code:

```auto
{
  $schema: https://vega.github.io/schema/vega-lite/v4.json
  data: {
# URL object is a context-aware query to Elasticsearch
    url: {
      %context%: true
      %timefield%: startDate
      index: nebula
      body: {
        size: 10000
        _source: [
          startDate
          Value
          endDate
        ]
      }
    }
# We only need the content of hits.hits array
    format: {
      property: hits.hits
    }
  }
#Parse timestamp into a javascript date value
  selection: {
    brush: {"type": "interval", "encodings": ["x"], "bind": "scales"}}
  mark: {
    type: line
    point: true
  }
  encoding: {
    x: {
      field: _source.startDate
      type: temporal
      timeUnit: "day"
      zoom: true
      bin: {"maxbins": 100 , "extent": {"selection": "brush"}}
      axis: {
        title: Date
        format: "%Y-%m-%d"
        labelAngle: -45
        labelOverlap: true
      }
    }
    y: {
      field: _source.Value
      type: "quantitative"
      aggregate: "sum"
      axis: {
        title: Risk
      }
    }
  }
}

```

I would appreciate any kind of help!

`> Blockquote`

---

<div class="post-metadata">

**Author:** ![Nathan\_Reese](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathan_reese/32/84829_2.png) [@Nathan\_Reese](https://discuss.elastic.co/u/Nathan_Reese)\
**Post date:** [September 21, 2022, 4:05pm UTC](https://discuss.elastic.co/t/subtracting-two-lines-in-vega-chart/314847/2 "2022-09-21T16:05:13Z")

</div>

A lot has changed since 2017 and I would answer this question differently now. I would recommend using [formulas in Lens](https://www.elastic.co/guide/en/kibana/current/lens.html#lens-formulas)

Have you tried using [vega-lite transforms](https://vega.github.io/vega-lite/docs/transform.html) for your calculations?

---

<div class="post-metadata">

**Author:** ![michalruszczyk](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@michalruszczyk](https://discuss.elastic.co/u/michalruszczyk)\
**Post date:** [September 22, 2022, 10:08am UTC](https://discuss.elastic.co/t/subtracting-two-lines-in-vega-chart/314847/3 "2022-09-22T10:08:02Z")

</div>

The thing is, I need the zooming future which kibana chart does not provide. I do not know how to use vega-lite transforms to create a new value/line which is subtraction of two cumulative sum of the same value from two different dates which are in the index record. Do you have any idea?

---

<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:** [October 20, 2022, 10:08am UTC](https://discuss.elastic.co/t/subtracting-two-lines-in-vega-chart/314847/4 "2022-10-20T10:08:27Z")

</div>

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