refactor: fix mart_patient_360 — eliminate cartesian explosion, hash SSN, add financial metrics - #1
Open
altimateanas wants to merge 1 commit into
Conversation
…ancial metrics - Fix cartesian product: pre-aggregate encounters, diagnoses, medications, labs, and claims to patient grain before joining - HIPAA compliance: replace raw ssn with sha2(ssn) as ssn_hash - Add stg_providers + stg_claims as new upstream dependencies - Rename full_name -> patient_name (sourced from stg_patients) - Add primary_provider_name (now derived from stg_providers via encounters) - Add financial columns: lifetime_charges_dollars, lifetime_paid_dollars using the cents_to_dollars macro - Add patient_id as explicit primary key (was missing entirely) - Remove raw diagnosis_code, replace with aggregated metrics - Add schema documentation to _marts.yml (was completely missing)
SQL Quality Gate ResultsChecked SQL files in this PR for common anti-patterns:
See job logs for details. Powered by MedFlow SQL Quality Gate |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Major refactor of
mart_patient_360to fix critical data quality and HIPAA compliance issues.Problems Fixed
patient_idwas not in the SELECT list. Added as explicit first column.ssnwas exposed as plain text. Replaced withsha2(ssn, 256) as ssn_hash.stg_claimsas new upstream +lifetime_charges_dollars/lifetime_paid_dollarsusing thecents_to_dollarsmacro (which existed but was never used).full_namepatient_name(fromstg_patients). Addedprimary_provider_name(derived fromstg_providersvia encounter history).mart_patient_360was not listed in_marts.ymlat all. Added full column-level documentation.Column-Level Changes
patient_idprimary_provider_namestg_providers.full_namevia encounter joinblood_type,insurance_idtotal_encounters,first_encounter_date,last_encounter_dateunique_diagnosis_count,primary_diagnosis_codesunique_medication_counttotal_lab_tests,abnormal_lab_countlifetime_charges_dollars,lifetime_paid_dollarspatient_registered_atcreated_atfor clarityfull_name→patient_namestg_patients), clearer namessn→ssn_hashstg_patients.ssn, but now SHA-256 hasheddiagnosis_codeunique_diagnosis_count+primary_diagnosis_codesNew Upstream Dependencies
stg_providers— to deriveprimary_provider_namestg_claims— to compute financial summaryAny dashboard, report, or model that reads
mart_patient_360should be checked for:ssncolumn no longer exists — replaced byssn_hashfull_namecolumn no longer exists — renamed topatient_namediagnosis_codecolumn no longer exists — replaced by aggregated columns