cswork / libraryOpen practice workspace

Coding questions · Intuit

Pivot daily users and revenue by platform

This question evaluates data manipulation and aggregation skills, specifically computing distinct user counts and revenue aggregates and reshaping results by platform; it is in the Data Manipulation (SQL/Python) domain and assesses practical application rather than purely conceptual understanding.

You are given transaction-level data and need daily aggregates by platform.

Input (Pandas DataFrame)

df with columns:

  • file_date (DATE or string parseable to date) — the activity date
  • id (INT/STRING) — user id
  • sku (STRING) — product identifier
  • price (NUMERIC) — revenue for the row (assume already in a single currency)
  • channel (STRING)
  • customer_segment (STRING)
  • platform (STRING) — e.g., 'web', 'mobile'

Example rows:

  • 01-01-2024, 123, A, 57, seo, alpha, web
  • 01-07-2024, 943, C, 100, tv, alpha, mobile

Task

Compute, for each file_date and platform:

  • total_users = number of distinct id
  • total_revenue = sum of price

Then reshape the result into a pivoted/wide daily report with one row per day and separate columns per platform:

  • file_date
  • web_total_users, web_total_revenue
  • mobile_total_users, mobile_total_revenue

(If additional platforms exist, include them similarly.)

Pivot daily users and revenue by platform

Table df

columntypeconstraints
file_dateDATE
idINTEGER
skuVARCHAR
priceDECIMAL(10,2)
channelVARCHAR
customer_segmentVARCHAR
platformVARCHAR

Sample data:

channelcustomer_segmentfile_dateidplatformpricesku
seoalpha2024-01-01123web57A
tvalpha2024-01-07943mobile100C

Expected output

One row per file_date with distinct user counts and total revenue pivoted into separate columns per platform. If a platform has no activity on a day, its values should be 0.

file_datemobile_total_revenuemobile_total_usersweb_total_revenueweb_total_users
2024-01-0100571
2024-01-07100100

Hints

  1. First aggregate by file_date and platform to compute COUNT(DISTINCT id) and SUM(price).
  2. Use conditional aggregation (CASE WHEN platform = 'web' THEN ... END) to pivot into wide columns.
  3. Wrap pivoted expressions with COALESCE(...) to replace NULLs with 0.
  4. If more platforms exist, add corresponding CASE/COALESCE expressions for each.