Sub topic 15
From queries to dashboards and alerts
This chapter assumes you can write and run queries in the SQL editor, use three-level Unity Catalog names, and read the warehouse sections of this tutorial on auto stop and cost. Here you turn a query into two things that run without you: a dashboard that people open, and an alert that watches a number for you.
How queries become dashboards and alerts
A query you run once in the editor is a one-off. Two features reuse it:
- A dashboard is a page of charts, tables, and filters. Each one is backed by a dataset, which is a SQL query.
- An alert runs a saved query on a schedule. It checks one value against a condition and notifies people when the condition is met.
Both run their SQL on a SQL warehouse. Every refresh and every alert check is a normal query. It shows up in query history, uses warehouse time, and can keep the warehouse awake.
Building a dashboard
Datasets
A dashboard dataset is a SQL query, and it is the only source of data for the dashboard's widgets. You can write it in the dashboard's data tab, or start from SQL you already tested in the editor and paste it in. Keep each dataset focused. A dataset that returns a few hundred aggregated rows is cheaper and faster than one that returns raw rows for a chart to summarize.
This dataset returns daily trip counts and average fares from the sample data:
SELECT
DATE_TRUNC('day', tpep_pickup_datetime) AS pickup_day,
pickup_zip,
COUNT(*) AS trips,
ROUND(AVG(fare_amount), 2) AS avg_fare
FROM samples.nyctaxi.trips
GROUP BY DATE_TRUNC('day', tpep_pickup_datetime), pickup_zip;The dataset keeps pickup_zip as a column on purpose. A dashboard filter can then narrow the result without a new query for each zip code.
Visualizations and filters
On the canvas you add a visualization widget, choose a dataset, and map columns to axes. A line chart of trips by pickup_day is one choice. A table is another. A filter widget lets viewers pick values such as a zip code or a date range. Filters apply to every widget that uses a dataset containing the filtered field.
Parameters in datasets
The :name marker you used in the editor also works in dashboard datasets. A parameter changes the SQL that runs, so the warehouse runs the query again with the new value. A filter on a returned column narrows data that is already loaded.
SELECT pickup_zip, trip_distance, fare_amount
FROM samples.nyctaxi.trips
WHERE trip_distance > :min_distance
ORDER BY fare_amount DESC
LIMIT 50;Use a parameter when the filter should reduce what the warehouse reads, for example a date range over a large table. Use a filter when the dataset is small and viewers will flip between values often.
| Need | Use | What happens on change |
|---|---|---|
| Reduce rows scanned in a large table | Dataset parameter | The query runs again on the warehouse |
| Slice a small result by a column | Filter widget | The loaded result is narrowed |
| Different default per viewer | Parameter with a default value | The query runs with the chosen value |
Publishing, credentials, and refresh
A dashboard has a draft and a published version. You edit the draft. Viewers see the published version until you publish again.
When you publish, you choose whose permissions run the queries:
- Shared data permissions (embedded credentials). Queries run with the publisher's access. A viewer sees data they could not query directly. This is convenient, but it is also an access decision. Only publish this way if everyone who can open the dashboard is allowed to see all of its data.
- Individual data permissions. Each viewer's own Unity Catalog permissions apply. A viewer who lacks access to a table sees an error on that widget instead of the data.
Viewers also need access to the dashboard itself. You grant that separately from table access, using the sharing dialog.
A published dashboard can refresh on a schedule. A scheduled refresh runs each dataset on the chosen warehouse and caches results, so the first viewer of the morning does not wait for every query.
Building an alert
An alert watches a value from a saved query. The query should return one row, and the alert reads a chosen column from the first row. If you need a count or a sum, aggregate in SQL.
SELECT COUNT(*) AS high_fare_trips
FROM samples.nyctaxi.trips
WHERE fare_amount > 100;Condition
You configure the alert in three parts:
- Query and column: the saved query and the column to test, such as
high_fare_trips. - Condition: an operator and a threshold, such as
>and0. The alert triggers when the condition is true. - Empty result behavior: what to do when the query returns no rows. Decide this on purpose. A query that returns nothing when something is broken (for example, no new orders today) can look the same as a healthy "nothing to report" result.
Schedule and notifications
You set how often the query runs and who is notified. Recipients can be workspace users or configured destinations such as email. You can also customize the message. Many teams include the value and a link to the query so the recipient can investigate.
An alert is always in one of three states:
| State | Meaning |
|---|---|
| OK | The last check ran and the condition was false |
| Triggered | The last check ran and the condition was true |
| Unknown or error | The alert has not run yet, or the check failed |
Notifications are sent when the alert moves into Triggered. Whether it notifies again while it stays Triggered depends on the alert's re-notification setting, so check it to avoid either silence or repeated messages.
Failure modes and gotchas
- Stale data. Dashboards show the last refresh. If the warehouse was stopped and the schedule is off, viewers see old results until someone refreshes or the next run happens.
- Permission surprises. A dashboard published with embedded credentials can expose data that its viewers could not query directly. The reverse also happens: with viewer credentials, a user with no table access sees errors.
- Warehouse choice. Frequent short checks suit a serverless warehouse because it starts quickly and stops quickly. A classic or pro warehouse has a slower start. If a stopped warehouse must start for every check, the alert is delayed and each check costs at least the minimum run before auto stop.
- Query changes. An alert depends on the saved query's column name. Renaming
high_fare_tripsin the query breaks the alert condition until you update it. - Many widgets, one warehouse. A dashboard with a dozen datasets sends a dozen queries at refresh. On a small warehouse with a low maximum cluster count, they queue. Check query history if refreshes are slow, then use the query profile on the slowest dataset.
Choosing between a dashboard and an alert
Use a dashboard when people need to explore, compare, or filter. Use an alert when the question is "tell me when this number crosses a line". A common pattern is both: the dashboard shows the trend, and an alert on the same logic sends a message when the trend goes wrong.
Exercise
You publish a dashboard with shared data permissions. A colleague who has no access to samples.nyctaxi.trips opens it. What do they see, and what should you check before publishing this way?
Show answer
They see the data, because the queries run with the publisher's permissions. Before publishing this way, confirm that everyone who can open the dashboard is allowed to see all the data it shows. If not, publish with individual data permissions so each viewer's own access applies.
Exercise
An alert uses the query SELECT COUNT(*) AS high_fare_trips FROM samples.nyctaxi.trips WHERE fare_amount > 100, with the condition high_fare_trips > 0, scheduled every 5 minutes on a pro warehouse with a 10-minute auto stop. What happens to the warehouse, and what would you change?
Show answer
The warehouse never reaches its 10-minute idle time, because a check runs every 5 minutes. It stays on all day and bills for it. Lengthen the interval, move the alert to a serverless warehouse with a short auto stop, or both.