Skip to content

Commit ac383ca

Browse files
author
David Shimamoto
authored
Fix enrollment being exploded to row per member-month. Adjust testing on PK. (#885)
* Fix enrollment being exploded to row per member-month. Adjusted unique value check on enrollment and member_month. * Linting * Revert testing changes * Added member_months to models.yml * Add stg core member months to models yml
1 parent 2b966b6 commit ac383ca

10 files changed

Lines changed: 184 additions & 174 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ release_notes/
1515
# IDE and editor files
1616
.idea/
1717
.vscode/
18+
dbt_internal_packages/
1819

1920
# Operating system files
2021
.DS_Store

macros/cross_database_utils/create_json_object.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,4 @@ select
159159
) as {{ object_col_name }}
160160
from {{ table_ref }}
161161
group by {{ group_by_col }}
162-
{% endmacro %}
162+
{% endmacro %}

models/claims_preprocessing/claims_enrollment/claims_enrollment__flag_claims_with_enrollment.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ select distinct
6060
, claim.inferred_claim_start_column_used
6161
, cast('{{ var('tuva_last_run') }}' as {{ dbt.type_timestamp() }}) as tuva_last_run
6262
from claim_year_month as claim
63-
inner join {{ ref('normalized_input__eligibility') }} as mm
63+
inner join {{ ref('claims_enrollment__member_months') }} as mm
6464
on mm.person_id = claim.person_id
6565
and mm.member_id = claim.member_id
6666
and mm.payer = claim.payer

models/claims_preprocessing/claims_enrollment/claims_enrollment__flag_rx_claims_with_enrollment.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ select distinct
3939
, claim.paid_year_month
4040
, cast('{{ var('tuva_last_run') }}' as {{ dbt.type_timestamp() }}) as tuva_last_run
4141
from claim_dates as claim
42-
inner join {{ ref('normalized_input__eligibility') }} as mm
42+
inner join {{ ref('claims_enrollment__member_months') }} as mm
4343
on mm.person_id = claim.person_id
4444
and mm.member_id = claim.member_id
4545
and mm.payer = claim.payer
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{{ config(
2+
enabled = var('claims_preprocessing_enabled',var('claims_enabled',var('tuva_marts_enabled',False)))
3+
| as_bool
4+
)
5+
}}
6+
7+
8+
with stg_eligibility as (
9+
select
10+
person_id
11+
, member_id
12+
, payer
13+
, {{ quote_column('plan') }}
14+
, data_source
15+
, enrollment_start_date
16+
, enrollment_end_date
17+
, tuva_last_run
18+
from {{ ref('normalized_input__eligibility') }} as elig
19+
)
20+
21+
, month_start_and_end_dates as (
22+
select
23+
{{ concat_custom(["year",
24+
dbt.right(concat_custom(["'0'", "month"]), 2)]) }} as year_month
25+
, min(full_date) as month_start_date
26+
, max(full_date) as month_end_date
27+
from {{ ref('reference_data__calendar') }}
28+
group by year, month, year_month
29+
)
30+
select distinct
31+
dense_rank() over (
32+
order by
33+
a.person_id
34+
, b.year_month
35+
, a.payer
36+
, a.{{ quote_column('plan') }}
37+
, a.data_source
38+
) as member_month_key
39+
, a.person_id
40+
, a.member_id
41+
, b.year_month
42+
, a.payer
43+
, a.{{ quote_column('plan') }}
44+
, a.data_source
45+
, a.tuva_last_run
46+
from stg_eligibility as a
47+
inner join month_start_and_end_dates as b
48+
on a.enrollment_start_date <= b.month_end_date
49+
and a.enrollment_end_date >= b.month_start_date

models/claims_preprocessing/claims_preprocessing_models.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,35 @@ models:
7272
- name: paid_year_month
7373
description: the year and month of the date the pharmacy claim was paid.
7474

75+
- name: claims_enrollment__member_months
76+
description: This table contains member months matching enrollment
77+
config:
78+
schema: |
79+
{%- if var('tuva_schema_prefix',None) != None -%}{{var('tuva_schema_prefix')}}_claims_preprocessing
80+
{%- else -%}claims_preprocessing{%- endif -%}
81+
alias: member_months
82+
tags:
83+
- claims_preprocessing
84+
materialized: table
85+
columns:
86+
- name: member_month_key
87+
description: Unique identifier for each member month.
88+
- name: person_id
89+
description: Unique identifier for each patient in the dataset.
90+
- name: member_id
91+
description: >
92+
Identifier that links a patient to a particular insurance product or
93+
health plan. A patient can have more than one member_id because they
94+
can have more than one insurance product/plan.
95+
- name: year_month
96+
description: The year and month of the member month.
97+
- name: payer
98+
description: Name of the payer.
99+
- name: plan
100+
description: Name of the plan.
101+
- name: data_source
102+
description: '{{ doc("data_source") }}'
103+
75104

76105

77106
### Normalized Claim Input Layer

models/claims_preprocessing/normalized_input/final/normalized_input__eligibility.sql

Lines changed: 32 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -5,112 +5,36 @@
55
}}
66

77

8-
with stg_eligibility as (
9-
select
10-
cast(elig.person_id as {{ dbt.type_string() }}) as person_id
11-
, cast(elig.member_id as {{ dbt.type_string() }}) as member_id
12-
, cast(elig.subscriber_id as {{ dbt.type_string() }}) as subscriber_id
13-
, cast(elig.gender as {{ dbt.type_string() }}) as gender
14-
, cast(elig.race as {{ dbt.type_string() }}) as race
15-
, cast(date_norm.normalized_birth_date as date) as birth_date
16-
, cast(date_norm.normalized_death_date as date) as death_date
17-
, cast(elig.death_flag as int) as death_flag
18-
, cast(date_norm.normalized_enrollment_start_date as date) as enrollment_start_date
19-
, cast(date_norm.normalized_enrollment_end_date as date) as enrollment_end_date
20-
, cast(elig.payer as {{ dbt.type_string() }}) as payer
21-
, cast(elig.payer_type as {{ dbt.type_string() }}) as payer_type
22-
, cast(elig.{{ quote_column('plan') }} as {{ dbt.type_string() }}) as {{ quote_column('plan') }}
23-
, cast(elig.original_reason_entitlement_code as {{ dbt.type_string() }}) as original_reason_entitlement_code
24-
, cast(elig.dual_status_code as {{ dbt.type_string() }}) as dual_status_code
25-
, cast(elig.medicare_status_code as {{ dbt.type_string() }}) as medicare_status_code
26-
, cast(elig.group_id as {{ dbt.type_string() }}) as group_id
27-
, cast(elig.group_name as {{ dbt.type_string() }}) as group_name
28-
, cast(elig.first_name as {{ dbt.type_string() }}) as first_name
29-
, cast(elig.last_name as {{ dbt.type_string() }}) as last_name
30-
, cast(elig.social_security_number as {{ dbt.type_string() }}) as social_security_number
31-
, cast(elig.subscriber_relation as {{ dbt.type_string() }}) as subscriber_relation
32-
, cast(elig.address as {{ dbt.type_string() }}) as address
33-
, cast(elig.city as {{ dbt.type_string() }}) as city
34-
, cast(elig.state as {{ dbt.type_string() }}) as state
35-
, cast(elig.zip_code as {{ dbt.type_string() }}) as zip_code
36-
, cast(elig.phone as {{ dbt.type_string() }}) as phone
37-
, cast(elig.data_source as {{ dbt.type_string() }}) as data_source
38-
, cast('{{ var('tuva_last_run') }}' as {{ dbt.type_string() }}) as tuva_last_run
39-
from {{ ref('normalized_input__stg_eligibility') }} as elig
40-
left outer join {{ ref('normalized_input__int_eligibility_dates_normalize') }} as date_norm
41-
on elig.person_id_key = date_norm.person_id_key
42-
)
43-
44-
, month_start_and_end_dates as (
45-
select
46-
{{ concat_custom(["year",
47-
dbt.right(concat_custom(["'0'", "month"]), 2)]) }} as year_month
48-
, min(full_date) as month_start_date
49-
, max(full_date) as month_end_date
50-
from {{ ref('reference_data__calendar') }}
51-
group by year, month, year_month
52-
)
53-
54-
, member_month_calc as (
55-
select distinct
56-
dense_rank() over (
57-
order by
58-
a.person_id
59-
, b.year_month
60-
, a.payer
61-
, a.{{ quote_column('plan') }}
62-
, a.data_source
63-
) as member_month_key
64-
, a.person_id
65-
, b.year_month
66-
, a.payer
67-
, a.{{ quote_column('plan') }}
68-
, a.data_source
69-
from stg_eligibility as a
70-
inner join month_start_and_end_dates as b
71-
on a.enrollment_start_date <= b.month_end_date
72-
and a.enrollment_end_date >= b.month_start_date
73-
)
74-
758
select
76-
m.member_month_key
77-
, a.person_id
78-
, a.member_id
79-
, a.subscriber_id
80-
, a.gender
81-
, a.race
82-
, a.birth_date
83-
, a.death_date
84-
, a.death_flag
85-
, a.enrollment_start_date
86-
, a.enrollment_end_date
87-
, a.payer
88-
, a.payer_type
89-
, a.{{ quote_column('plan') }}
90-
, b.year_month
91-
, a.original_reason_entitlement_code
92-
, a.dual_status_code
93-
, a.medicare_status_code
94-
, a.group_id
95-
, a.group_name
96-
, a.first_name
97-
, a.last_name
98-
, a.social_security_number
99-
, a.subscriber_relation
100-
, a.address
101-
, a.city
102-
, a.state
103-
, a.zip_code
104-
, a.phone
105-
, a.data_source
106-
, a.tuva_last_run
107-
from stg_eligibility as a
108-
inner join month_start_and_end_dates as b
109-
on a.enrollment_start_date <= b.month_end_date
110-
and a.enrollment_end_date >= b.month_start_date
111-
inner join member_month_calc as m
112-
on m.person_id = a.person_id
113-
and m.payer = a.payer
114-
and m.{{ quote_column('plan') }} = a.{{ quote_column('plan') }}
115-
and m.data_source = a.data_source
116-
and m.year_month = b.year_month
9+
cast(elig.person_id as {{ dbt.type_string() }}) as person_id
10+
, cast(elig.member_id as {{ dbt.type_string() }}) as member_id
11+
, cast(elig.subscriber_id as {{ dbt.type_string() }}) as subscriber_id
12+
, cast(elig.gender as {{ dbt.type_string() }}) as gender
13+
, cast(elig.race as {{ dbt.type_string() }}) as race
14+
, cast(date_norm.normalized_birth_date as date) as birth_date
15+
, cast(date_norm.normalized_death_date as date) as death_date
16+
, cast(elig.death_flag as int) as death_flag
17+
, cast(date_norm.normalized_enrollment_start_date as date) as enrollment_start_date
18+
, cast(date_norm.normalized_enrollment_end_date as date) as enrollment_end_date
19+
, cast(elig.payer as {{ dbt.type_string() }}) as payer
20+
, cast(elig.payer_type as {{ dbt.type_string() }}) as payer_type
21+
, cast(elig.{{ quote_column('plan') }} as {{ dbt.type_string() }}) as {{ quote_column('plan') }}
22+
, cast(elig.original_reason_entitlement_code as {{ dbt.type_string() }}) as original_reason_entitlement_code
23+
, cast(elig.dual_status_code as {{ dbt.type_string() }}) as dual_status_code
24+
, cast(elig.medicare_status_code as {{ dbt.type_string() }}) as medicare_status_code
25+
, cast(elig.group_id as {{ dbt.type_string() }}) as group_id
26+
, cast(elig.group_name as {{ dbt.type_string() }}) as group_name
27+
, cast(elig.first_name as {{ dbt.type_string() }}) as first_name
28+
, cast(elig.last_name as {{ dbt.type_string() }}) as last_name
29+
, cast(elig.social_security_number as {{ dbt.type_string() }}) as social_security_number
30+
, cast(elig.subscriber_relation as {{ dbt.type_string() }}) as subscriber_relation
31+
, cast(elig.address as {{ dbt.type_string() }}) as address
32+
, cast(elig.city as {{ dbt.type_string() }}) as city
33+
, cast(elig.state as {{ dbt.type_string() }}) as state
34+
, cast(elig.zip_code as {{ dbt.type_string() }}) as zip_code
35+
, cast(elig.phone as {{ dbt.type_string() }}) as phone
36+
, cast(elig.data_source as {{ dbt.type_string() }}) as data_source
37+
, cast('{{ var('tuva_last_run') }}' as {{ dbt.type_string() }}) as tuva_last_run
38+
from {{ ref('normalized_input__stg_eligibility') }} as elig
39+
left outer join {{ ref('normalized_input__int_eligibility_dates_normalize') }} as date_norm
40+
on elig.person_id_key = date_norm.person_id_key

models/core/core_models.yml

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,16 @@ models:
9292
alias: eligibility
9393
tags: core
9494
materialized: table
95+
tests:
96+
- dbt_utils.unique_combination_of_columns:
97+
combination_of_columns:
98+
- eligibility_id
99+
- data_source
95100
columns:
96101
- name: eligibility_id
97102
description: '{{ doc("eligibility_id") }}'
98103
tests:
99-
- unique:
100-
config:
101-
severity: warn
104+
- not_null
102105
- name: person_id
103106
description: '{{ doc("person_id") }}'
104107
tests:
@@ -143,6 +146,8 @@ models:
143146
description: '{{ doc("group_name") }}'
144147
- name: data_source
145148
description: '{{ doc("data_source") }}'
149+
tests:
150+
- not_null
146151
- name: tuva_last_run
147152
description: '{{ doc("tuva_last_run") }}'
148153

@@ -704,6 +709,11 @@ models:
704709
tags:
705710
- core
706711
materialized: table
712+
tests:
713+
- dbt_utils.unique_combination_of_columns:
714+
combination_of_columns:
715+
- member_month_key
716+
- data_source
707717
columns:
708718
- name: member_month_key
709719
description: '{{ doc("member_month_key") }}'
@@ -723,6 +733,8 @@ models:
723733
description: '{{ doc("plan") }}'
724734
- name: data_source
725735
description: '{{ doc("data_source") }}'
736+
tests:
737+
- not_null
726738
- name: tuva_last_run
727739
description: '{{ doc("tuva_last_run") }}'
728740
- name: payer_attributed_provider
@@ -1158,6 +1170,16 @@ models:
11581170
tags: core_stage_claims
11591171
materialized: table
11601172

1173+
- name: core__stg_claims_member_months
1174+
description: Staging core member months
1175+
config:
1176+
schema: |
1177+
{%- if var('tuva_schema_prefix',None) != None -%}{{var('tuva_schema_prefix')}}_core
1178+
{% else %}core{%- endif -%}
1179+
alias: _stg_claims_member_months
1180+
tags: core_stage_claims
1181+
materialized: view
1182+
11611183
- name: core__stg_claims_patient
11621184
description: Staging core claims patients
11631185
config:

models/core/final/core__member_months.sql

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,4 @@
33
)
44
}}
55

6-
with final_before_attribution_fields as (
7-
select distinct
8-
a.member_month_key
9-
, a.person_id
10-
, a.member_id
11-
, a.year_month
12-
, a.payer
13-
, a.{{ quote_column('plan') }}
14-
, data_source
15-
, '{{ var('tuva_last_run') }}' as tuva_last_run
16-
from {{ ref('normalized_input__eligibility') }} as a
17-
)
18-
19-
, add_attribution_fields as (
20-
select
21-
a.member_month_key
22-
, a.person_id
23-
, a.member_id
24-
, a.year_month
25-
, a.payer
26-
, a.{{ quote_column('plan') }}
27-
, a.data_source
28-
, a.tuva_last_run
29-
30-
, b.payer_attributed_provider
31-
, b.payer_attributed_provider_practice
32-
, b.payer_attributed_provider_organization
33-
, b.payer_attributed_provider_lob
34-
, b.custom_attributed_provider
35-
, b.custom_attributed_provider_practice
36-
, b.custom_attributed_provider_organization
37-
, b.custom_attributed_provider_lob
38-
39-
from final_before_attribution_fields as a
40-
left outer join {{ ref('financial_pmpm__stg_provider_attribution') }} as b
41-
on a.person_id = b.person_id
42-
and a.year_month = b.year_month
43-
and a.payer = b.payer
44-
and a.{{ quote_column('plan') }} = b.{{ quote_column('plan') }}
45-
and a.data_source = b.data_source
46-
)
47-
48-
select
49-
member_month_key
50-
, person_id
51-
, member_id
52-
, year_month
53-
, payer
54-
, {{ quote_column('plan') }}
55-
, data_source
56-
, tuva_last_run
57-
, payer_attributed_provider
58-
, payer_attributed_provider_practice
59-
, payer_attributed_provider_organization
60-
, payer_attributed_provider_lob
61-
, custom_attributed_provider
62-
, custom_attributed_provider_practice
63-
, custom_attributed_provider_organization
64-
, custom_attributed_provider_lob
65-
from add_attribution_fields
6+
select * from {{ ref('core__stg_claims_member_months') }}

0 commit comments

Comments
 (0)