Semaphor
Custom Visuals

SQL and config cards

Which fields a custom visual sees on a card built from fields and on a SQL card, and how to shape the data for the visual you want

A custom visual draws whatever your card returns, so getting the visual you want starts with the card's data. Both kinds of card work. They differ in where the visual's fields come from and how you name and shape them.

A card built from fieldsROWSRegionChannel1PIVOT COLUMNSLeave empty for a visualMETRICSRevenueAverage discount2A SQL cardSELECTregion AS "Region",order_month AS "Order month",SUM(amount) AS "Revenue"FROM ordersGROUP BY 1, 2ORDER BY 23The fields your visual seesRegiondimensionChanneldimensionRevenuemeasure · adds upAverage discountmeasure · doesn't add upThe columns your visual seesRegiondimensionOrder monthdimensionRevenuemeasure41Rows are the dimensionsIn the order they sit on the shelf.2Metrics are the measuresEach knows whether it adds up.3Aliases name the fieldsThe names in legends and tooltips.4Column types set the kindNumbers are measures; text and datesare dimensions.
On a card built from fields, the shelves decide what the visual sees. On a SQL card, the columns and their names do.

Cards built from fields

On a drag-and-drop (config) card, the shelves decide what the visual sees:

  • Rows are the dimensions, in the order they sit on the shelf. The first one fills the visual's first dimension, and so on.
  • Metrics are the measures, in order.
  • Names are the fields' names on the card. Rename a field on its shelf and the visual's labels follow.
  • Formats are the fields' formats: set a measure's currency or decimals on the field itself.
  • Adds up or not is known from each measure's aggregate, so a visual that adds rows can warn you about an average.

Things to watch:

  • Leave Pivot Columns empty. A pivot turns each value of the pivot field into its own measure column, which a visual reads as many separate measures.
  • Comparisons stay on the card. A measure compared with a previous period still gives the visual its current value only. To draw both, add the previous period as a measure of its own, or write it in SQL.
  • Dates group as you set them. A date on Rows grouped by month reaches the visual as months, labelled as the card labels them.

SQL cards

On a SQL card, the result columns decide what the visual sees:

  • Number columns are measures. Text, date and true-or-false columns are dimensions.
  • Aliases are the names. AS "Revenue" is what the visual shows in its legend and tooltips.
  • Column order is field order. Dimensions fill the visual's dimension fields in the order they appear in your SELECT; the same for measures.
  • ORDER BY is row order. The visual draws rows in the order your query returns them.
  • Formats come from the column settings. Set a column's number format, such as a currency, from its header on the Results tab.

Things to watch:

  • A number you group by reads as a measure. A year or a store number stored as a number is taken as a measure. Cast it to text in your SQL, or pick it for the right field in the panel; on a SQL card, every picker offers every column.
  • The visual can't tell a sum from an average. If the visual adds rows, such as a sankey or a waterfall, give it a SUM or COUNT column.
  • Server pagination shows one page. With Server pagination on, the visual gets only the first page of rows and says so. Turn it off for a card that feeds a visual, and keep the result to the rows you want drawn.
  • Run the query before copying the prompt, so the prompt lists your columns.

Shape the data for the visual

Each visual wants its data in a certain shape. Here is what the templates expect, and how to get it on either kind of card.

VisualShapeOn a config cardIn SQL
SankeyOne row per path, a dimension per stage, one count or sumChannel, Region, Status on Rows; a count on MetricsSELECT channel, region, status, COUNT(*) … GROUP BY 1, 2, 3
Calendar heatmapOne row per day, one measureOrder date grouped by day on Rows; revenue on MetricsA date column, one row per day
GaugeOne row, a value, and optionally a targetRevenue and Revenue goal on Metrics, nothing on RowsTwo number columns in one row
WaterfallOne row per step, one sumStep on Rows; a sum of the change on MetricsA step label and a summed change per step
Small multiplesOne row per panel and bar, one measureRegion, then Product on Rows; revenue on MetricsTwo dimension columns and a number
Progress listOne row per item, a value, and optionally a targetProduct on Rows; revenue and revenue goal on MetricsAn item, a value and a target column

Example: a sankey in SQL

Orders from channel to region to status
SELECT
  sales_channel AS "Channel",
  region        AS "Region",
  order_status  AS "Status",
  COUNT(*)      AS "Orders"
FROM orders
GROUP BY 1, 2, 3

The three text columns fill the sankey's Stages in order, and Orders sizes each flow.

Example: progress toward a target in SQL

Revenue against each product's goal
SELECT
  p.product_name     AS "Product",
  SUM(o.amount)      AS "Revenue",
  MAX(g.goal_amount) AS "Revenue goal"
FROM orders o
JOIN products p ON p.id = o.product_id
JOIN product_goals g ON g.product_id = p.id
GROUP BY 1
ORDER BY 2 DESC

Product fills Item, then Revenue and Revenue goal fill Value and Target in that order. Swap the two columns and the visual swaps them too, or pick them in the panel.

Which kind of card to use

  • Use a card built from fields when the data lives in a semantic domain: names, formats and aggregates come with the fields, and the visual knows what adds up.
  • Use a SQL card when you need a shape the shelves can't give, such as a target from another table or a calculation across rows.

Either way, check the Results tab first. If the rows there aren't what the visual should draw, fix the card, not the visual.

On this page