Compare Google Cloud Month-to-Date Spend With the Prior Month
Last updated: November 29, 2026
Quick Overview
For each billing account, compute month-to-date net cost through yesterday and the cost for the same span of the previous month, handling months of different lengths, and report growth.
0
0
0 solved
For each billing account, compute month-to-date net cost through yesterday and the cost for the same span of the previous month, handling months of different lengths, and report growth.
Cloud billing dashboards in the Google Cloud console show month-to-date against the prior month, and getting the date math right is the whole problem. The interviewer checks complete-day cutoffs, month length edge cases (Mar 30 versus February) and single-pass conditional aggregation.
What the Interviewer Expects
- Use only complete days, in the billing time zone
- Define the prior-month window aligned to the same day of month
- Handle shorter prior months and the last day of a month
- Compute both totals in one scan with conditional SUM
- Guard the growth division
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- What happens when the query runs on the first day of a month?
- How would you forecast the end-of-month total from month to date?
- Why might comparing day 1 to 15 of each month still mislead, and what would you use instead?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
daily_costs(billing_account_id, cost_date, net_cost) with one row per account, day and service. Billing days are in Pacific time, so "yesterday" is computed there. Month to date covers the first of ...
Approach
Build both windows in a small CTE. The prior window starts at the first of the previous month and ends at yesterday minus one month; BigQuery clamps invalid dates, so Mar 30 maps to Feb 28. One extra ...