0

So I am working with vega-lite-v4 (that's the version our businesses airtable extension uses) and the answer to my previous post was that I need to use the pivot transform

But any time I try and use it as it is explained in the v4 documentation (https://vega.github.io/vega-lite-v4/docs/pivot.html) it throws an error as if the pivoted field does not exist

I've used the following test data: Airtable test data

With the following test code:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "title": "Table 2",
  "transform": [{
    "pivot": "type",
    "value": "calls",
    "groupby": ["Month"]
  }],
  "mark": "bar",
  "encoding": {
    "x": {"field": "Month", "type": "nominal"},
    "y": {"field": "Total", "type": "quantitative"}
  }
}

And I still get the same error:

Total is not a valid transform name, inline data name, or field name from the Table 2 table:

"y": {
  "field": "Total",
------------^
  "type": "quantitative"
}

Even when I copy and paste the examples from the above documentation into the widget, it comes up with this error like pivot isn't making these fields

Can anyone help me figure out why this isn't working, or what to use instead?

EDIT:

So, a weird solution/workaround I found is to calculate the field as itself:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "title": "Table 2",
  "transform": [{
    "pivot": "type",
    "value": "calls",
    "groupby": ["Month"]
  },
  {"calculate" : "datum.Total", "as" : "newTotal"}
  ],
  "mark": "bar",
  "encoding": {
    "x": {"field": "Month", "type": "nominal"},
    "y": {"field": "newTotal", "type": "quantitative"}
  }
}

This makes the graph behave completely as normal. I can use this for now, but it means I have to hard code each field name with a calculate transform, does this help anyone understand what's going on with this transform?

1 Answers1

1

First of all, Vega and Vega-lite field names are case-sensitive, so "Month" is not the same as "month".

In your first code sample, "month" is incorrect and should be "Month":

"x": {"field": "month", "type": "nominal"},

but in the second code sample that was changed to "Month" which is correct:

"x": {"field": "Month", "type": "nominal"},

Try just correcting field name "Month" in the first code sample without calculating "newTotal":

 {
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "title": "Table 2",
  "transform": [{
    "pivot": "type",
    "value": "calls",
    "groupby": ["Month"]
  }],
  "mark": "bar",
  "encoding": {
    "x": {"field": "Month", "type": "nominal"},
    "y": {"field": "Total", "type": "quantitative"}
  }
}

[EDIT: added following]

Here is a working example using your example data with pivot transform and rendered as bar chart by Vega-lite v5.2.0 with no errors.

Try using Vega-lite v5.2.0 instead of v4.

View in Vega on-line editor

enter image description here

Roy Ing
  • 724
  • 1
  • 2
  • 2
  • Whoops, I caught that halfway through making my post and only changed the second code block. My bad, its all case corrected now however, and the issue still arises – Richard Sweet Aug 12 '22 at 14:35
  • Added example to Answer showing current version of Vega-lite (v5.2.0) works with your data and pivot transform without errors. Try using Vega-lite v5.2.0 instead of v4. – Roy Ing Aug 12 '22 at 20:21
  • Unfortunately airtable uses v4, and we collect and store our data on airtable. I will check if they have plans to upgrade – Richard Sweet Aug 12 '22 at 21:09