BUSINESS ANALYTICS • DATA WRANGLING AND QUERYING

Creating Derived Variables — Create derived variables and feature columns

Transform raw data into powerful analytic features that drive better business decisions and predictive models.

Historical Context & Motivation

The practice of creating derived variables — new data columns computed from existing ones — has roots that stretch back to the earliest days of systematic business record-keeping. Long before the era of digital spreadsheets, accountants and analysts routinely computed ratios, percentages, and aggregates from raw ledger entries to gain deeper operational insight. What has changed dramatically over the past century is the scale, speed, and sophistication with which organizations can perform these transformations, as well as the range of downstream applications — from descriptive dashboards to machine-learning models — that consume the resulting features.

Understanding this historical trajectory helps explain why feature engineering — the modern discipline of creating and selecting derived variables — is widely regarded as the single most impactful step in any analytics workflow. A well-chosen derived variable can reveal patterns invisible in the raw data, whereas a poorly conceived one can introduce noise and misleading conclusions. The milestones below trace how the concept evolved from manual bookkeeping ratios to automated feature pipelines.

1494
Pacioli's Double-Entry Bookkeeping
Luca Pacioli codified double-entry bookkeeping, establishing the earliest systematic framework for computing derived financial figures such as profit margins and account balances from raw transaction records.
1920s
DuPont Analysis & Financial Ratios
The DuPont Corporation popularized decomposing Return on Equity into derived components — profit margin, asset turnover, and leverage — pioneering ratio-based derived variables in corporate finance.
1979
VisiCalc & the Spreadsheet Revolution
The first electronic spreadsheet enabled business users to create computed columns interactively, making derived variable creation accessible to non-programmers and accelerating ad-hoc analysis.
2001
Pandas & Programmatic Data Wrangling
The Python pandas library (released publicly in 2008, rooted in earlier work) gave analysts a code-based toolkit for rapid column transformations, enabling reproducible feature engineering at scale.
2010s–Present
Automated Feature Engineering
Tools like Featuretools and cloud-based ML platforms introduced automated feature engineering, generating hundreds of derived variables algorithmically and selecting those with the highest predictive power.

The central question this lesson addresses is deceptively simple: given a raw dataset, how do you decide which new columns to create, and how do you implement them reliably? As we will see, the answer draws on domain knowledge, mathematical transformations, and thoughtful validation — skills every business analyst needs.

Core Principles & Definitions

A derived variable (also called a computed column or feature column) is any new field in a dataset whose values are calculated from one or more existing fields rather than collected directly from an original data source. The term feature originates in machine learning and refers to any input variable fed into a model; when that feature is derived rather than raw, we call the creation process feature engineering. In practice, business analysts create derived variables for reports, dashboards, and predictive models alike, so the skill transcends any single tool or framework.

1

Arithmetic Derivation

Combine existing columns through addition, subtraction, multiplication, or division to produce ratios, margins, or totals. Example: profit_margin = net_income / revenue.
2

Temporal Extraction

Parse date or timestamp fields to extract components such as day of the week, month, quarter, or time since a reference event. These features capture seasonality and recency effects.
3

Categorical Encoding

Convert text-based categories into numerical representations — including one-hot encoding, label encoding, or binning continuous values into ordinal groups — so models can process them.
4

Aggregation & Window Functions

Compute rolling averages, cumulative sums, or group-level statistics and attach them back to each row. Example: a customer's average purchase amount over the last 90 days.
5

Interaction & Polynomial Terms

Multiply two features together (interaction) or raise a feature to a power (polynomial) to capture non-linear relationships. Example: ad_spend × conversion_rate.
KEY TAKEAWAY
Think of raw data as crude ingredients in a kitchen. A column of timestamps is like a bag of unpeeled potatoes — useful, but not ready to serve. Derived variables are the prepped ingredients: diced, seasoned, and combined in ways that make the final dish (your analysis or model) far more flavorful and digestible. The better your prep, the better your results.

Visual Explanation — From Raw Data to Derived Features

The diagram shows a three-stage pipeline. On the left, the raw dataset contains order dates, revenue, cost, and region. In the center, four transformations — temporal extraction, arithmetic ratio, one-hot encoding, and interaction — produce the derived columns on the right.

Notice how each transformation addresses a distinct analytical need. The quarter extraction converts a granular timestamp into a categorical seasonality feature, enabling group-by analyses or seasonal dummies in regression. The profit margin normalizes the relationship between revenue and cost into a unit-free ratio, making comparisons across differently sized product lines meaningful. The one-hot indicator translates a categorical label into a binary numeric form consumable by regression and classification algorithms. Finally, the interaction term captures the joint effect of region and profitability — something neither variable reveals on its own.

Mathematical Framework for Derived Variables

While many derived variables arise from business intuition, formalizing the most common transformations helps you communicate precisely with data engineers and ensures reproducibility. Below are the key mathematical patterns you will encounter across virtually all business analytics projects.

RATIO / MARGIN
Margin = (Revenue − Cost) / Revenue
Where Revenue is the top-line sales figure and Cost represents the direct costs. The result is a unit-free proportion typically expressed as a percentage.
GROWTH RATE
Growth Rate = (Xₜ − Xₜ₋₁) / Xₜ₋₁
Where Xₜ is the metric value in the current period and Xₜ₋₁ is the same metric in the prior period. This is also called period-over-period change.
LOGARITHMIC TRANSFORM
X' = ln(X + 1)
Adding 1 avoids taking the log of zero. This transformation is widely used in business analytics to reduce right skew in variables like transaction amounts or web traffic counts, stabilizing variance for regression models.
INTERACTION TERM
X_interaction = X₁ × X₂
An interaction term captures the combined effect of two features. For instance, multiplying a binary is_premium_customer flag by discount_pct creates a feature measuring the discount impact specifically for premium customers.

Each formula above generates a new column in your dataset. In SQL, these appear as expressions in a SELECT clause; in Python's pandas library, they are vectorized operations on DataFrame columns. The underlying mathematical logic is identical regardless of tool — mastering the formula ensures you can implement it anywhere.

Detailed Breakdown — Categories of Derived Variables

In business analytics practice, derived variables can be organized into a taxonomy that guides the analyst in deciding which transformation to reach for given the data type and the analytical objective. The diagram below maps the most common categories and provides representative examples drawn from marketing, finance, and operations data.

This taxonomy organizes derived variables into four families. Arithmetic transformations produce ratios and KPIs. Temporal features capture time-based patterns. Encoding converts categories to numeric form. Aggregate / Window functions inject group-level statistics into row-level data.
Summary of derived variable categories, their input/output types, and representative business examples.
CategoryInput TypeOutput TypeBusiness Example
ArithmeticNumeric columnsNumeric (ratio, difference, log)Customer Lifetime Value = Total Revenue − Total Cost per customer
TemporalDate / timestampInteger or categoricalDays since last purchase (recency in RFM analysis)
EncodingCategorical / textBinary or ordinal numericOne-hot encode product category for logistic regression
Aggregate / WindowNumeric + grouping keyNumeric (mean, sum, rank)Rolling 7-day average order value per store
InteractionTwo or more columnsNumeric productAd spend × click-through rate to capture joint campaign intensity

Worked Example — Building a Customer-Level Feature Set

Suppose you work as an analyst at an e-commerce retailer and need to create a feature-rich dataset for a churn prediction model. Your raw orders table contains customer_id, order_date, order_total, discount_pct, and product_category. The reference date is 2024-09-01. We will derive five new features using SQL.

Deriving Customer Features for Churn Prediction
1
Step 1 — Compute Recency (Temporal)Calculate the number of days between the customer's most recent order and the reference date. In SQL: DATEDIFF('2024-09-01', MAX(order_date)) AS days_since_last_order. A higher value signals that the customer has been inactive for longer, a strong churn indicator.
Customer A: last order 2024-08-10 → days_since_last_order = 22
2
Step 2 — Compute Frequency (Aggregation)Count the total number of orders per customer: COUNT(*) AS total_orders. This captures the frequency dimension of the classic RFM (Recency, Frequency, Monetary) framework.
Customer A placed 14 orders → total_orders = 14
3
Step 3 — Compute Average Order Value (Arithmetic)Divide total revenue by order count: SUM(order_total) / COUNT(*) AS avg_order_value. This derived variable normalizes monetary spend so that a big spender with few large orders is distinguished from a frequent low-value buyer.
Customer A: total spend $1,820 ÷ 14 orders → avg_order_value = $130.00
4
Step 4 — Compute Discount Sensitivity (Arithmetic Ratio)Calculate the proportion of orders where a discount was applied: SUM(CASE WHEN discount_pct > 0 THEN 1 ELSE 0 END) / COUNT(*) AS pct_discounted_orders. Customers who predominantly buy on discount may churn when promotions end.
Customer A: 10 of 14 orders had discounts → pct_discounted_orders = 0.714
5
Step 5 — Create Interaction FeatureMultiply recency by discount sensitivity to capture the idea that an inactive, discount-dependent customer is the highest churn risk: days_since_last_order × pct_discounted_orders AS recency_discount_interaction. This interaction term will surface customers who are both dormant and price-sensitive.
Customer A: 22 × 0.714 → recency_discount_interaction = 15.71
💡 Putting It All Together in SQL
All five features can be computed in a single SELECT statement grouped by customer_id. Wrapping the query in a Common Table Expression (CTE) or a view makes it reusable across multiple downstream analyses. In pandas, the equivalent uses .groupby('customer_id').agg(...) followed by column arithmetic for the interaction term.

Strengths, Limitations & Common Pitfalls

Creating derived variables is one of the most impactful steps in any analytics workflow, but it also introduces risks. Poorly designed features can leak future information into a model, inflate dimensionality, or obscure interpretability. The table below contrasts the strengths of feature engineering with its common pitfalls, followed by practical mitigation strategies.

Strengths vs. limitations of creating derived variables in business analytics.
StrengthsLimitations / Pitfalls
Captures domain knowledge that raw data alone cannot express (e.g., profit margin vs. raw revenue and cost separately)Data leakage: using information from the future or the target variable to create features, invalidating model results
Improves model accuracy often more than algorithm selection; a simple model on great features beats a complex model on raw dataMulticollinearity: derived columns that are linear combinations of existing ones can destabilize regression coefficients
Enables comparability across different business units or time periods through normalization and ratiosCurse of dimensionality: creating too many features without pruning increases noise and training time
Makes reports and dashboards more intuitive by presenting KPIs rather than raw figuresDivision by zero / missing values: ratio-based derivations require guardrails when denominators can be zero or null
Reproducible when coded (SQL, Python) rather than manual; serves as documentation of business logicOver-engineering: excessively complex transformations reduce interpretability and may not generalize to new data
KEY TAKEAWAY
Feature engineering is like seasoning a dish: the right amount transforms a bland meal into something exceptional, but too much overwhelms the palate. Always ask two questions before adding a derived variable: Does this feature genuinely encode new information? and Could this feature introduce leakage or redundancy? If you cannot answer both confidently, pause and validate before proceeding.

Connection to Advanced Feature Engineering & Machine Learning Pipelines

The manual derived-variable techniques covered in this lesson form the foundation of more advanced, automated approaches used in production machine-learning systems. As organizations scale their analytics, they move from ad-hoc SQL expressions to feature stores — centralized repositories that compute, version, and serve derived features consistently across training and inference. Understanding manual feature creation is prerequisite knowledge for working effectively with these systems, because you still need to define the transformation logic and validate its business meaning.

Manual vs. automated feature engineering approaches.
AspectManual Feature Engineering (This Lesson)Automated / Advanced Pipelines
Feature DiscoveryAnalyst uses domain knowledge to hypothesize and create features one at a timeTools like Featuretools generate hundreds of candidate features via Deep Feature Synthesis
Feature SelectionManual review of correlations, VIF, and business logicAutomated selection via mutual information, LASSO regularization, or recursive elimination
ReproducibilityDepends on well-documented SQL/Python scripts or viewsFeature stores version transformations and guarantee train-serve consistency
ScalabilitySuitable for small to medium datasets; analyst effort is the bottleneckDistributed compute (Spark, Dask) handles billions of rows with scheduled pipelines
InterpretabilityHigh — analyst understands each feature's business meaningCan be lower for auto-generated features; requires SHAP/LIME for explanation

As you advance through your analytics coursework, you will encounter techniques like Principal Component Analysis (PCA) for dimensionality reduction, target encoding for high-cardinality categorical variables, and embedding layers in deep learning that learn feature representations automatically. All of these are sophisticated forms of derived-variable creation. The intuition you build now — asking what information a transformation adds and whether it could introduce bias — transfers directly to those advanced contexts.

Practice Problems

PROBLEM 1CONCEPTUAL
A colleague suggests adding a column total_cost = unit_cost × quantity to a sales dataset. Explain why total_cost qualifies as a derived variable rather than a raw variable. What category of derivation does it belong to, and why might this feature be more useful than keeping unit_cost and quantity separate?
PROBLEM 2BASIC CALCULATION
A product had revenue of $48,000 and cost of goods sold (COGS) of $30,000 in Q1. Compute the gross profit margin as a derived variable. Express it as both a decimal and a percentage. Then compute the log-transformed revenue using ln(Revenue + 1).
PROBLEM 3INTERMEDIATE
You have an orders table with columns customer_id, order_date, and order_total. Write a SQL query that creates three derived columns for each customer: (a) order_count, (b) avg_order_value, and (c) days_since_last_order (assuming today is '2024-09-01'). Identify which derivation category each column belongs to.
PROBLEM 4APPLIED
A marketing team provides you with a dataset containing campaign_name (categorical, 8 unique values), ad_spend, impressions, and conversions. They want to predict conversion rate using a linear regression model. Propose at least four derived features you would create, explain the transformation for each, and flag any potential pitfalls (e.g., leakage or multicollinearity).
PROBLEM 5CRITICAL THINKING
An automated feature engineering tool generates 500 derived variables from a dataset of 2,000 customer records. Your colleague argues that more features always improve model accuracy. Critically evaluate this claim. In your response, discuss (a) the curse of dimensionality, (b) the risk of overfitting, (c) how feature selection techniques can mitigate these risks, and (d) why interpretability matters in a business context.

Lesson Summary

Derived variables are new columns computed from existing data, and they are the cornerstone of effective feature engineering in business analytics. The five major categories are arithmetic derivations (ratios, margins, log transforms), temporal extractions (day of week, recency, seasonality flags), categorical encodings (one-hot, label, binning), aggregate and window functions (rolling averages, cumulative sums, group-level statistics), and interaction terms (products of two or more features). Mastering these categories equips you to transform raw transactional data into insight-rich feature sets.

Key formulas include the margin ratio (Revenue − Cost) / Revenue, the growth rate (Xₜ − Xₜ₋₁) / Xₜ₋₁, and the log transform ln(X + 1). When creating derived variables, always guard against data leakage, multicollinearity, and the curse of dimensionality. The best derived variables are those grounded in domain knowledge, validated through statistical testing, and interpretable to business stakeholders — ultimately transforming data wrangling from a chore into the most creative and impactful stage of any analytics project.

Varsity Tutors • Business Analytics • Creating Derived Variables — Create derived variables and feature columns