Vega Context Within Specific Aggregations

Need help building a query to properly calculate turnover to be used in Vega. I tried looking at the context-must/context-must_not portions of Vega but I don't know if I can apply these to specific aggregations unless I do nested queries. The formula we're using for turnover is as follows.

(Terminations / ((Headcount in start month + Headcount in end month) / 2)) * 100

In the time filter, the user would select the desired date range and the min date would be the start month while the max date would be the end month. Terminations would include all term rows for all dates between start and finish.

According to Vega documentation, I should be able to use the following in order to apply the specific dates I want, but I need them to apply only to the specific their intended aggregations. Min should only apply to the Begin Headcount aggregation while max should only apply to the Ending Headcount

"min": {"%timefilter%": "min"}
"max": {"%timefilter%": "max"}

Data structure:

    YR-MTH    ACTIONTYPE    RCDTYPE     EMPLID  
    2021-01   Involuntary    TER         1
    2021-01   Active         BGN         2   
    2021-01   Active         END         2  
    2021-01   Active         BGN         3   
    2021-01   Active         END         3  
    2021-01   Active         BGN         4   
    2021-01   Active         END         4
    2021-02   Active         BGN         2   
    2021-02   Active         END         2
    2021-02   Involuntary    TER         3     
    2021-02   Active         BGN         4   
    2021-02   Active         END         4
    2021-03   Involuntary    TER         4
    2021-03   Active         BGN         2   
    2021-03   Active         END         2   

So with this example if the user selects January 2021 to March 2021 the formula would be as follows.
Again Total terms across selected range divided by total BGN rows for min month, plus total END rows for final month, divided by two.

(TER / (( BGN + END ) / 2 )) * 100
( 3 / (( 3 + 1 ) / 2)) * 100

My current query only works when one month is selected. Thoughts on how to proceed?

"size": 0,
  "aggs": {
    "Turnover_Calculation": {
      "terms": {
        "script": "'try this'"
      },
      "aggs": {
        "Turnover":{ 
          "filter": { "term": { "VH_RCD_TYPE_ORCL_ES_NA_ENG":"TER" } },
          "aggs" : {
            "termcount": {
              "value_count": {
                "field": "ACTIONTYPE_ORCL_ES_NA_ENG"
              }
            }
          }
        },
        "Headcount_BGN":{ 
          "filter": { "term": { "VH_RCD_TYPE_ORCL_ES_NA_ENG": "BGN" } },
          "aggs" : {
            "headcount": {
              "value_count": {
                "field": "ACTIONTYPE_ORCL_ES_NA_ENG"
              }
            }
          }
        },
        "Headcount_END":{ 
          "filter": { "term": { "VH_RCD_TYPE_ORCL_ES_NA_ENG": "END" } },
          "aggs" : {
            "headcount": {
              "value_count": {
                "field": "ACTIONTYPE_ORCL_ES_NA_ENG"
              }
            }
          }
        },
        "Turnover-Percent": {
          "bucket_script": {
            "buckets_path": {
              "Headcount_BGN": "Headcount_BGN>headcount",
              "Turnover": "Turnover>termcount",
              "Headcount_END": "Headcount_END>headcount"
            },
            "script": "params.Turnover / ((params.Headcount_BGN + params.Headcount_END) /2) * 100"
          }
        }
      }
    }
  }

This did the trick.

Probably user error, but I kept getting zero documents when including the date ranges in the must section of the bool. Can anyone explain why?

Also, an example from my dataset displays the turnover percentage as "6.14...". Is there anyway to add a "%" just for flair and remove the "..." at the end?

{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.json",
  "data": {
    "url": {
      "%timefield%": "EFFDT_ORCL_ES_ENG",
      "%context%": true,
      "index": "turnover*",
      "body": {
        "aggs": {
    "Turnover_Calculation": {
      "terms": {
        "script": "'try this'"
      },
      "aggs": {
        "Terminations":{ 
         "filter": { 
            "bool": {
              "should": [
                { "range": {
                    "EFFDT_ORCL_ES_KW_ENG": {
                      "%timefilter%": true
                    }
                  }
                }
              ],
              "must": [
                { "match":{ "RCD_TYPE_ORCL_ES_NA_ENG": "TER" } }
              ],
              "minimum_should_match": 1
            }
          },
          "aggs" : {
            "termcount": {
              "value_count": {
                "field": "ACTIONTYPE_ORCL_ES_NA_ENG"
              }
            }
          }
        },
        "Headcount_BGN":{ 
          "filter": { 
            "bool": {
              "should": [
                { "range": {
                    "EFFDT_ORCL_ES_KW_ENG": {
                      "from": {"%timefilter%": "min"}
                      "to": {"%timefilter%": "min", "shift": 4, "unit": "week" }
                    }
                  }
                }
              ],
              "must": [
                { "match":{ "RCD_TYPE_ORCL_ES_NA_ENG": "BGN" }
                }
              ],
              "minimum_should_match": 1
            }
          },
          "aggs" : {
            "headcount": {
              "value_count": {
                "field": "ACTIONTYPE_ORCL_ES_NA_ENG"
              }
            }
          }
        },
        "Headcount_END":{ 
          "filter": { 
            "bool": {
              "should": [
                { "range": {
                    "EFFDT_ORCL_ES_KW_ENG": {
                      "from": {"%timefilter%": "max", "shift": -4, "unit": "week"}
                      "to": {"%timefilter%": "max" }
                    }
                  }
                }
              ],
              "must": [
                { "match":{ "RCD_TYPE_ORCL_ES_NA_ENG": "END" }
                }
              ],
              "minimum_should_match": 1
            }
          },
          "aggs" : {
            "headcount": {
              "value_count": {
                "field": "ACTIONTYPE_ORCL_ES_NA_ENG"
              }
            }
          }
        },
        "Turnover-Percent": {
          "bucket_script": {
            "buckets_path": {
              "Headcount_BGN": "Headcount_BGN>headcount",
              "Terms": "Terminations>termcount",
              "Headcount_END": "Headcount_END>headcount"
            },
            "script": "params.Terms / ((params.Headcount_BGN + params.Headcount_END) /2) * 100"
          }
        }
      }
    }
  },
        "size": 0
      }
    },
    "format": {"property": "aggregations.Turnover_Calculation.buckets"}
  },
  "config": {
    "style": {
      "cell": {"stroke": "transparent"},
      "text": {"fontSize": 30, "fontWeight": "bold", "limit": 110}
    }
  },
  "mark": "text",
  "style": "text",
  "encoding": {"text": {"field": "Turnover-Percent.value", "type": "string"}}
}

If you look at the request what is the actual query being executed after all variables are replaced with real values.

Then you can take the query to Dev Tools to try to adjust it to fit what you are looking for.

The Inspect button is grayed out for my visualization. Is there a setting I'm missing to enable this?

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