Entitx

Lineage and integration

Source-to-target mapping: a worked example from source system to report

  • Updated
  • 10 min read
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:

ItemWhat it recordsExample
TargetThe system, table and field being filleddw.fact_sales_line.net_amount_eur
Target data typeThe type, length and precisionDECIMAL(18,2)
Business termThe glossary term the field representsNet revenue of an order line, in euros
Source fieldsEvery field the value is derived fromquantity, unit_price, discount_amount
Mapping typeHow the value is producedLookup and derivation
Transformation ruleThe rule in business words and as an expressionNet amount converted at the rate of the order date
FiltersThe conditions that decide which rows arriveCancelled orders are left out
Joins and lookupsThe keys used to combine data or translate valuesExchange rate by currency and order date
Empty values and defaultsWhat happens when a source value is missing or unknownAn empty discount counts as 0
ChecksThe quality checks applied on the wayThe net amount is not negative
Owner, version, statusWho approves it, which version, whether it is implementedDraft, approved, implemented

Mapping types

TypeWhat happensExample
DirectThe value is copied as it is, possibly under a new nameorder_id to order_id
ConversionThe type, format or unit changesA text date becomes a date; cents become currency units
DerivationThe value is calculated from one or more fieldsQuantity times unit price, minus the discount
LookupThe value is translated through reference data or a key mapA currency code becomes an exchange rate; a customer number becomes a surrogate key
AggregationMany rows are summed or counted into oneRevenue summed by month and region
ConditionalThe value depends on a conditionA status code turned into “open” or “closed”
Constant or defaultA fixed value, or a fallback when the source has noneThe source system’s name; “unknown” for a missing region
Row filterNo value is copied, but the field decides which rows arriveOnly 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.order_line: one row per product on an order, with order_id, line_no, product_code, quantity, unit_price, discount_amount, tax_amount and currency_code;
  • sales.order_header: one row per order, with order_id, customer_id, order_date and status;
  • crm.customer: customer_id and the customer’s region_code, with the dates each region was valid from and to;
  • ref.fx_rate: exchange rates to the euro, by currency_code and 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.order_line)SourceTypeRule
order_id, line_nosales.order_lineDirectCopied
quantity, unit_price, discount_amount, currency_codesales.order_lineDirectCopied
load_tsnoneConstantThe time of the load
source_systemnoneConstantThe name of the order system

Hop 2: from staging to the warehouse

The fact table dw.fact_sales_line holds one row per order line whose order was not cancelled:

Target (dw.fact_sales_line)Source fieldsTypeRule
order_line_keyorder_id, line_noDerivationThe two together identify one order line
order_dateorder_header.order_dateLookupJoined on order_id
customer_keyorder_header.customer_idLookupThe surrogate key of the customer; an unknown customer gets the key of the “unknown” row
region_codecrm.customer.region_codeLookupThe customer’s region valid on the order date
net_amountquantity, unit_price, discount_amountDerivationquantity × unit_price − discount_amount, with an empty discount counted as 0; tax excluded
net_amount_eurnet_amount, currency_code, fx_rate.rate_to_eurLookup and derivationnet_amount × rate_to_eur, at the rate for the order’s currency on the order date

The row filter is order_header.status <> 'CANCELLED'.

Four source fields influence the result without being copied: status decides which rows arrive, order_id joins lines to their order, and currency_code and order_date choose the exchange rate. A mapping that leaves them out is incomplete.

Hop 3: from the warehouse to the report

Target (report)Source fieldsTypeRule
Monthfact_sales_line.order_dateConversionThe calendar month of the order date
Sales regionfact_sales_line.region_code, ref.region.region_nameLookupThe region’s name
Net revenue (EUR)fact_sales_line.net_amount_eurAggregationSummed by month and region

The lineage of one figure

Read backwards, the three mappings give the column-level lineage of “Net revenue (EUR)”:

  1. The report sums dw.fact_sales_line.net_amount_eur by month and region.
  2. net_amount_eur is net_amount converted at ref.fx_rate.rate_to_eur, chosen by currency_code and order_date.
  3. net_amount is quantity × unit_price − discount_amount from sales.order_line, through stg.order_line.
  4. Only lines whose order in sales.order_header is not cancelled are counted.
  5. The region is the crm.customer.region_code of the order’s customer, valid on the order date.

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

  1. Start from the target. Know who uses each target field and what it should mean; link it to its business term before choosing sources.
  2. Profile the sources first. Empty values, unexpected codes, duplicates and the real granularity of the data change the rules.
  3. Write each rule twice: in business words for reviewers, and as an expression or pseudo-code for engineers.
  4. Cover the unhappy paths: missing values, unknown codes, late or corrected data, duplicates.
  5. State units, currencies and time zones. “Amount” and “date” are not enough.
  6. Watch the granularity. Joining order-level values onto order lines and then summing them counts them once per line.
  7. Record every filter and join, not only the fields that are copied.
  8. Give each rule a test: expected results for typical and edge cases, and totals that must reconcile between source and target.
  9. 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.