Skip to content

Commit 2133826

Browse files
Merge pull request #19 from Ritik574-coder/dbt_branch
feat(intermediate): build customer profile, location, contact, and segmentation models
2 parents cd0abfb + 413b65c commit 2133826

5 files changed

Lines changed: 222 additions & 78 deletions

File tree

models/intermediate/Customers/int_customer_location.sql

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
-- ============================================================================
22
-- Model: int_customer_location
3-
3+
--
44
-- Purpose:
5-
-- Standardizes customer location attributes by correcting common data quality
6-
-- issues such as missing values, typographical errors, inconsistent casing,
7-
-- invalid state abbreviations, invalid ZIP codes, and non-standard country
8-
-- names. This model prepares clean and consistent geographic information for
9-
-- downstream analytical models.
10-
11-
-- Maintainer: Ritik
5+
-- Creates a trusted customer location dataset by standardizing geographic
6+
-- attributes and resolving common data quality issues across customer
7+
-- addresses. This model corrects city name spelling errors, normalizes
8+
-- address formatting, validates state and ZIP code information, and
9+
-- standardizes country values into business-defined categories. The
10+
-- resulting dataset provides reliable geographic attributes for customer
11+
-- segmentation, regional reporting, sales analysis, and downstream
12+
-- dimensional models.
13+
--
14+
-- Maintainer: Ritik
1215
-- ============================================================================
1316

1417
-- dbt.TitleCase is a custom function taht help me to convert string into title case

models/intermediate/Customers/int_customer_profile.sql

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
-- Model: int_customer_profile
33
--
44
-- Purpose:
5-
-- Standardizes customer profile attributes by cleaning and transforming
6-
-- personal information such as titles, names, gender, and date of birth.
7-
-- This model normalizes inconsistent values, extracts first and last names,
8-
-- standardizes gender categories, and converts multiple date formats into a
9-
-- consistent DATE data type for reliable downstream analytical models.
5+
-- Creates a standardized customer profile dataset by applying business
6+
-- rules to personal customer attributes. This model derives first and last
7+
-- names from the full name, normalizes gender values into consistent
8+
-- categories, and converts multiple source date formats into a reliable
9+
-- DATE data type. The resulting dataset serves as a trusted foundation for
10+
-- customer demographics, segmentation, personalization, and downstream
11+
-- dimensional models.
1012
--
1113
-- Maintainer: Ritik
1214
-- ============================================================================
Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,57 @@
1+
-- ============================================================================
2+
-- Model: int_customer_segmentation
3+
--
4+
-- Purpose:
5+
-- Creates a standardized customer segmentation dataset by applying business
6+
-- rules to customer profile and account attributes. This model normalizes
7+
-- customer segments, imputes missing annual income using segment-level
8+
-- median values, standardizes company names and account status values, and
9+
-- converts inconsistent account creation dates into a reliable DATE format.
10+
-- The resulting dataset serves as a trusted foundation for customer
11+
-- segmentation, loyalty analysis, marketing analytics, and dimensional
12+
-- modeling.
13+
--
14+
-- Maintainer: Ritik
15+
-- ============================================================================
116
SELECT
217
customer_id,
3-
customer_segment,
4-
loyalty_points,
5-
annual_income_usd,
6-
company,
7-
is_active,
8-
account_created_date
18+
CASE
19+
WHEN customer_segment IS NULL OR customer_segment = '' THEN 'Unknown'
20+
ELSE customer_segment
21+
END as customer_segment, -- distinct value is Bronze Gold Platinum Silver
22+
23+
TRY_CAST(loyalty_points AS INT) as loyalty_points,
24+
25+
COALESCE(
26+
annual_income_usd,
27+
PERCENTILE_CONT(0.5)
28+
WITHIN GROUP (ORDER BY annual_income_usd)
29+
OVER (PARTITION BY customer_segment)
30+
) as annual_income_usd,
31+
32+
CASE
33+
WHEN company IS NULL OR company = '' THEN 'Unknown'
34+
WHEN TRIM(REPLACE(REPLACE(company, CHAR(13), ''), CHAR(10), '')) = '' THEN 'Unknown'
35+
ELSE TRIM(REPLACE(REPLACE(company, CHAR(13), ''), CHAR(10), ''))
36+
END as company,
37+
38+
CASE
39+
WHEN TRIM(LOWER(is_active)) IN ('1', 'active', 'true', 'y', 'yes') THEN 'True'
40+
WHEN TRIM(LOWER(is_active)) IN ('0', 'inactive', 'false', 'n', 'no') THEN 'False'
41+
ELSE 'Unknown'
42+
END AS is_active,
43+
44+
CASE
45+
WHEN TRIM(account_created_date) LIKE '[A-Z][a-z][a-z] __, ____' THEN CONVERT(DATE,account_created_date)
46+
WHEN TRIM(account_created_date) LIKE '[A-Z][a-z][a-z][a-z]% __, ____' THEN CONVERT(DATE,account_created_date)
47+
WHEN TRIM(account_created_date) LIKE '____/__/__' THEN CONVERT(DATE,account_created_date)
48+
WHEN TRIM(account_created_date) LIKE '____-__-__' THEN CONVERT(DATE,account_created_date)
49+
50+
WHEN TRIM(account_created_date) LIKE '__-__-____' AND TRY_CONVERT(INT, LEFT(account_created_date,2)) > 12 THEN TRY_CONVERT(DATE, account_created_date, 105)
51+
WHEN TRIM(account_created_date) LIKE '__-__-____' AND TRY_CONVERT(INT, SUBSTRING(account_created_date,4,2)) > 12 THEN TRY_CONVERT(DATE, account_created_date, 110)
52+
WHEN TRIM(account_created_date) LIKE '__/__/____' AND TRY_CONVERT(INT, LEFT(account_created_date,2)) > 12 THEN TRY_CONVERT(DATE, account_created_date, 103)
53+
WHEN TRIM(account_created_date) LIKE '__/__/____' AND TRY_CONVERT(INT, SUBSTRING(account_created_date, 4, 2)) > 12 THEN TRY_CONVERT(DATE, account_created_date, 101)
54+
ELSE TRY_CONVERT(DATE, account_created_date,101)
55+
END as account_created_date
56+
957
FROM {{ ref('stg_customers') }} ;

models/intermediate/Customers/int_customers_contact.sql

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
-- Model: int_customers_contact
33
--
44
-- Purpose:
5-
-- Standardizes customer contact information by cleaning and validating
6-
-- email addresses, phone numbers, and preferred communication channels.
7-
-- This model corrects common email domain typographical errors, formats
8-
-- phone numbers into a consistent structure, and normalizes customer
9-
-- communication preferences for reliable downstream analytical models.
5+
-- Creates a trusted customer contact dataset by validating, correcting, and
6+
-- standardizing customer communication attributes. This model repairs common
7+
-- email formatting issues and domain typographical errors, transforms phone
8+
-- numbers into a consistent business format, and normalizes preferred
9+
-- communication channels into standardized business categories. The resulting
10+
-- dataset provides reliable contact information for customer communication,
11+
-- marketing campaigns, CRM operations, and downstream analytical models.
1012
--
1113
-- Maintainer: Ritik
1214
-- ============================================================================

0 commit comments

Comments
 (0)