{
  "version": 1,
  "project": {
    "name": "Retail model example",
    "context": "Tables and relationships from the supplied schema.",
    "dataStatus": "schema-only",
    "definitionStatus": "Schema supplied; no records included",
    "sources": [
      "retail-example.sql"
    ],
    "businessContext": "Illustrative retail schema with demonstration metric definitions."
  },
  "tables": [
    {
      "id": "public.customers",
      "label": "Customers",
      "sqlNameParts": [
        "public",
        "customers"
      ],
      "fields": [
        {
          "id": "customer_id",
          "label": "Customer id",
          "type": "number",
          "sqlType": "INTEGER",
          "meaning": "",
          "nullable": false
        },
        {
          "id": "region",
          "label": "Region",
          "type": "string",
          "sqlType": "VARCHAR ( 100 )",
          "meaning": "",
          "nullable": true
        }
      ],
      "primaryKey": [
        "customer_id"
      ],
      "uniqueKeys": [],
      "grain": "",
      "meaning": "",
      "status": "Declared in schema; records not checked",
      "source": "retail-example.sql"
    },
    {
      "id": "public.orders",
      "label": "Orders",
      "sqlNameParts": [
        "public",
        "orders"
      ],
      "fields": [
        {
          "id": "order_id",
          "label": "Order id",
          "type": "number",
          "sqlType": "INTEGER",
          "meaning": "",
          "nullable": false
        },
        {
          "id": "customer_id",
          "label": "Customer id",
          "type": "number",
          "sqlType": "INTEGER",
          "meaning": "",
          "nullable": false
        },
        {
          "id": "order_date",
          "label": "Order date",
          "type": "string",
          "sqlType": "DATE",
          "meaning": "",
          "nullable": false
        },
        {
          "id": "status",
          "label": "Status",
          "type": "string",
          "sqlType": "VARCHAR ( 20 )",
          "meaning": "",
          "nullable": false
        }
      ],
      "primaryKey": [
        "order_id"
      ],
      "uniqueKeys": [],
      "grain": "",
      "meaning": "",
      "status": "Declared in schema; records not checked",
      "source": "retail-example.sql"
    },
    {
      "id": "public.customer_profiles",
      "label": "Customer profiles",
      "sqlNameParts": [
        "public",
        "customer_profiles"
      ],
      "fields": [
        {
          "id": "customer_id",
          "label": "Customer id",
          "type": "number",
          "sqlType": "INTEGER",
          "meaning": "",
          "nullable": false
        },
        {
          "id": "segment",
          "label": "Segment",
          "type": "string",
          "sqlType": "VARCHAR ( 30 )",
          "meaning": "",
          "nullable": true
        }
      ],
      "primaryKey": [
        "customer_id"
      ],
      "uniqueKeys": [],
      "grain": "",
      "meaning": "",
      "status": "Declared in schema; records not checked",
      "source": "retail-example.sql"
    },
    {
      "id": "public.products",
      "label": "Products",
      "sqlNameParts": [
        "public",
        "products"
      ],
      "fields": [
        {
          "id": "product_id",
          "label": "Product id",
          "type": "number",
          "sqlType": "INTEGER",
          "meaning": "",
          "nullable": false
        },
        {
          "id": "category",
          "label": "Category",
          "type": "string",
          "sqlType": "VARCHAR ( 100 )",
          "meaning": "",
          "nullable": true
        }
      ],
      "primaryKey": [
        "product_id"
      ],
      "uniqueKeys": [],
      "grain": "",
      "meaning": "",
      "status": "Declared in schema; records not checked",
      "source": "retail-example.sql"
    },
    {
      "id": "public.order_items",
      "label": "Order items",
      "sqlNameParts": [
        "public",
        "order_items"
      ],
      "fields": [
        {
          "id": "order_id",
          "label": "Order id",
          "type": "number",
          "sqlType": "INTEGER",
          "meaning": "",
          "nullable": false
        },
        {
          "id": "line_number",
          "label": "Line number",
          "type": "number",
          "sqlType": "INTEGER",
          "meaning": "",
          "nullable": false
        },
        {
          "id": "product_id",
          "label": "Product id",
          "type": "number",
          "sqlType": "INTEGER",
          "meaning": "",
          "nullable": false
        },
        {
          "id": "quantity",
          "label": "Quantity",
          "type": "number",
          "sqlType": "INTEGER",
          "meaning": "",
          "nullable": false
        },
        {
          "id": "sales_amount",
          "label": "Sales amount",
          "type": "number",
          "sqlType": "DECIMAL ( 12 , 2 )",
          "meaning": "",
          "nullable": true
        },
        {
          "id": "refund_amount",
          "label": "Refund amount",
          "type": "number",
          "sqlType": "DECIMAL ( 12 , 2 )",
          "meaning": "",
          "nullable": false
        }
      ],
      "primaryKey": [
        "order_id",
        "line_number"
      ],
      "uniqueKeys": [],
      "grain": "",
      "meaning": "",
      "status": "Declared in schema; records not checked",
      "source": "retail-example.sql"
    }
  ],
  "relationships": [
    {
      "id": "relationship_1",
      "parentTable": "public.customers",
      "parentFields": [
        "customer_id"
      ],
      "childTable": "public.orders",
      "childFields": [
        "customer_id"
      ],
      "parentToChildren": "zero_or_many",
      "childOptional": false,
      "meaning": "",
      "status": "Declared foreign key; records not checked"
    },
    {
      "id": "relationship_2",
      "parentTable": "public.customers",
      "parentFields": [
        "customer_id"
      ],
      "childTable": "public.customer_profiles",
      "childFields": [
        "customer_id"
      ],
      "parentToChildren": "zero_or_one",
      "childOptional": false,
      "meaning": "",
      "status": "Declared foreign key; records not checked"
    },
    {
      "id": "relationship_3",
      "parentTable": "public.orders",
      "parentFields": [
        "order_id"
      ],
      "childTable": "public.order_items",
      "childFields": [
        "order_id"
      ],
      "parentToChildren": "zero_or_many",
      "childOptional": false,
      "meaning": "",
      "status": "Declared foreign key; records not checked"
    },
    {
      "id": "relationship_4",
      "parentTable": "public.products",
      "parentFields": [
        "product_id"
      ],
      "childTable": "public.order_items",
      "childFields": [
        "product_id"
      ],
      "parentToChildren": "zero_or_many",
      "childOptional": false,
      "meaning": "",
      "status": "Declared foreign key; records not checked"
    }
  ],
  "metrics": [
    {
      "id": "orders",
      "label": "Number of orders",
      "meaning": "Count every order once, including completed and cancelled orders in this example.",
      "definition": "Count every order once, including completed and cancelled orders in this example.",
      "unit": "Orders",
      "formula": "COUNT(*)",
      "sqlFormula": "COUNT(*)",
      "pythonFormula": "result = len(data)",
      "rFormula": "result <- nrow(data)",
      "sourceFields": [
        {
          "table": "public.orders",
          "field": "order_id"
        }
      ],
      "grain": "One order",
      "aggregation": "count",
      "timeBasis": "All available records when run on your data.",
      "filterSemantics": "No filters in this example definition.",
      "inclusions": [
        "All order statuses"
      ],
      "exclusions": [],
      "zeroDenominator": "Not calculated without records. Empty or all-missing averages are unavailable.",
      "status": "Example business definition supplied alongside the schema",
      "calculation": {
        "op": "count",
        "table": "public.orders"
      },
      "displayFormat": "number",
      "analysis": {
        "rowFilters": []
      },
      "measureType": "count"
    },
    {
      "id": "line_value",
      "label": "Total sales",
      "meaning": "Add the sales amounts recorded for items on orders, before refunds. Includes all order statuses. Amounts are USD in this example.",
      "definition": "Add the sales amounts recorded for items on orders, before refunds. Includes all order statuses. Amounts are USD in this example.",
      "unit": "USD",
      "formula": "SUM(sales_amount)",
      "sqlFormula": "SUM(sales_amount)",
      "pythonFormula": "result = data[\"sales_amount\"].sum(min_count=1)",
      "rFormula": "values <- data[[\"sales_amount\"]]\nresult <- if (any(!is.na(values))) sum(values, na.rm = TRUE) else NA_real_",
      "sourceFields": [
        {
          "table": "public.order_items",
          "field": "sales_amount"
        }
      ],
      "grain": "One item entry on an order",
      "aggregation": "sum",
      "timeBasis": "All available records when run on your data.",
      "filterSemantics": "No filters in this example definition.",
      "inclusions": [
        "All order statuses"
      ],
      "exclusions": [
        "Missing sales amounts are skipped when adding or averaging sales."
      ],
      "zeroDenominator": "Not calculated without records. Empty or all-missing averages are unavailable.",
      "status": "Example business definition supplied alongside the schema",
      "calculation": {
        "op": "sum",
        "table": "public.order_items",
        "field": "sales_amount"
      },
      "displayFormat": "currencyUSD",
      "analysis": {
        "rowFilters": []
      },
      "measureType": "total"
    },
    {
      "id": "average_line_value",
      "label": "Average sales amount",
      "meaning": "Average the sales amounts recorded for items on orders. Each item entry counts once, even if its quantity is greater than one. Missing amounts are skipped. Amounts are USD in this example.",
      "definition": "Average the sales amounts recorded for items on orders. Each item entry counts once, even if its quantity is greater than one. Missing amounts are skipped. Amounts are USD in this example.",
      "unit": "USD",
      "formula": "AVG(sales_amount)",
      "sqlFormula": "AVG(sales_amount)",
      "pythonFormula": "values = data[\"sales_amount\"].dropna()\nresult = float(values.mean()) if not values.empty else None",
      "rFormula": "values <- data[[\"sales_amount\"]]\nresult <- if (any(!is.na(values))) mean(values, na.rm = TRUE) else NA_real_",
      "sourceFields": [
        {
          "table": "public.order_items",
          "field": "sales_amount"
        }
      ],
      "grain": "One item entry on an order",
      "aggregation": "mean",
      "timeBasis": "All available records when run on your data.",
      "filterSemantics": "No filters in this example definition.",
      "inclusions": [
        "All order statuses"
      ],
      "exclusions": [
        "Missing sales amounts are skipped when adding or averaging sales."
      ],
      "zeroDenominator": "Not calculated without records. Empty or all-missing averages are unavailable.",
      "status": "Example business definition supplied alongside the schema",
      "calculation": {
        "op": "mean",
        "table": "public.order_items",
        "field": "sales_amount"
      },
      "displayFormat": "currencyUSD",
      "analysis": {
        "rowFilters": []
      },
      "measureType": "average"
    },
    {
      "id": "net_sales",
      "label": "Net sales",
      "meaning": "Total sales minus total refunds recorded against the same items on orders. Includes all order statuses. Amounts are USD in this example.",
      "definition": "Total sales minus total refunds recorded against the same items on orders. Includes all order statuses. Amounts are USD in this example.",
      "unit": "USD",
      "formula": "SUM(sales_amount) - SUM(refund_amount)",
      "sqlFormula": "SUM(sales_amount) - SUM(refund_amount)",
      "pythonFormula": "gross = data[\"sales_amount\"].sum(min_count=1)\ncredits = data[\"refund_amount\"].sum(min_count=1)\nresult = gross - credits",
      "rFormula": "gross <- data[[\"sales_amount\"]]\ncredits <- data[[\"refund_amount\"]]\nresult <- if (any(!is.na(gross)) && any(!is.na(credits))) sum(gross, na.rm = TRUE) - sum(credits, na.rm = TRUE) else NA_real_",
      "sourceFields": [
        {
          "table": "public.order_items",
          "field": "sales_amount"
        },
        {
          "table": "public.order_items",
          "field": "refund_amount"
        }
      ],
      "grain": "One item entry on an order",
      "aggregation": "difference of sums",
      "timeBasis": "All available records when run on your data.",
      "filterSemantics": "No filters in this example definition.",
      "inclusions": [
        "All order statuses"
      ],
      "exclusions": [
        "Missing sales amounts are skipped when adding or averaging sales."
      ],
      "zeroDenominator": "Missing sales amounts are skipped. Refund amounts are required; record zero when there is no refund. Empty input produces an unavailable result.",
      "status": "Example business definition supplied alongside the schema",
      "calculation": {
        "op": "difference",
        "left": {
          "op": "sum",
          "table": "public.order_items",
          "field": "sales_amount"
        },
        "right": {
          "op": "sum",
          "table": "public.order_items",
          "field": "refund_amount"
        }
      },
      "displayFormat": "currencyUSD",
      "analysis": {
        "rowFilters": []
      },
      "measureType": "total"
    },
    {
      "id": "order_items_count",
      "label": "Number of order items",
      "meaning": "Count each item entry on an order once. An entry with a quantity of three still counts as one entry. Includes all order statuses.",
      "definition": "Count each item entry on an order once. An entry with a quantity of three still counts as one entry. Includes all order statuses.",
      "unit": "Order items",
      "formula": "COUNT(*)",
      "sqlFormula": "COUNT(*)",
      "pythonFormula": "result = len(data)",
      "rFormula": "result <- nrow(data)",
      "sourceFields": [
        {
          "table": "public.order_items",
          "field": "line_number"
        }
      ],
      "grain": "One item entry on an order",
      "aggregation": "count",
      "timeBasis": "All available records when run on your data.",
      "filterSemantics": "No filters in this example definition.",
      "inclusions": [
        "All order statuses"
      ],
      "exclusions": [],
      "zeroDenominator": "Not calculated without records. Empty or all-missing averages are unavailable.",
      "status": "Example business definition supplied alongside the schema",
      "calculation": {
        "op": "count",
        "table": "public.order_items"
      },
      "displayFormat": "number",
      "analysis": {
        "rowFilters": []
      },
      "measureType": "count"
    },
    {
      "id": "refund_percentage",
      "label": "Percentage of sales refunded",
      "meaning": "Total refunds divided by total recorded sales, multiplied by 100. Includes all order statuses. Missing sales amounts are skipped in the sales total.",
      "definition": "Total refunds divided by total recorded sales, multiplied by 100. Includes all order statuses. Missing sales amounts are skipped in the sales total.",
      "unit": "%",
      "formula": "100 * total refunds / total sales",
      "sqlFormula": "100.0 * SUM(refund_amount) / NULLIF(SUM(sales_amount), 0)",
      "pythonFormula": "import pandas as pd\n\nsales = data[\"sales_amount\"].sum(min_count=1)\nrefunds = data[\"refund_amount\"].sum(min_count=1)\nresult = 100 * refunds / sales if pd.notna(sales) and pd.notna(refunds) and sales != 0 else None",
      "rFormula": "sales <- data[[\"sales_amount\"]]\nrefunds <- data[[\"refund_amount\"]]\nsales_total <- if (any(!is.na(sales))) sum(sales, na.rm = TRUE) else NA_real_\nresult <- if (!is.na(sales_total) && sales_total != 0 && any(!is.na(refunds))) 100 * sum(refunds, na.rm = TRUE) / sales_total else NA_real_",
      "sourceFields": [
        {
          "table": "public.order_items",
          "field": "sales_amount"
        },
        {
          "table": "public.order_items",
          "field": "refund_amount"
        }
      ],
      "grain": "One item entry on an order",
      "aggregation": "Percentage from totals",
      "timeBasis": "All available records when run on your data.",
      "filterSemantics": "No filters in this example definition.",
      "inclusions": [
        "All order statuses"
      ],
      "exclusions": [
        "Missing sales amounts are skipped when adding or averaging sales."
      ],
      "zeroDenominator": "No percentage is calculated when total sales is zero or missing.",
      "status": "Example business definition supplied alongside the schema",
      "calculation": {
        "op": "percentage",
        "numerator": {
          "op": "sum",
          "table": "public.order_items",
          "field": "refund_amount"
        },
        "denominator": {
          "op": "sum",
          "table": "public.order_items",
          "field": "sales_amount"
        }
      },
      "displayFormat": "percentPoints",
      "analysis": {
        "rowFilters": []
      },
      "measureType": "percentage"
    }
  ],
  "dimensions": [
    {
      "id": "region",
      "label": "Customer region",
      "meaning": "Region assigned to a customer.",
      "table": "public.customers",
      "field": "region"
    },
    {
      "id": "status",
      "label": "Order status",
      "meaning": "Whether the order is completed or cancelled.",
      "table": "public.orders",
      "field": "status",
      "values": [
        "completed",
        "cancelled"
      ],
      "valuesScope": "allowed"
    },
    {
      "id": "category",
      "label": "Product category",
      "meaning": "Category assigned to a product.",
      "table": "public.products",
      "field": "category"
    }
  ],
  "scenarios": [],
  "assumptions": [],
  "presentation": {
    "showMetricFilters": false
  },
  "importInfo": {
    "format": "SQL DDL",
    "reader": "Common CREATE TABLE and ALTER TABLE key constraints; no SQL executed"
  }
}