From cfff95bff2eaefacd1461074f483cc2de4af8a09 Mon Sep 17 00:00:00 2001 From: Christian Vidal Wolf Date: Wed, 28 Jan 2026 13:24:29 +0100 Subject: [PATCH] Fix Global Weights: Fallback to flat distribution if reference data is sparse (<4 months) --- services/dataProcessor.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/services/dataProcessor.ts b/services/dataProcessor.ts index 89c33a8..591b8da 100644 --- a/services/dataProcessor.ts +++ b/services/dataProcessor.ts @@ -1658,15 +1658,26 @@ export const calculateForecastViewData = ( const getGlobalWeightsInner = (records: SalesRecord[]) => { const weights = new Array(12).fill(0); let total = 0; + const seenMonths = new Set(); + records.forEach(r => { const m = r.month.split('-')[0]; const idx = MONTH_ORDER.indexOf(m); if (idx !== -1) { weights[idx] += r.units; total += r.units; + if (r.units > 0) seenMonths.add(m); } }); - return total === 0 ? new Array(12).fill(1 / 12) : weights.map(w => w / total); + + // SAFETY NET: Even for Global Weights, if the reference file (2025 Sales) + // has fewer than 4 months of data (e.g. user only uploaded Jan 2025), + // we should NOT assume 100% seasonality in those months. Fallback to flat. + if (total === 0 || seenMonths.size < 4) { + return new Array(12).fill(1 / 12); + } + + return weights.map(w => w / total); }; const panEuWeights = getGlobalWeightsInner(panEuData2025);