FHIR to OMOP ETL: The Mapping Is Easy. Keys and Deletes Aren't.
A FHIR to OMOP ETL is a derived build. Stable surrogate keys, concept_id 0 coverage, delete and merge propagation, and DQD as a CI gate.
Key takeaways
- Treat OMOP as a derived build from the raw FHIR layer, so you can drop and rebuild every CDM table without losing anything.
- Use a persisted key map, not auto-increment sequences or pure hashes, so person_id and visit_occurrence_id survive rebuilds and server moves.
- Gate the build on DQD fatal checks, fail on regressions in the full suite, and track per-domain concept_id 0 coverage alongside.
OMOP should be a build output. If you can't drop every CDM table and rebuild it from raw FHIR without losing anything that matters, you've created a second source of truth. It will drift from the first one.
I've written up how to get HL7v2 into Snowflake and into Google Cloud. This post is the build I'd put on top of that raw layer when a research team asks for OMOP. Resource-to-table mapping is covered well elsewhere. Four other things decide whether the instance stays trustworthy: stable keys, unmapped codes, deletes and merges, and a quality gate that can fail the build.
FHIR vs OMOP: keep both, as separate layers
If clinical data already lives in your warehouse, you run both. The real question is which one is the truth. It should be raw FHIR. OMOP is derived.
That rule settles a lot of arguments early. Vocabulary fixes go in the mapping code. Nobody hand-edits condition_occurrence. A new OHDSI vocabulary release means a rebuild. Nothing writes to the CDM except the build. I made the same argument about graph databases: the derived copy is never the truth.
Be honest about how far from the source you already are. The HL7 FHIR to OMOP Implementation Guide points out that most observational data isn't born as FHIR. Much of it has already been transformed once before OMOP sees it. If your FHIR came from an HL7v2 feed, OMOP is the second lossy hop. Every mapping decision from the first conversion is baked in before you write a line of OMOP SQL.
How to map FHIR to OMOP with dbt
Use dbt models, or SQL-on-FHIR views that feed dbt. Write one model per CDM table and keep them all in git. Follow the patterns in the Vulcan FHIR to OMOP IG. The current published version is 1.0.0, with Informative status. Treat it as well-researched advice, not a spec you get certified against.
Three IG rules belong in your models from day one.
Filter on status in the pipeline. OMOP records clinical facts, so planned, cancelled and in-progress items stay out. The IG says to put these filters in the pipeline so the same rules apply to every incremental load for the life of the instance. A one-off cleanup script doesn't count.
Keep source values exact. Original codes go into _source_value character for character, spacing included. The IG ties this to incremental maintenance and to remapping when vocabularies change. If you store a trimmed or upper-cased code, you can't join it back to the source later.
Use _type_concept_id for provenance. A MedicationStatement is patient-reported. A MedicationRequest is a prescription, and it doesn't prove the drug was taken. OMOP can't tell them apart unless you set the type concept.
A common pattern puts a lineage column (resource type, id, versionId) on every OMOP row. I wouldn't. The IG recommends a separate mapping table outside the core schema, and extra columns on CDM tables may trip DQD conformance checks. Keep lineage in a sidecar table. The next section needs one anyway.
Stable OMOP person_id and visit_occurrence_id across rebuilds
Most OMOP builds hand out integer keys from an auto-increment sequence, and the IG says so. When you rebuild, patient 10042 can become patient 88317. Every saved cohort and every exported person list keyed on person_id now points at the wrong human.
The obvious fix is to hash ResourceType + Resource.id into an integer. That's better, but the IG warns that Resource.id is server-scoped and may change when data migrates to a new FHIR server. A pure hash can re-key your whole population the day you switch vendors. Logical IDs are strings of up to 64 characters, often UUIDs, so hashing them into a 64-bit bigint also leaves room for collisions.
Persist the mapping instead. Allocate an ID once, the first time a resource shows up, and never allocate it again:
create table if not exists omop_meta.key_map (
omop_table varchar not null, -- 'person', 'visit_occurrence'
fhir_type varchar not null, -- 'Patient', 'Encounter'
fhir_id varchar not null, -- Resource.id, never an MRN
omop_id bigint not null,
last_version_id varchar,
first_seen timestamp not null,
primary key (fhir_type, fhir_id)
);
-- allocate IDs for resources never seen before
insert into omop_meta.key_map
select 'visit_occurrence', 'Encounter', e.id,
(select coalesce(max(omop_id), 0) from omop_meta.key_map
where omop_table = 'visit_occurrence')
+ row_number() over (order by e.id),
e.version_id, current_timestamp
from raw_fhir.encounter e
left join omop_meta.key_map k
on k.fhir_type = 'Encounter' and k.fhir_id = e.id
where k.fhir_id is null;
-- record the latest version for lineage
update omop_meta.key_map k
set last_version_id = e.version_id
from raw_fhir.encounter e
where k.fhir_type = 'Encounter' and k.fhir_id = e.id
and k.last_version_id is distinct from e.version_id;
The key map lives outside the CDM schema, so a rebuild never drops it. Run allocation from a single writer so two jobs can't grab the same max. If you migrate FHIR servers, write a one-time remap from old fhir_id to new fhir_id in this table. Don't let the migration allocate new IDs.
This table also covers the IG's traceability recommendation, which is a table that maps each OMOP table and ID back to a ResourceType/id. The update step adds the version. One structure gives you stable keys and lineage.
Never derive keys from business identifiers. The IG calls MRNs and encounter numbers not appropriate for key derivation, because they carry PII and vary in format. It also says to keep them out of visit_source_value and similar fields. They can overflow the typical VARCHAR(50), and one resource can carry several of them. If you need them for audit, put them in a separate access-controlled table.
OMOP unmapped codes: concept_id 0 and mapping coverage
Local codes that don't map to a standard concept get concept_id 0 plus the exact source value. Don't drop the row, and don't guess a mapping. The IG frames concept 0 as what lets queries tell mapped data from unmapped. That turns coverage into a number you can track.
Track two numbers per domain, per build. The first is the unmapped row rate, for example avg(case when condition_concept_id = 0 then 1.0 else 0 end) over condition_occurrence. The second is the count of distinct condition_source_value values on those rows. Run the same query shape for measurement, drug_exposure, procedure_occurrence and observation.
The row rate tells you how much research data is invisible. The distinct-code count tells you how much mapping work is left. Sort the unmapped codes by row count and fix them from the top, because that's where the invisible rows are.
FHIR data-absent-reason doesn't map to a concept either. The IG suggests keeping the original null meaning, like not-permitted, in value_source_value. A missing value and a value withheld on purpose are different facts.
I'll let an LLM suggest mappings. I won't let it write them. Every suggestion goes into a review queue with a status. It starts as suggested and ends as approved or rejected, with the reviewer and timestamp recorded. Only approved rows get written to your local source-to-concept mappings, and nothing touches the OHDSI-distributed vocabulary tables. It's the same rule from HL7v2 to FHIR Conversion With an LLM: Mapping, Not Messages: the model proposes at design time, a person approves, and the pipeline only runs approved rules.
Incremental OMOP loads, deletes and patient merges
The source changes its mind all the time. Three cases have to propagate:
- Entered-in-error. A resource that flips to
entered-in-errorwas never a clinical fact. Its OMOP row has to disappear on the next incremental run. Waiting for the next full rebuild is too late. - Deleted resources. If the raw layer only appends, a deleted resource looks exactly like one that stopped changing. Record each deletion as an event in a raw deletions table and anti-join it out of every model. Where those events come from depends on your source. Check whether your server's export or history API reports deletes. If it doesn't, the fallback is a periodic reconciliation: any ID in raw that the server no longer returns becomes a deletion event.
- Merged patients. Sometimes two Patient records turn out to be one person. The losing record's conditions, visits and measurements then move to the survivor's
person_id, and the losingperson_iddrops out ofperson. Keep a merge table so you can answer "where did patient 10042 go?" Cohorts built on the old ID need that answer.
The simplest way to get all three right is to make every OMOP model a pure function of current raw state. Here's the core of a measurement model built from Observation:
with latest as (
select *
from raw_fhir.observation
qualify row_number() over (
partition by id order by last_updated desc) = 1
),
live as (
select l.*
from latest l
left join raw_fhir.deletions d
on d.fhir_type = 'Observation' and d.fhir_id = l.id
where d.fhir_id is null
and l.status in ('final', 'amended', 'corrected') -- policy choice; drops entered-in-error
)
select
km.omop_id as measurement_id,
coalesce(mg.surviving_person_id, pk.omop_id) as person_id,
o.code_value as measurement_source_value
-- concept lookups, dates, values ...
from live o
join omop_meta.key_map km
on km.fhir_type = 'Observation' and km.fhir_id = o.id
join omop_meta.key_map pk
on pk.fhir_type = 'Patient' and pk.fhir_id = o.patient_id
left join omop_meta.person_merge mg
on mg.losing_person_id = pk.omop_id
Four steps, in order: take the latest version, apply the status filter, exclude deleted IDs, and remap merged patients through the merge table.
The status list is a policy choice, not a standard. Pick yours from the Observation.status values, write it into your ETL documentation (the IG recommends documenting assumptions like this), and use the same list in every model that reads Observation.
Resolve merge chains (A into B, then B into C) when you write person_merge, so the join always lands on the final survivor. The person model does the inverse and excludes every losing_person_id.
Incremental dbt models with merge semantics are a speed optimization on top of this, and the logic stays the same. If an incremental run and a full rebuild ever produce different row counts, the incremental path is wrong. That comparison makes a cheap nightly test.
Running the OHDSI Data Quality Dashboard in CI, including on Snowflake
If OMOP is a build, it gets a build gate. It's the same principle as letting an agent write FHIR interface code: the build doesn't get to say done until the checks do.
DQD has a severity tier for this. Fatal checks should never fail because they cover the relational integrity of the CDM. The checkSeverity parameter runs only those checks, which makes them a hard stop:
library(DataQualityDashboard)
res <- executeDqChecks(
connectionDetails = cd,
cdmDatabaseSchema = "cdm",
resultsDatabaseSchema = "dqd",
cdmSourceName = Sys.getenv("BUILD_ID"),
outputFolder = "dqd-out",
checkSeverity = "fatal"
)
failed <- subset(res$CheckResults, failed == 1)
if (nrow(failed) > 0) {
print(failed[, c("checkName", "cdmTableName", "cdmFieldName")])
quit(status = 1)
}
Pin DQD to 2.8.8 or later. That release fixed a bug where running only fatal checks made passed-check counts show as zero, and fatal-only is exactly the mode CI runs in. The DQD release pages don't agree on which version is latest, so pin by the exact tag you install.
The full check suite is the second stage. It won't be all green on real data, and an absolute gate there teaches people to ignore it. Write each run's results to a table with writeJsonResultsToTable. Then fail the build only when a check that passed on the last green build fails now. When a check regresses, OHDSI's MiimansaDQD can write the rows behind each failure to a separate file when you set verboseOutput = TRUE.
On Snowflake, there's an open issue, #660, for a bug in the FIELD_CDM_DATATYPE check SQL. Record it as a known failure in your baseline so it doesn't block the regression gate. Don't let it teach people to skip the report.
Put the coverage numbers from the unmapped-codes section in the same report. A build that passes every DQD check while the unmapped measurement rate doubles has still regressed. DQD doesn't know your local codes.
Questions people ask
How do you keep OMOP person_id stable when rebuilding from FHIR?
Allocate each ID once, the first time a resource is seen, and store it in a key map table outside the CDM schema that rebuilds never drop. Auto-increment sequences reshuffle IDs on rebuild, and hashing Resource.id can break if you migrate FHIR servers, because logical IDs may change.
What should a FHIR to OMOP ETL do with codes that don't map to a standard concept?
Give them concept_id 0 and keep the original code character for character in the source_value field. Track the unmapped row rate and the count of distinct unmapped codes per domain, and fix the highest-volume codes first.
How do entered-in-error, deleted and merged FHIR resources get removed from OMOP?
Build every OMOP model as a function of current raw state: take the latest version, filter on status, anti-join a deletions table, and remap merged patients through a merge table. If an incremental run and a full rebuild produce different row counts, the incremental path is wrong.
Can the OHDSI Data Quality Dashboard run as a CI gate on Snowflake?
Yes. Run DQD with checkSeverity set to fatal as a hard gate, then run the full suite as a second stage that fails only when a previously passing check fails. On Snowflake, record open issue #660 in the FIELD_CDM_DATATYPE check as a known failure in your baseline.