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.
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 BYis 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
SUMorCOUNTcolumn. - 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.
| Visual | Shape | On a config card | In SQL |
|---|---|---|---|
| Sankey | One row per path, a dimension per stage, one count or sum | Channel, Region, Status on Rows; a count on Metrics | SELECT channel, region, status, COUNT(*) … GROUP BY 1, 2, 3 |
| Calendar heatmap | One row per day, one measure | Order date grouped by day on Rows; revenue on Metrics | A date column, one row per day |
| Gauge | One row, a value, and optionally a target | Revenue and Revenue goal on Metrics, nothing on Rows | Two number columns in one row |
| Waterfall | One row per step, one sum | Step on Rows; a sum of the change on Metrics | A step label and a summed change per step |
| Small multiples | One row per panel and bar, one measure | Region, then Product on Rows; revenue on Metrics | Two dimension columns and a number |
| Progress list | One row per item, a value, and optionally a target | Product on Rows; revenue and revenue goal on Metrics | An item, a value and a target column |
Example: a sankey in SQL
SELECT
sales_channel AS "Channel",
region AS "Region",
order_status AS "Status",
COUNT(*) AS "Orders"
FROM orders
GROUP BY 1, 2, 3The three text columns fill the sankey's Stages in order, and Orders sizes each flow.
Example: progress toward a target in SQL
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 DESCProduct 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.