Funnel Analysis and Conversion Metrics in SQL
Funnel analysis crops up everywhere. It can be used to analyze success of sales and marketing teams, a customer's progression through a website, or the efficiency of a recruiting pipeline. A famous example in the startup world is David McClure’s AARRR (pirate) framework for product metrics. In general, it is a useful approach to analyzing just about any process that involves multiple steps and attrition.
Some Example Data
Let’s walk through an example involving some mock sales data. In our sales model, we have the following progression of events:
- Lead - a potential client has been identified
- Marketing Qualified - the marketing team has determined that a potential client is a good fit, based on broad characteristics
- Sales Qualified - the sales team has determined that potential client is a good fit, based on conversations with the client
- Negotiated - the sales team has discussed pricing and features with a potential client
- Customer - the sales team has closed new business with client
- Renewed - the client has renewed their contract for the next period
The particulars of a funnel can vary. In the actual Salesforce schema, you will likely be looking at the “opportunity_history” table, and examining steps with names like “contacted,” “qualified,” and “won.” What is most important to making this model work is that the expected value of a client increases as they progress down the funnel, while the raw number of clients decreases.
We’ll use a table with the following fields:
- account_id - a unique identifier for client accounts
- date - date on which an event occurred
- event - type of event, from the above progression
The “events” table contains only accounts that were opened in fiscal year 2018. Cohort analysis is a subject for another post.
The simplest thing you might consider doing is this:
SELECT event, COUNT(*) as event_count FROM samplesales.events GROUP BY event ORDER BY COUNT(*) DESC
A straightforward aggregation, and a great start for getting the absolute magnitudes of each step. But what if we want percentages?
WITH grouped_events as (SELECT event, COUNT(*) as event_count FROM samplesales.events GROUP BY event) SELECT event, event_count, event_count/(SELECT MAX(event_count) FROM grouped_events) as event_percentage FROM grouped_events ORDER BY event_count DESC
We’ve turned the first query into a common table expression, and then referred to a specific value from it. And what if we want to measure the percentage of prospective clients who are lost at each step.? We add a window function called “lag” that returns the preceding data for each row.
WITH grouped_events as (SELECT event, COUNT(*) as event_count FROM samplesales.events GROUP BY event) SELECT event, event_count, event_count/(SELECT MAX(event_count) FROM grouped_events) as total_percentage, lag(event_count, 1) over (ORDER BY event_count DESC) as above_count FROM grouped_events ORDER BY event_count DESC
Then, let’s divide
above_count to get the percentage of clients who convert between each step in the funnel:
WITH grouped_events as (SELECT event, COUNT(*) as event_count FROM samplesales.events GROUP BY event) SELECT event, event_count, event_count/(SELECT MAX(event_count) FROM grouped_events) as total_percentage, event_count/lag(event_count, 1) over (ORDER BY event_count DESC) as percentage_survival_by_step FROM grouped_events ORDER BY event_count DESC
Note that, at about 38%, the transition from negotiation to registration is proportionally the most serious choke point in the funnel, although serious attrition takes place at every step. An impactful intervention here might be to focus on changing negotiation tactics used by our sales team.
Fivetran offers connectors across a range of business operations. Funnel analysis is one method among several that, combined with multiple connectors, will allow you to build a comprehensive representation of how your company is performing in matters of marketing, sales, customer success, recruiting, and more.
Check out a demo of Fivetran so that we can help you bring data literacy to every facet of your business.