Your question is Rolling GM% With Late-Arriving Facts. Start with the requirements and the two tables on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
NVIDIA finance analysts need a daily gross-margin view for NVIDIA products by sales region. Write a PostgreSQL query using fact_sales and dim_calendar to calculate a 28-day rolling GM% that remains correct when a sale arrives late or a previously loaded sale is corrected.
loaded_at as the current version of each sale_id.dim_calendar as the reporting date spine, including dates with no sales.gm_percent = (revenue - cogs) / revenue * 100, rounded to two decimals.| Column | Type | Description |
|---|---|---|
| sale_id | BIGINT | Business identifier shared by all versions of a sale |
| sale_date | DATE | Economic date of the sale, related to dim_calendar.calendar_date |
| loaded_at | TIMESTAMP | Timestamp when this fact version entered the warehouse |
| region | VARCHAR(40) | Sales region |
| product | VARCHAR(80) | NVIDIA product family |
| revenue | NUMERIC(14,2) | Sale revenue |
| cogs | NUMERIC(14,2) | Cost of goods sold |
| Column | Type | Description |
|---|---|---|
| calendar_datePK | DATE | Calendar date used as the reporting date spine |