Vega, barras apiladas con multiples backets

Hello

I have problems to make a bar chart stacked, the data is with aggregations and the field that I need to stack is at the last level and I do not know how to obtain it.
I tried to flatten the structure but it did not work, I think I do not know how to do it.

I have the base of the bar graph armed with the groupings that I need.

I attach the data that give me the query and the vega

I would appreciate it if you could tell me how I can get the key of the last bucket

Result of query :arrow_right: Data

Vega

{
 "$schema": "https://vega.github.io/schema/vega-lite/v2.4.json",
 "data": {
 "url": {
  "%context%": true,
  "%timefield%": "caso_ts",
  "index": "gdm_full",
  "body": {
    "size": 0,
    "aggs": {
      "cotizaciones": {
        "terms": {
          "field": "caso_S2_id.keyword",
          "size": 500,
          "order": {
            "gestiones": "desc"
          }
        },
        "aggs": {
          "gestiones": {
            "cardinality": {
              "field": "ges_id.keyword"
            }
          },
          "ultima_ges": {
            "terms": {
              "field": "ges_id.keyword",
              "size": 1,
              "order": {
                "_term": "desc"
              }
            },
            "aggs": {
              "ges_ultima": {
                "cardinality": {
                  "field": "ges_id.keyword"
                }
              },
              "res_ultima_ges": {
                "terms": {
                  "field": "ges_resultado_llamada.keyword",
                  "size": 10
                },
                "aggs": {
                  "res_ultima": {
                    "cardinality": {
                      "field": "ges_id.keyword"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
},
"format": {
  "property": "aggregations.cotizaciones.buckets"
}
},
"transform": [
{
  "type": "flatten", 
  "fields": ["['ultima_ges']['buckets']"], 
  "as":["key"]
},
    {
  "type": "flatten", 
  "fields": ["['ultima_ges_buckets.key']"], 
  "as":["key2"]
},
{
  "aggregate": [
    {
      "op": "count",
      "field": "key",
      "as": "cotizacionescount"
    }
  ],
  "groupby": [
    "gestiones.value"
  ]
}
],
"encoding": {
"x": {
  "field": "gestiones\\.value",
  "type": "ordinal",
  "sort": "descending"
},
"y": {
  "field": "cotizacionescount",
  "type": "quantitative"
}
},
"layer": [
{
  "mark": "bar",
  "encoding": {
    "y": {
      "field": "cotizacionescount", 
      "type": "quantitative", 
      "stack": "zero"
    },
    "x": {
       "field": "gestiones\\.value",
       "type": "ordinal",
       "sort": "descending"
    },
    "color": {
      "field": "key", 
      "type": "nominal"
    }
  }
},
{
  "mark": {
    "type": "text",
    "align": "center",
    "baseline": "bottom",
    "dx": 0
  },
  "encoding": {
    "x": {
       "field": "gestiones\\.value",
       "type": "ordinal",
       "sort": "descending"
    },
    "y": {
      "field": "cotizacionescount", 
      "type": "quantitative", 
      "stack": "zero"
    },
    "color": {
      "field": "",
      "type": "nominal",
      "scale": {
        "range": [
          "white"
        ]
      },
      "legend": null
    },
    "text": {
     "field": "cotizacionescount", 
      "type": "quantitative",
      "format": ""
    }
  }
}
]

@nyuriks can you please help here?

Thanks
Rashmi

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