Tooltip Formatting Issues

I am having a heck of a time understanding how to format tooltips. See linked where there are two visualizations, where I’m looking for assistance in:
How would I tweak the Kerry Kolosko creation so that:

Tool tips are formatted such that

  1. the left example would display Plan tooltip to be “Plan: 15.0k” and the Value tooltip would be “Value: 9.4k”
    and the
  2. the right example tooltip, regardless over where the user rolled over would display

Thank you for any assistance you can provide!

https://lancemillercpa-my.sharepoint.com/:u:/g/personal/lance_lancemillercpa_onmicrosoft_com/EaEyf7BBQAJMgtQkRKtfoWABuHHW1Hq3Pjf5FOVfnnxiMQ?e=iN1IIo

Hi @lancemillercpa98.

How’s this?

To implement, just add a tooltip block to the encoding block and reference the plan and value fields.

To perform in-visual calculations for the variance and % variance, add a transform block then expand the tooltip encoding block as desired.

{
  "data": {"name": "dataset"},
  "transform": [
    {
      "calculate": "datum['Value'] - datum['Plan']",
      "as": "_variance"
    },
    {
      "calculate": "datum['_variance'] / datum['Plan']",
      "as": "_percent_variance"
    }
  ],
  "encoding": {
    "x": {
      "type": "quantitative",
      "scale": {"nice": false},
      "title": null,
      "axis": null
    }
  },
  "layer": [
    {
      "mark": {
        "type": "bar",
        "color": "#ccc",
        "size": 40,
        "yOffset": 10,
        "tooltip": true
      },
      "encoding": {
        "x": {"field": "Plan"},
        "y": {
          "field": "",
          "title": null
        }
      }
    },
    {
      "mark": {
        "type": "bar",
        "size": 20,
        "yOffset": 10,
        "tooltip": true
      },
      "encoding": {
        "x": {
          "field": "Value",
          "title": null
        },
        "color": {
          "condition": [
            {
              "test": "datum['KPI'] === 2",
              "value": "red"
            },
            {
              "test": "datum['KPI'] === 1",
              "value": "yellow"
            },
            {
              "test": "datum['KPI'] === 0",
              "value": "black"
            }
          ],
          "value": "green"
        },
        "tooltip": [
          {
            "field": "Plan",
            "type": "quantitative",
            "formatType": "pbiFormat",
            "format": "#,#"
          },
          {
            "field": "Value",
            "type": "quantitative",
            "formatType": "pbiFormat",
            "format": "#,#"
          },
          {
            "field": "_variance",
            "type": "quantitative",
            "title": "Variance",
            "formatType": "pbiFormat",
            "format": "#,#"
          },
          {
            "field": "_percent_variance",
            "type": "quantitative",
            "title": "% Variance",
            "formatType": "pbiFormat",
            "format": "#%"
          }
        ]
      }
    },
    {
      "mark": {
        "type": "tick",
        "color": "orange",
        "size": 50,
        "yOffset": 10,
        "thickness": 2,
        "tooltip": true
      },
      "encoding": {
        "x": {
          "field": "KPITarget Value",
          "title": null
        }
      }
    },
    {
      "mark": {
        "type": "text",
        "color": "orange",
        "dx": -5,
        "dy": 0,
        "xOffset": 0,
        "yOffset": 50,
        "angle": 0,
        "align": "right",
        "baseline": "bottom",
        "fontSize": 14,
        "fontWeight": "normal",
        "limit": 0
      },
      "encoding": {
        "text": {
          "field": "KPITarget Value",
          "format": ".2s"
        },
        "x": {
          "field": "KPITarget Value",
          "type": "quantitative"
        }
      }
    },
    {
      "mark": {
        "type": "text",
        "dx": 0,
        "dy": 0,
        "xOffset": 0,
        "yOffset": -40,
        "angle": 0,
        "align": "right",
        "baseline": "bottom",
        "font": "sans-serif",
        "fontSize": 20,
        "fontStyle": "normal",
        "fontWeight": "normal",
        "limit": 0
      },
      "encoding": {
        "x": {
          "aggregate": "max",
          "field": "Plan",
          "type": "quantitative"
        },
        "text": {
          "field": "Actual",
          "format": ".2s"
        }
      }
    },
    {
      "mark": {
        "type": "text",
        "dx": 0,
        "dy": 0,
        "xOffset": 0,
        "yOffset": -40,
        "angle": 0,
        "align": "right",
        "baseline": "top",
        "font": "sans-serif",
        "fontSize": 14,
        "fontStyle": "normal",
        "fontWeight": "normal",
        "limit": 0
      },
      "encoding": {
        "x": {
          "aggregate": "max",
          "field": "Plan",
          "type": "quantitative"
        },
        "text": {
          "field": "Format Variance"
        }
      }
    }
  ]
}

Hope it helps.
Greg
eDNA Forum - Deneb Tooltip Formatting.pbix (2.5 MB)

1 Like

This is excellent! Again, thank you Greg!!!

To add to this, if you pass in a field and value to the encoding block AND the field can be traced back to one that was added from the model AND that field has a specified format in the data model, Deneb will resolve it automatically. For calculated fields, you will need to provide a format specifier.

For calculation groups and dynamic format strings for measures, Deneb 1.6 will have changes to support this. 1.6 is pending certification, but you can read about this upcoming feature in the staging area here.

2 Likes