Skip to content

How we report sales whose channel code isn't mapped yet

Ihor Seleznov5 min read

When a company starts selling through a marketplace, its first sales can arrive with a channel code nobody has added to the reporting tables yet. We think those sales belong on their own Unmapped line, because a report that files them under Other still matches the source total and shows no marketplace at all.

A sales report usually takes channel names from a small reference table, a list that turns each short code into a name such as Web or Store. A report that keeps every sale and writes Other wherever the name is missing looks safe, because no revenue disappears. The total is right, so nobody checks the split, and a sales director sees the new channel's first weeks as unexplained growth in Other.

We built a small example in Databricks Free Edition with six synthetic sales, one of them with no code at all, and a reference table that doesn't know the marketplace code yet. A join that wrote Other for every missing name showed Other at 110 against the 20 that channel really sold, with the total still at 290. A count of unmatched sales per code found the 90 behind it. After we added the code, a streaming table in our Lakeflow pipeline still held that 90 under Unmapped, while a materialized view over the same join moved the marketplace sales into their channel. We didn't build the job that holds the report back when unmapped revenue is too high.

Where the marketplace revenue went

Our reference table knows three codes. WEB is Web, STORE is Store, and OTH is a channel the business really calls Other. Two sales arrive as MKT, the marketplace, for 50 and 30, and one sale arrives with no code, for 10. The report joins each sale to the reference table with a LEFT JOIN, which keeps every sale and leaves the name empty when there's no match, and then fills an empty name with Other.

On the Free Edition SQL warehouse, that report showed Other at 110, while the real Other channel had sold 20. The source total and the report total were both 290, so a reconciliation of totals passed. The marketplace had no row at all.

The version that loses revenue is easier to catch. With an inner join, which keeps only the sales that found a match, the report total fell to 200, and the same reconciliation would flag the missing 90. Filling the name keeps the money and moves it to the wrong line, which is why we'd look at the split as well as the total.

An Unmapped line and a count per code

We'd label sales without a match as Unmapped in the report itself. In our example the report then read Web 100, Store 80, Other 20 and Unmapped 90. Other went back to meaning the channel called Other, and anyone reading the report could see that 90 of revenue had no channel yet.

The check behind that line counts the sales without a match for each source code, so each gap goes to the right person:

SELECT s.channel_code,
       COUNT(*) AS sales,
       SUM(s.amount) AS revenue,
       ROUND(100 * SUM(s.amount) / (SELECT SUM(amount) FROM sales_src), 1) AS pct_of_revenue
FROM sales_src s
LEFT ANTI JOIN channel_ref r ON s.channel_code = r.channel_code
GROUP BY s.channel_code
ORDER BY s.channel_code NULLS LAST;

A LEFT ANTI JOIN returns the sales that found no match. It returned MKT with 2 sales and 80, which is 27.6% of revenue, and an empty code with 1 sale and 10, which is 3.4%. A sale with no code can't match anything in this join, so it lands in Unmapped too. The two rows go to different people. The new code goes to whoever owns the reference table, and the missing code goes to whoever owns the source system.

We'd agree a share with the report's owner and hold the channel report when unmapped revenue goes above it. A total-revenue report can still go out with a warning, because its number is right. We'd send every new code to the reference table's owner even below that share, since its revenue sits outside its channel until someone adds the code. Holding a report back takes a validation step that the publishing task depends on, and we've described that design in our release gate for Databricks data quality.

What a streaming table keeps after the code is added

Adding the code fixes the report only where the join runs again over the earlier sales. We built the same join twice in a Lakeflow pipeline, the Databricks feature that keeps a set of tables up to date from their sources. A streaming table processes each sale once, as it arrives, and keeps that result. A materialized view keeps the result of its query over the tables as they stand at each refresh. Both carry the reference table's code into a column, so a sale without a match shows an empty code there.

CREATE OR REFRESH STREAMING TABLE channel_sales_st (
  CONSTRAINT mapped_channel EXPECT (ref_code IS NOT NULL)
) AS
SELECT s.order_id, s.channel_code, r.channel_code AS ref_code, r.channel_name, s.amount
FROM STREAM(workspace.unmapped_codes_probe.sales_src) s
LEFT JOIN workspace.unmapped_codes_probe.channel_ref r
  ON s.channel_code = r.channel_code;

After the first update, both tables reported Unmapped at 90. Then we added MKT as Marketplace in the reference table, one more marketplace sale of 40 arrived, and the pipeline ran again. The materialized view showed Marketplace at 120 and Unmapped at 10, the sale with no code. The streaming table showed Marketplace at 40 and Unmapped still at 90, because the two earlier marketplace sales kept the empty name they got when they were first processed.

Both tables reported Unmapped at 90

We added MKT as Marketplace

One more marketplace sale of 40

The pipeline ran again

Streaming table: Marketplace at 40, Unmapped at 90

Materialized view: Marketplace at 120, Unmapped at 10

After we added MKT as Marketplace and the pipeline ran again, the streaming table still showed Unmapped at 90 while the materialized view showed Marketplace at 120.

Databricks documents this on its streaming tables page: "Joins in streaming tables do not recompute when dimensions change." For a channel report, adding the code fixes next week's sales and leaves the launch weeks under Unmapped. We'd build a channel report on a materialized view, or agree with the table's owner how the earlier sales get processed again once the code is in.

Why the pipeline's own numbers looked clean

Each table in our pipeline carries an expectation, a Lakeflow rule checked on every row as it's processed. Ours says the reference code isn't empty, and its warn action keeps the failing rows and counts them. Drop would have taken their revenue out of the total, and fail would have stopped the update over a single row.

In the first update, both tables reported 3 rows passed and 3 failed. In the second, the materialized view reported 6 passed and 1 failed, which matches what it holds. The streaming table reported 1 passed and 0 failed, because it processed only the new sale. At that moment it still held three sales and 90 of revenue without a channel.

The second update's counts showed no failures for a table that still had 90 of revenue without a channel. So we'd run the per-code count against the table the report reads, each time the report is built, and read the expectation counts as a record of what each update processed.

Where we'd start

We'd start with the reports finance and sales read every week. We'd look for places where a join to a reference table is followed by a default label such as Other, Unknown or N/A, and run the per-code count on the data behind each one. Then we'd agree with each report's owner what share of unmapped revenue holds the report, and who gets a new code when it shows up. If the report reads a streaming table, we'd decide before the next channel launches how its first sales will get their name. We'd do it as part of data engineering on Databricks, before a new channel's first sales land on the wrong line.

Frequently asked questions

Why not drop sales whose channel code has no match?

Dropping them takes their revenue out of the report's total, so finance sees a lower number with no explanation. In our example that would have been 90 of 290. The Unmapped line keeps the money in the total and shows where it's waiting.

Where do sales with no channel code go?

They go to Unmapped too, because an empty code can't match a row in the reference table with this join. In our example one sale of 10 had no code. We'd send that one to the owner of the source system, since a new row in the reference table can't fix it.

Does the check catch a code mapped to the wrong channel?

No. A code that finds a match passes, even if it points at the wrong channel. We'd have the reference table's owner review each new row, and compare a channel's revenue with the source system's own figures where they exist.

How do we stop the channel report from publishing?

An expectation can't look up another table, and a validation table inside a pipeline doesn't hold back the tables after it. We'd run the per-code count as its own job task and let the task that publishes the channel report run only when that one succeeds, the design in our release gate post. Which checks it blocks on is a choice we test first, the way we tested our quality rules against twelve defects.