Lineage and integration
Source-to-target mapping: a worked example from source system to report
Contents
A source-to-target mapping says, for every field of a target (a table, a file, a message or a report), where its value comes from and what happens to it on the way. It is the specification of an integration, the checklist for testing it and, kept up to date, the most precise form of data lineage. This guide shows what a good mapping contains and follows one example from an order system to a revenue figure in a management report.
In short
- A source-to-target mapping lists, for each target field, its source fields, the transformation rule, and the filters, joins and lookups that decide which rows and values arrive.
- Every mapping row has a type: a direct copy, a conversion, a derivation, a lookup, an aggregation, a condition or a constant.
- Fields that only filter or join belong in the mapping too: they change the result without being copied.
- A chain of mappings, hop by hop, is column-level lineage: it explains a report figure back to the fields it comes from.
- A mapping is useful only while it matches the code: keep it under version control, review it with every change and test it.
What a source-to-target mapping is
A source-to-target mapping, often shortened to STTM, is written whenever data moves from one structure to another:
- when an integration, an ETL or ELT pipeline or an interface is designed;
- when data is migrated from an old system to a new one;
- when a data warehouse or a data mart is built;
- when a regulatory report must be explained down to its sources.
It serves several readers at once. Engineers build from it, testers derive expected results from it, data stewards check that the target fields mean what their names promise, and auditors use it to see how a reported figure was produced. The mapping is usually written by a data analyst or a data engineer together with someone who knows the business rules, and approved by the owner of the target.
What a mapping row contains
Each row describes one target field:
| Item | What it records | Example |
|---|---|---|
| Target | The system, table and field being filled | dw. |
| Target data type | The type, length and precision | DECIMAL(18,2) |
| Business term | The glossary term the field represents | Net revenue of an order line, in euros |
| Source fields | Every field the value is derived from | quantity, unit_, discount_ |
| Mapping type | How the value is produced | Lookup and derivation |
| Transformation rule | The rule in business words and as an expression | Net amount converted at the rate of the order date |
| Filters | The conditions that decide which rows arrive | Cancelled orders are left out |
| Joins and lookups | The keys used to combine data or translate values | Exchange rate by currency and order date |
| Empty values and defaults | What happens when a source value is missing or unknown | An empty discount counts as 0 |
| Checks | The quality checks applied on the way | The net amount is not negative |
| Owner, version, status | Who approves it, which version, whether it is implemented | Draft, approved, implemented |
Mapping types
| Type | What happens | Example |
|---|---|---|
| Direct | The value is copied as it is, possibly under a new name | order_ to order_ |
| Conversion | The type, format or unit changes | A text date becomes a date; cents become currency units |
| Derivation | The value is calculated from one or more fields | Quantity times unit price, minus the discount |
| Lookup | The value is translated through reference data or a key map | A currency code becomes an exchange rate; a customer number becomes a surrogate key |
| Aggregation | Many rows are summed or counted into one | Revenue summed by month and region |
| Conditional | The value depends on a condition | A status code turned into “open” or “closed” |
| Constant or default | A fixed value, or a fallback when the source has none | The source system’s name; “unknown” for a missing region |
| Row filter | No value is copied, but the field decides which rows arrive | Only orders that were not cancelled |
A worked example: from order lines to a revenue report
The monthly management report shows net revenue in euros by month and by sales region. The data passes through three hops: from the order system to a staging area, from staging to the data warehouse, and from the warehouse to the report. The systems and tables below are illustrative.
The sources:
sales.: one row per product on an order, withorder_ line order_,id line_,no product_,code quantity,unit_,price discount_,amount tax_andamount currency_;code sales.: one row per order, withorder_ header order_,id customer_,id order_anddate status;crm.:customer customer_and the customer’sid region_, with the dates each region was valid from and to;code ref.: exchange rates to the euro, byfx_ rate currency_andcode rate_.date
Hop 1: from the source to staging
Staging copies the source tables as they are and adds two technical fields. Almost every row is a direct mapping:
Target (stg.) | Source | Type | Rule |
|---|---|---|---|
order_, line_ | sales. | Direct | Copied |
quantity, unit_, discount_, currency_ | sales. | Direct | Copied |
load_ | none | Constant | The time of the load |
source_ | none | Constant | The name of the order system |
Hop 2: from staging to the warehouse
The fact table dw. holds one row per order line whose order was not cancelled:
Target (dw.) | Source fields | Type | Rule |
|---|---|---|---|
order_ | order_, line_ | Derivation | The two together identify one order line |
order_ | order_ | Lookup | Joined on order_ |
customer_ | order_ | Lookup | The surrogate key of the customer; an unknown customer gets the key of the “unknown” row |
region_ | crm. | Lookup | The customer’s region valid on the order date |
net_ | quantity, unit_, discount_ | Derivation | quantity × unit_, with an empty discount counted as 0; tax excluded |
net_ | net_, currency_, fx_ | Lookup and derivation | net_, at the rate for the order’s currency on the order date |
The row filter is order_.
Four source fields influence the result without being copied: status decides which rows arrive, order_ joins lines to their order, and currency_ and order_ choose the exchange rate. A mapping that leaves them out is incomplete.
Hop 3: from the warehouse to the report
| Target (report) | Source fields | Type | Rule |
|---|---|---|---|
| Month | fact_ | Conversion | The calendar month of the order date |
| Sales region | fact_, ref. | Lookup | The region’s name |
| Net revenue (EUR) | fact_ | Aggregation | Summed by month and region |
The lineage of one figure
Read backwards, the three mappings give the column-level lineage of “Net revenue (EUR)”:
- The report sums
dw.by month and region.fact_ sales_ line. net_ amount_ eur net_isamount_ eur net_converted atamount ref., chosen byfx_ rate. rate_ to_ eur currency_andcode order_.date net_isamount quantity × unit_fromprice − discount_ amount sales., throughorder_ line stg..order_ line - Only lines whose order in
sales.is not cancelled are counted.order_ header - The region is the
crm.of the order’s customer, valid on the order date.customer. region_ code
A change to the meaning of status, to the way discounts are stored, or to the source of exchange rates changes the report, although only some of these fields are copied into it. This is why impact analysis needs mappings at field level, with their filters and lookup keys. See data lineage.
How to write a good mapping
- Start from the target. Know who uses each target field and what it should mean; link it to its business term before choosing sources.
- Profile the sources first. Empty values, unexpected codes, duplicates and the real granularity of the data change the rules.
- Write each rule twice: in business words for reviewers, and as an expression or pseudo-code for engineers.
- Cover the unhappy paths: missing values, unknown codes, late or corrected data, duplicates.
- State units, currencies and time zones. “Amount” and “date” are not enough.
- Watch the granularity. Joining order-level values onto order lines and then summing them counts them once per line.
- Record every filter and join, not only the fields that are copied.
- Give each rule a test: expected results for typical and edge cases, and totals that must reconcile between source and target.
- Version and approve. Record who approved the mapping, which version is implemented, and link to the code that implements it.
Keeping mappings and code in step
A mapping written once and never updated becomes misleading at the first change. Three habits keep it true:
- One change, one review. Change the mapping in the same review as the code that implements it.
- Generate or harvest. Generate code from the mapping, or derive the mapping from the code by parsing it; either way, one of them is produced from the other.
- Reconcile each hop. Compare row counts and control totals, such as the sum of net amounts, between source and target after every load.
Common mistakes
- Only the happy path. Rules that say nothing about empty values or unknown codes leave engineers to guess.
- Filters and joins left out. They change results as much as the copied fields, and impact analysis misses them.
- Rules hidden in code. A business rule that exists only in a SQL statement cannot be reviewed by the business.
- Units and time zones unstated. Amounts in different currencies or timestamps in different zones get added up.
- Granularity mismatches. Values duplicated by a join and then summed inflate totals.
- A spreadsheet that drifts. A mapping kept apart from the code stops matching it within a few releases.
- No business meaning on the target. A precisely mapped field that nobody can define is still unusable.
Frequently asked questions
What is a source-to-target mapping?
A source-to-target mapping is a specification that says, for every field of a target table, file, message or report, which source fields its value comes from, how it is transformed, and which filters, joins and lookups apply. It is used to build, test and explain integrations, migrations and reports.
What should a source-to-target mapping include?
For each target field: the target table and field with its data type, the business term it represents, the source fields, the mapping type, the transformation rule in words and as an expression, the filters and joins, the handling of empty and unknown values, the checks applied, and the owner, version and status of the mapping.
What is the difference between a source-to-target mapping and data lineage?
A source-to-target mapping describes one hop: how one target is filled from its sources. Data lineage connects the hops into a path from the original sources to the final reports. Field-level mappings, chained together, are the most precise column-level lineage an organization can have.
Who writes and owns source-to-target mappings?
They are usually written by data analysts or data engineers together with people who know the business rules, and approved by the owner of the target data. Data stewards check that the target fields match their business definitions, and the team that runs the pipeline keeps the mapping and the code in step.
How do you test a source-to-target mapping?
Derive test cases from each rule, including empty values, unknown codes and other edge cases, and compare the target with the expected results. After each load, reconcile row counts and control totals between source and target, and investigate any difference the filters in the mapping do not explain.