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

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:

  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":

   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:

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 :frowning:

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

Thanks so much in advance for your insights!

Fixed this by making a calculate in the transform section:

{
      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
    }
1 Like

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