Mprove
.mdx

Model - Malloy

For SQL connections, Mprove creates Data Models based on Malloy Sources.

In Malloy, the term “model” usually refers to a file containing Malloy code.

In Mprove, the term “model” refers to a Malloy Source with the tag #(mprove) model.

To minimize Malloy compilation time in Mprove, it is important to store Malloy sources in separate files from other Malloy elements, such as run: ... or query: ....

This file structure is also easier to maintain.

Malloy Sources are reusable. Multiple Sources and Queries can reference the same Source.

c1_order_items.malloy
import './tables/c1_order_items_tx.malloy';
import './tables/c1_orders_tx.malloy';
import './tables/c1_users_tx.malloy';
import './tables/c1_user_order_facts_tx.malloy';
import './tables/c1_inventory_items_tx.malloy';
import './tables/c1_products_tx.malloy';
import './tables/c1_distribution_centers_tx.malloy';

#(mprove) model
#(mprove) access_roles="sales, marketing"
#(mprove) label="Order Items (Postgres)"
#(mprove) top_label="Order Items"
source: c1_order_items is c1_order_items_tx extend {
  join_one: orders is c1_orders_tx on order_id = orders.order_id
  join_one: users is c1_users_tx on orders.user_id = users.user_id
  join_one: user_order_facts is c1_user_order_facts_tx on users.user_id = user_order_facts.user_id
  join_one: inventory_items is c1_inventory_items_tx on inventory_item_id = inventory_items.inventory_item_id
  join_one: products is c1_products_tx on inventory_items.product_id = products.product_id
  join_one: distribution_centers is c1_distribution_centers_tx on inventory_items.distribution_center_id = distribution_centers.distribution_center_id

  measure:
    total_profit is total_sale_price - inventory_items.total_actual_cost
    profit_margin is total_profit / total_sale_price
}

Example of building Metrics that can be used in Reports:

tables/c1_orders_tx.malloy
##! experimental{sql_functions}
##! experimental.access_modifiers
##! experimental { sql_functions virtual_source }

type: c1_orders_type is {
  order_id :: string,
  user_id :: string,
  created_at :: number,
  created_at_ts :: timestamptz,
  status :: string
}

source: c1_orders_table is c1_postgres.table('ecommerce.orders')::c1_orders_type extend {
  rename: created_at_timestamp is created_at_ts
}

source: c1_orders_tx is c1_orders_table include {
  internal:
    created_at_timestamp
    created_at
  public: *
} extend {
  primary_key: order_id

  measure:
    orders_count is count()

  #(mprove) build_metrics field_group="Created At"
  dimension:
    created_at_t is created_at_timestamp
    created_at_ts is created_at_t
    created_at_day is created_at_t.day
    created_at_week is created_at_t.week
    created_at_month is created_at_t.month
    created_at_quarter is created_at_t.quarter
    created_at_year is created_at_t.year
    created_at_hour is created_at_t.hour
    created_at_minute is created_at_t.minute
    created_at_second is created_at_t.second
}

Reference

Source #(mprove) Tags

NameTypeDefaultDescription
modeltag-If specified, the Malloy source will become available for exploration as an Mprove model.
access_rolestag-User roles list separated by comma. If access_roles tag is set, only users with specified roles will be able to explore the model. If access_roles tag is not specified, all project users will be able to explore the model.
labeltag-Model label in UI
top_labeltag-Top node label in the Model Schema

Dimension #(mprove) Tags

NameTypeDefaultDescription
build_metricstag-Mprove creates a metric for each pair of measure and tagged time based dimension (for all measures of the malloy source). build_metrics field_group must have fields with "_t" and "_ts" suffixes. Timeframes can only be based on a field with "_t" suffix.
field_grouptag-Mprove adds the tagged fields to a subgroup in the web interface (model fields tree).

On this page