Business analytics often requires aggregating time-series data into meaningful periods for reporting and decision-making. This drill teaches you to parse dates, group transactions by month, calculate aggregate statistics, and format currency. You'll learn date manipulation, hash-based grouping, aggregation functions, and business reporting—essential skills for building dashboards, analytics tools, and financial reports.
Date.parse(string) converts date string to Date object
date.strftime('%Y-%m') formats date as 'YYYY-MM'
Hash.new { |h, k| h[k] = [] } creates hash with array default values
array.sum calculates total of all elements
hash.keys.sort sorts month strings alphabetically (which is chronological)
hash.values.flatten combines all arrays into one
Format decimals: '%.2f' % number
Add commas: reverse string, insert commas every 3 digits, reverse back
analyze_sales('sales.csv')
MONTHLY SALES REPORT ================================================== Month: 2024-01 Transactions: 3 Total Sales: $1,419.97 Average: $473.32 Month: 2024-02 Transactions: 3 Total Sales: $1,779.97 Average: $593.32 Month: 2024-03 Transactions: 4 Total Sales: $1,869.96 Average: $467.49 -------------------------------------------------- OVERALL SUMMARY Total Transactions: 10 Total Sales: $5,069.90 Average per Transaction: $506.99
analyze_sales('sales.csv')
MONTHLY SALES REPORT ================================================== Month: 2024-05 Transactions: 3 Total Sales: $450.00 Average: $150.00 -------------------------------------------------- OVERALL SUMMARY Total Transactions: 3 Total Sales: $450.00 Average per Transaction: $150.00
analyze_sales('sales.csv')
MONTHLY SALES REPORT ================================================== Month: 2024-06 Transactions: 2 Total Sales: $8,500.50 Average: $4,250.25 Month: 2024-07 Transactions: 3 Total Sales: $5,501.00 Average: $1,833.67 -------------------------------------------------- OVERALL SUMMARY Total Transactions: 5 Total Sales: $14,001.50 Average per Transaction: $2,800.30
Console output will appear here...
Are you sure?
You're making great progress