# Percentage difference over two different periods

**URL:** <https://discuss.elastic.co/t/percentage-difference-over-two-different-periods/247838>\
**Category:** Kibana\
**Created:** [September 8, 2020, 8:00am UTC](https://discuss.elastic.co/t/percentage-difference-over-two-different-periods/247838 "2020-09-08T08:00:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas74](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Thomas74](https://discuss.elastic.co/u/Thomas74)\
**Post date:** [September 8, 2020, 8:00am UTC](https://discuss.elastic.co/t/percentage-difference-over-two-different-periods/247838/1 "2020-09-08T08:00:13Z")

</div>

Hi,

I would like to see how I receive specifics logs during two periods :

- Current month
- Current month - 1

The goal is to see the difference, in percentage, between them. For example, this month we increased log integration by 20% from last month.

I tried with timelion and TSVB but with no success 😕

Math integration could be a good idea but I don't know how to get params metrics from antoher bucket.

Is someone can guide me ?

Best regards,

Thomas R.

---

<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:** [September 10, 2020, 8:04pm UTC](https://discuss.elastic.co/t/percentage-difference-over-two-different-periods/247838/2 "2020-09-10T20:04:44Z")

</div>

This type of use case is harder than it sounds in Kibana for the reasons you found- it requires math across buckets. It's also difficult because of the global time filter which intersects with any buckets. When I have this kind of problem I write a Vega visualization from this template:

```auto
{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "data": {
    "url": {
      "index": "metricbeat-*",
      "body": {
        "aggs": {
          "f": {
            "filters": {
              "filters": {
                "current": {
                  "range": { "@timestamp": { "%timefilter%": true } }
                },
                "previous": {
                  "range": {
                    "@timestamp": {
                      "%timefilter%": true, "shift": -4, "unit": "week"
                    }
                  }
                }
              }
            }
          }
        },
        "size": 0
      }
    },
    "format": {
      "type": "json",
      "property": "aggregations.f.buckets"
    },
  }
  
  "transform": [
    {
      "calculate": "if (datum.previous.doc_count, datum.current.doc_count / datum.previous.doc_count, null)",
      "as": "percent_diff"
    }
  ]
  
  "vconcat": [
    {
      "title": "Percent change from 4 weeks ago"
      "width": "container"
      height: 40
      "mark": "text",
      "encoding": {
        "text": {
          "field": "percent_diff"
          "format": "0.2%"
        },
        "size": { value: 32 },
        "align": { "value": "center" },
      }
    },
    {
      "title": "Current value"
      "width": "container"
      "mark": "text",
      "encoding": {
        "text": {
          field: "current.doc_count",
          format: ","
        },
        "fill": { "value": "black" },
        "fontWeight": { "value": "bold" },
        "align": { "value": "center" },
      }
    }

  ],
}

```

---

<div class="post-metadata">

**Author:** ![Thomas74](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Thomas74](https://discuss.elastic.co/u/Thomas74)\
**Post date:** [September 11, 2020, 9:32am UTC](https://discuss.elastic.co/t/percentage-difference-over-two-different-periods/247838/3 "2020-09-11T09:32:24Z")

</div>

Thanks I will test it 🙂 I never used vega visualization. It seems interesting too.

---

<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 9, 2020, 9:32am UTC](https://discuss.elastic.co/t/percentage-difference-over-two-different-periods/247838/4 "2020-10-09T09:32:25Z")

</div>

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