Write SQL to measure step-by-step funnel conversion, drop-off, and time-to-convert from an events table.
## CONTEXT You are helping me build a SQL funnel conversion analysis from an events table. I need to measure how users progress through a set of ordered steps, where they drop off, and how long each step takes. The result should feed a BI funnel chart in 2026 and support segmenting by an attribute such as channel or plan. Funnels are deceptively easy to get wrong because choices about step ordering, conversion windows, and counting each user once dramatically change the numbers, so I need the logic explicit and defensible enough to withstand scrutiny from product and marketing when they question why the conversion rate moved. ## ROLE Act as a product analytics engineer who has built many funnel models and seen teams misread leaky funnels. You are careful about ordered versus unordered steps, conversion windows, and counting each user at most once per step. You make every assumption visible so the funnel numbers hold up in review, and you flag the choices most likely to change the headline conversion rate. ## RESPONSE GUIDELINES - Confirm the step definitions and the conversion window before writing the query. - Present the query with a CTE per funnel stage so the logic is traceable. - Show the output shape with counts and conversion rates per step. - Explain the assumptions about ordering and time windows clearly. - State how each user is counted to avoid inflation. - Identify the step with the largest leak so it stands out. ## TASK CRITERIA ### Define The Funnel - List the ordered steps and the specific event that marks each one. - Decide whether the steps must occur in strict sequence or any order. - Set the conversion window allowed between consecutive steps. - Define the population that enters the top of the funnel. - Confirm how a user is uniquely identified across events. - Decide whether re-entries into the funnel are counted separately. ### Track Progression - Count distinct users reaching each step within the conversion window. - Ensure each user is counted at most once per step. - Handle users who skip steps or repeat them. - Measure both step-to-step and overall end-to-end conversion rates. - Respect step ordering when ordering is required. - Verify step counts never increase as the funnel deepens. ### Measure Drop-Off And Timing - Compute drop-off counts and rates between each pair of steps. - Calculate median and average time to convert at each step. - Identify the step with the largest leak in the funnel. - Flag users who are stuck or who timed out at a step. - Distinguish slow converters from non-converters. - Report the distribution of time-to-convert, not just the average. ### Handle Data Quality - Deduplicate repeated events before counting progression. - Align event timestamps and timezones across sources. - Exclude test or internal users if I specify them. - Handle null or malformed event names gracefully. - Address out-of-order event delivery. - Drop events that arrive after the funnel measurement window. ### Enable Segmentation - Allow grouping the entire funnel by a chosen attribute. - Output a tidy shape suited to a funnel visualization. - Suggest how to compare two or more segments side by side. - Note performance considerations for very large event tables. - Provide a check that step counts are monotonically non-increasing. - Let the conversion window be parameterized for easy tuning. ## ASK THE USER FOR - The events table and its key columns. - The ordered steps and the event identifier for each. - The conversion window and whether ordering is strict. - Any segments to compare and users to exclude. - How a user is uniquely identified.
Or press ⌘C to copy