diff --git a/macros/substring.sql b/macros/substring.sql new file mode 100644 index 000000000..64cf63e94 --- /dev/null +++ b/macros/substring.sql @@ -0,0 +1,31 @@ +{% macro substring(string, start, length) %} + {{ return(adapter.dispatch('substring', 'macros')(string, start, length)) }} +{% endmacro %} + +{% macro default__substring(string, start, length) %} + substring({{ string }}, {{ start }}, {{ length }}) +{% endmacro %} + +{% macro postgres__substring(string, start, length) %} + substring({{ string }} from {{ start }} for {{ length }}) +{% endmacro %} + +{% macro snowflake__substring(string, start, length) %} + substr({{ string }}, {{ start }}, {{ length }}) +{% endmacro %} + +{% macro bigquery__substring(string, start, length) %} + substr({{ string }}, {{ start }}, {{ length }}) +{% endmacro %} + +{% macro redshift__substring(string, start, length) %} + substring({{ string }}, {{ start }}, {{ length }}) +{% endmacro %} + +{% macro databricks__substring(string, start, length) %} + substring({{ string }}, {{ start }}, {{ length }}) +{% endmacro %} + +{% macro duckdb__substring(string, start, length) %} + substring({{ string }}, {{ start }}, {{ length }}) +{% endmacro %} \ No newline at end of file diff --git a/models/cms_hcc/cms_hcc_models.yml b/models/cms_hcc/cms_hcc_models.yml index 01e1ffad8..2013718b2 100644 --- a/models/cms_hcc/cms_hcc_models.yml +++ b/models/cms_hcc/cms_hcc_models.yml @@ -536,4 +536,30 @@ models: - name: birth_date description: Date the patient was born. - name: death_date - description: The death date of the patient if there is one. \ No newline at end of file + description: The death date of the patient if there is one. + - name: cms_hcc__stg_financial_pmpm__member_months + config: + schema: | + {%- if var('tuva_schema_prefix',None) != None -%}{{var('tuva_schema_prefix')}}_cms_hcc{% else %}cms_hcc{%- endif -%} + alias: _stg_member_months + tags: cms_hcc + materialized: ephemeral + description: Staging member months from financial_pmpm + columns: + - name: patient_id + description: Unique identifier for each patient in the dataset. + - name: year_month + description: Unique year-month of in the dataset computed from eligibility. + - name: payer + description: Name of the payer (i.e. health insurer) providing coverage. + - name: plan + description: Name of the plan (i.e. sub contract) providing coverage. + - name: data_source + description: > + User-configured field that indicates the data source (e.g. typically + named after the payer and state "BCBS Tennessee"). + - name: tuva_last_run + description: > + The last time the data was refreshed. Generated by + `dbt_utils.pretty_time` as the local time of the `dbt run` + environment. Timezone is configurable via the `tuva_last_run` var. \ No newline at end of file diff --git a/models/cms_hcc/final/cms_hcc__patient_risk_scores.sql b/models/cms_hcc/final/cms_hcc__patient_risk_scores.sql index 01cbc8a87..08ab66e3b 100644 --- a/models/cms_hcc/final/cms_hcc__patient_risk_scores.sql +++ b/models/cms_hcc/final/cms_hcc__patient_risk_scores.sql @@ -24,6 +24,18 @@ with seed_adjustment_rates as ( ) +, member_months as ( + + select + patient_id + , cast({{ substring('year_month', 1, 4) }} as integer) as eligible_year + , COUNT(1) as member_months + from {{ ref('cms_hcc__stg_financial_pmpm__member_months') }} + group by + patient_id + , cast({{ substring('year_month', 1, 4) }} as integer) +) + , raw_score as ( select @@ -135,6 +147,24 @@ with seed_adjustment_rates as ( ) +, weighted_score as ( + + select + payment.patient_id + , payment.v24_risk_score + , payment.v28_risk_score + , payment.blended_risk_score + , payment.normalized_risk_score + , payment.payment_risk_score + , member_months.member_months + , payment.payment_risk_score * member_months.member_months as payment_risk_score_weighted_by_months + , payment.payment_year + from payment + left join member_months + on payment.patient_id = member_months.patient_id + and payment.payment_year = member_months.eligible_year +) + , add_data_types as ( select @@ -144,8 +174,10 @@ with seed_adjustment_rates as ( , round(cast(blended_risk_score as {{ dbt.type_numeric() }}),3) as blended_risk_score , round(cast(normalized_risk_score as {{ dbt.type_numeric() }}),3) as normalized_risk_score , round(cast(payment_risk_score as {{ dbt.type_numeric() }}),3) as payment_risk_score + , round(cast(payment_risk_score_weighted_by_months as {{ dbt.type_numeric() }}),3) as payment_risk_score_weighted_by_months + , cast(member_months as integer) as member_months , cast(payment_year as integer) as payment_year - from payment + from weighted_score ) @@ -156,6 +188,8 @@ select , blended_risk_score , normalized_risk_score , payment_risk_score + , payment_risk_score_weighted_by_months + , member_months , payment_year , '{{ var('tuva_last_run')}}' as tuva_last_run from add_data_types \ No newline at end of file diff --git a/models/cms_hcc/staging/cms_hcc__stg_financial_pmpm__member_months.sql b/models/cms_hcc/staging/cms_hcc__stg_financial_pmpm__member_months.sql new file mode 100644 index 000000000..8daa8d466 --- /dev/null +++ b/models/cms_hcc/staging/cms_hcc__stg_financial_pmpm__member_months.sql @@ -0,0 +1,12 @@ +{{ config( + enabled = var('cms_hcc_enabled',var('financial_pmpm_enabled', var('claims_enabled',var('tuva_marts_enabled',False)))) | as_bool + ) +}} +select + patient_id + , year_month + , payer + , plan + , data_source + , '{{ var('tuva_last_run')}}' as tuva_last_run +from {{ ref('financial_pmpm__member_months') }} \ No newline at end of file