Search code examples
vega-lite

How do you make Vega-Lite calculations with a field that has a space in its name?


I'm trying to do a calculation in Vega-Lite and the data I'm working with has spaces in its field header names. To provide a sample of some of the data I'm working with, see below:

  "data": {
    "values": [
      {
        "Event Classification": "Online",
        "Time Period": "Last Week",
        "Status": "Activity",
        "Events": "100",
        "Total Events": "500",
      },
      {
        "Event Classification": "In Person",
        "Time Period": "Last Week",
        "Status": "No Activity",
        "Events": "400",
        "Total Events": "500",
      }

And in a transform object I am writing code to divide Events by Total Events. However, I am not able to get the below code working:

 "transform": [
    {
      "calculate": "datum.Events/datum.Total Events",
      "as": "Percent of Events"
    }

I am getting the error Unexpect token after expression. when I am doing this in the Vega Editor. I cannot change the column headers because this data is coming out of an enterprise-wide database. Here is the link to the editor for someone to check out the problem I am running into. Thanks so much for your help!


Solution

  • Use bracket notation:

     "calculate": "datum.Events/datum['Total Events']",