The Data Model
What a Credible data model is and what goes into one (sources, joins, dimensions, measures, views, and annotations for documentation, search, access rules and materialization), and how models ship as versioned packages
A data model is the set of Malloy files that says what your data means and how it fits together. To build one, start with the Model Your Data.
What a Data Model Is
A data model connects your raw tables to the terms your business uses. It records which tables matter, their keys and how they join. It also defines what terms like revenue or an active customer mean, documents each field, and says who may see which rows.
The model is the input you write and own. Credible is the engine that runs it: it compiles queries against the model, builds the tables and indexes the model asks for, enforces its access rules, and serves it to every surface. Because every surface reads the same definitions, a measure gives the same number in a chat, a dashboard, a data app and an agent's answer.
Credible data models are written in Malloy, an open-source modeling and query language that compiles to SQL.
An Example
The model in sales.malloy:
##! experimental.givens
given:
#(secure)
GROUPS :: string[]
#(secure)
REGIONS :: string[]
#(doc) One row per customer account
source: customers is warehouse.table('sales.customers') extend {
primary_key: id
#(doc) Sales region assigned at account creation
#(index)
dimension: region is upper(region_code)
}
#(doc) One row per order, for every region
source: order_facts is warehouse.table('sales.orders') extend {
primary_key: order_id
join_one: customers on customer_id = customers.id
#(doc) The customer's sales region
dimension: region is customers.region
dimension: order_month is order_date.month
#(doc) Net revenue recognized at order completion, in USD
measure: total_revenue is sum(order_amount)
view: revenue_by_region is {
group_by: region
aggregate: total_revenue
}
}
#(doc) Orders in the regions the caller may see
#(access_filter) region in $REGIONS
source: orders is order_facts extend {}
#(doc) Orders with cost and margin, for the finance team
#(authorize) 'finance' in $GROUPS
source: order_margins is orders extend {
#(doc) Revenue minus cost of goods sold, in USD
measure: gross_margin is sum(order_amount - cost_amount)
}
#(doc) Monthly revenue by region, built as a table
#(access_filter) region in $REGIONS
#@ persist name="revenue_by_month"
source: revenue_by_month is order_facts -> {
group_by: order_month, region
aggregate: total_revenue
}The package's index.malloy, which decides what callers can query:
import "sales.malloy"
export { orders, order_margins, revenue_by_month }order_facts and customers have no access rule, so index.malloy leaves them out of the package's exports and people query orders, order_margins and revenue_by_month instead. Without an index.malloy, every source in the package could be queried, and a query on order_facts would read every region. The sections below explain each part.
Building Blocks
Sources
A source is a table, a SQL query, or another source, extended with everything the model knows about it: its primary key, joins, dimensions, measures and views. Other sources and queries build on it. A source can extend another, as orders extends order_facts, and add only what differs.
Joins
A join declares how two sources relate: join_one for many-to-one or one-to-one, join_many for one-to-many, and join_cross for a cross join. Queries then use the relationship by name (customers.region) without repeating the join condition. Malloy computes aggregates at the right grain across joins, so a measure is not double-counted when a query joins in a one-to-many relationship.
Dimensions
A dimension is a value you group or filter by: a column, or an expression over columns, such as order_month or region above.
Measures
A measure is an aggregate calculation, such as a sum, a count or a ratio. It is defined on its source and means the same thing in every query that uses it. A measure can include its own filter, for example revenue from enterprise customers only.
Views
A view is a named query on a source, such as revenue_by_region. Views record the analyses people run often and give people and agents a known starting point.
Annotations
Annotations are lines that start with #, placed directly above the definition they apply to. Annotations lists every one Credible reads.
Documentation
#(doc) describes a source or field in plain words. Agents read it to choose between similar fields. See Descriptions and Value Search.
Search Indexes
#(index) on a dimension adds its values to the search index, so a question that mentions "EMEA" finds the region field. See Descriptions and Value Search.
Access Rules
#(authorize) decides which callers can query a source, and #(access_filter) decides which rows each caller sees. Both read secure givens such as $GROUPS and $REGIONS, which Credible fills in from the caller's identity. In the example, only the finance group can query order_margins, and everyone sees only orders in their own regions. See Row and Column Access.
Permissions on environments and packages are separate: they decide who can open a package at all, and are managed in the app. See Permissions.
Materialization
#@ persist on a source defined as a query asks the engine to build it as a table and answer its queries from that table. Without it, queries run live against your data. A materialized source with an access filter declares the filter itself, as revenue_by_month does. See Materialization and Performance.
Packages
A package is the unit you build, version and publish together: a data model and everything built on it, such as dashboards, notebooks and data apps. Changes start in a draft, a working copy that isn't served to anyone until it's published. Each publish creates a new version, and the latest version is what everyone gets by default.
Teams that want changes reviewed before they go live can use a git-backed environment, where each publish opens a pull request instead. See Publishing and Versions.