Roll Up Ad Spend to Ad Set and Campaign Subtotals

Last updated: November 29, 2026

Quick Overview

Ad-level daily spend rows carry account_id, campaign_id, ad_set_id and ad_id. Produce a single report table containing ad set subtotals, campaign subtotals and account totals, each labeled with its level, sorted so each subtotal follows its children, and with each row's share of its parent.

Meta
Data Manipulation (SQL/Python)
Data Scientist
Meta
November 29, 2026
Data Scientist
Take-home Project
Data Manipulation (SQL/Python)
Medium

0

0

0 solved


Ad-level daily spend rows carry account_id, campaign_id, ad_set_id and ad_id. Produce a single report table containing ad set subtotals, campaign subtotals and account totals, each labeled with its level, sorted so each subtotal follows its children, and with each row's share of its parent.

Ads Manager presents spend as an account, campaign, ad set, ad hierarchy, and analysts often rebuild that rollup in notebooks. Meta take-homes include this to test multi-level groupby, concatenating levels with consistent columns (like SQL ROLLUP), and computing parent shares without mismatched joins.

What the Interviewer Expects
  • Aggregate at each hierarchy level with groupby
  • Align columns across levels so they can be concatenated
  • Label each row's level and sort into a readable order
  • Compute share of parent with a groupby transform at the parent level
  • Verify subtotals reconcile with the grand total
Key Topics to Cover
multi-level groupby
rollup
concat
share of parent
hierarchy
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you express this with SQL GROUPING SETS or ROLLUP?
  • How would you handle an ad set that was moved between campaigns mid-month?
  • How would you add week-over-week deltas at each level?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

ads(account_id, campaign_id, ad_set_id, ad_id, spend). Each ad set belongs to one campaign and each campaign to one account. Output columns: level, account_id, campaign_id, ad_set_id, `spend...

Approach

Compute three aggregates: by (account, campaign, ad_set), by (account, campaign), and by account. Share of parent at the ad set level is spend over the campaign total via transform('sum') on campaig...


Submit Your Answer
Markdown supported