# Use "calculate" to make an operation on a flattened field in Vega

**URL:** <https://discuss.elastic.co/t/use-calculate-to-make-an-operation-on-a-flattened-field-in-vega/310100>\
**Category:** Kibana\
**Tags:** vega\
**Created:** [July 20, 2022, 7:31am UTC](https://discuss.elastic.co/t/use-calculate-to-make-an-operation-on-a-flattened-field-in-vega/310100 "2022-07-20T07:31:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![InesCM](https://avatars.discourse-cdn.com/v4/letter/i/9d8465/32.png) [@InesCM](https://discuss.elastic.co/u/InesCM)\
**Post date:** [July 20, 2022, 7:31am UTC](https://discuss.elastic.co/t/use-calculate-to-make-an-operation-on-a-flattened-field-in-vega/310100/1 "2022-07-20T07:31:40Z")

</div>

Hi!  
I'm trying to make a simple multiplication operation on a flattened field, but I don't seem to be able to access the flattened fields in the calculation.

For instance, if I make this multiplication operation with non-flattened fields, it works fine:

```auto
  format: {
      property: aggregations.freqs.freq.buckets
    }
  }
  transform: [
    {
      calculate: datum.rangos.buckets
      as: snr_rangos
    }
    {
      flatten: [
        snr_rangos
      ]
    }
    {
      calculate: datum.key
      as: frequencies
    }
    {
      calculate: datum.doc_count
      as: devices
    }
    {
    calculate: datum.doc_count * 100
    as: result
    }
  ]

```

But if I try to make the same simple operation on the flattened snr\_rangos, I get an error: Unrecognized signal name: "snr\_rangos":

```auto
   format: {
      property: aggregations.freqs.freq.buckets
    }
  }
  transform: [
    {
      calculate: datum.rangos.buckets
      as: snr_rangos
    }
    {
      flatten: [
        snr_rangos
      ]
    }
    {
      calculate: datum.key
      as: frequencies
    }
    {
      calculate: datum.doc_count
      as: devices
    }
    {
    calculate: snr_rangos.doc_count * 100
    as: result
    }
  ]

```

The flatten is working fine, I am perfectly able to use the fields from snr\_rangos on the enconding later. This works fine:

```auto
encoding: {
    x: {
      field: frequencies
      axis: {
        title: DVB-C frequency
      }
    }
    y: {
      field: snr_rangos.doc_count
      type: quantitative
      axis: {
        title: "# of devices"
      }
    }

```

It's only the calculate on the flattened snr\_rangos that does not seem to work ☹

Is there any way to perform a "calculate" on a flattened field?

Thanks so much in advance for your insights!

---

<div class="post-metadata">

**Author:** ![InesCM](https://avatars.discourse-cdn.com/v4/letter/i/9d8465/32.png) [@InesCM](https://discuss.elastic.co/u/InesCM)\
**Post date:** [July 26, 2022, 9:51am UTC](https://discuss.elastic.co/t/use-calculate-to-make-an-operation-on-a-flattened-field-in-vega/310100/2 "2022-07-26T09:51:28Z")

</div>

Fixed this by making a calculate in the transform section:

```auto
{
      flatten: [
        snr_rangos
      ]
      as: S
    }
    {
      calculate: datum.key
      as: frequencies
    }
    {
      calculate: datum.doc_count
      as: devices
    }
    {
      calculate: datum.S.doc_count * 100 / datum.doc_count
      as: devices_percent
    }

```

---

<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:** [August 23, 2022, 9:51am UTC](https://discuss.elastic.co/t/use-calculate-to-make-an-operation-on-a-flattened-field-in-vega/310100/3 "2022-08-23T09:51:48Z")

</div>

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