Dandomain Webshop model

Product sales over time

Sales per order line, excluding VAT, with credit-note lines as returns.

Grain
day x shop x order x order line
Built from
  1. 3
    Raw tables
  2. 3
    Staging
  3. 1
    Core
  4. 1
    Analytics
Reads from Dandomain Webshop
orderorder_discount_codeorder_line
Ships as
Weld and dbt SQL, open source on GitHub
core.dandomain_webshop.product_sales_over_time
1-- core.dandomain_webshop.product_sales_over_time
2-- Sales per order line, excluding VAT, with credit-note lines as returns.
3--
4-- Grain: day x shop x order x order line. row_type is SALE, or RETURN for the
5-- lines of a credit note. Currency: the order's own. Dates: shop-local.
6--
7-- Depends on: staging.dandomain_webshop.order, .order_line, .order_discount_code
8--
9-- Same order filter as sales_over_time. Discount codes apply to the whole order,
10-- so they are spread across its lines in proportion to each line's value after
11-- line discounts; with that, net_sales here sums to net_sales in sales_over_time.
12-- tests/assert_dandomain_webshop__product_reconciles_to_sales.sql checks it. Shipping and payment
13-- fees have no product and stay in sales_over_time only.
14
15WITH orders AS (
16    SELECT
17        dandomain_shop,
18        order_id,
19        order_created_at,
20        status_id,
21        currency_iso,
22        IF(status_id = 100, 'RETURN', 'SALE')              AS row_type,
23        IF(status_id = 100, -1, 1)                         AS sign_factor
24    FROM {{staging.dandomain_webshop.order}}
25    WHERE COALESCE(status_id, -1) NOT IN (0, 5, 99)
26),
27
28codes AS (
29    SELECT dandomain_shop, order_id, SUM(ABS(discount_amount)) AS discount_codes
30    FROM {{staging.dandomain_webshop.order_discount_code}}
31    GROUP BY dandomain_shop, order_id
32),
33
34lines AS (
35    SELECT
36        l.*,
37        ABS(l.unit_price * l.quantity)                     AS line_gross,
38        ABS(COALESCE(l.unit_discount, 0) * l.quantity)     AS line_discount,
39        o.order_created_at,
40        o.status_id,

This model and the 4 it depends on. Deploy them together; refs like {{staging.…}} resolve to the matching folders in Weld, and the dbt view uses source() and ref().

Notes

How it counts. And where it stops.

Same order filter as sales_over_time. Discount codes apply to the whole order, so they are spread across its lines in proportion to each line's value after line discounts; with that, net_sales here sums to net_sales in sales_over_time. tests/assert_dandomain_webshop__product_reconciles_to_sales.sql checks it. Shipping and payment fees have no product and stay in sales_over_time only.

Raw tables it reads

  • raw.dandomain_webshop.order
  • raw.dandomain_webshop.order_discount_code
  • raw.dandomain_webshop.order_line

More Dandomain Webshop models

Put Dandomain Webshop in front
of your agents.

Connect Dandomain Webshop, deploy the models, and ask questions in Claude, Cursor or ChatGPT. Free for 14 days, no credit card.