Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

"Previous period" with "Match day of week" picks the later weekday, so the comparison overlaps the selected range

Aperta Adatta ai principianti
#6,802 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
elixir
Ambito
analytics, backend

Direzione di ricerca

Inizia con shift_to_nearest/3 in lib/plausible/stats/comparisons.ex e leggi i casi esistenti di corrispondenza del giorno della settimana in test/plausible/stats/comparisons_test.exs. Il report identifica le differenze di data con segno come causa e suggerisce di confrontare le distanze assolute. Aggiungi un caso di test in cui il giorno della settimana corrispondente precedente sia più vicino, poi esegui i test di comparisons; il lavoro è concluso quando l'intervallo di confronto resta prima del periodo selezionato e i casi esistenti continuano a passare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Watch in BugReels (the date menu opens at 0:04, Compare is clicked at 0:13, and every top stat shows "24 Sep - 5 Oct" under "1 Oct - 12 Oct" from 0:13)

Past Issues Searched
  • I have searched open and closed issues to make sure that the bug has not yet been reported
Issue is a Bug Report
  • This is a bug report and not a feature request, nor asking for self-hosted support
Using official Plausible Cloud hosting or self-hosting?

Plausible Cloud from plausible.io

Describe the bug

With Compare set to "Previous period" and "Match day of week" (the default), the comparison range is moved to the next matching weekday even when the previous one is closer. When the period length leaves a remainder of 4 to 6 days after dividing by 7, the comparison moves forward by 4 to 6 days and overlaps the selected range.

Reproduced on the public demo dashboard https://plausible.io/plausible.io on 2026-10-26. The code is the same on master (57f734e).

Steps:

  1. Open https://plausible.io/plausible.io?period=custom&from=2026-10-01&to=2026-10-12 (Thu 1 Oct to Mon 12 Oct).
  2. Open the date menu and click "Compare". Leave "Previous period" and "Match day of week" as they are.
  3. Read the date lines under the top stats.

Actual: every top stat compares "1 Oct - 12 Oct" with "24 Sep - 5 Oct", so 1 to 5 Oct is counted in both periods. The query API returns the same range:

POST /api/stats/plausible.io/query
{"date_range":["2026-10-01","2026-10-12"],"metrics":["visitors"],"include":{"compare":"previous_period","compare_match_day_of_week":true}}
-> "comparison_date_range":["2026-09-24T00:00:00+00:00","2026-10-05T23:59:59+00:00"]

Built-in periods hit it on some days. On 2026-10-26, "Month to Date" compares 1 Oct - 26 Oct with 10 Sep - 5 Oct, and "Year to Date" compares 1 Jan - 26 Oct with 13 Mar 2025 - 5 Jan 2026. The dashboard also sends compare: previous_period with compare_match_day_of_week: true for the top stats when Compare is off, so the percent change arrows use this range too. In the recording, Unique visitors shows "309k" and "25%" before Compare is turned on, which is 309k against the 247k of 24 Sep - 5 Oct.

Expected behavior

The unshifted previous period is Sat 19 Sep - Wed 30 Sep. The nearest Thursday to 19 Sep is 17 Sep (2 days back, against 5 days forward), so the comparison should be 17 Sep - 28 Sep. The docs describe Previous period as "the period immediately before your selected date range". With the same rule, Month to Date on 2026-10-26 would compare with 3 Sep - 28 Sep and Year to Date with 6 Mar 2025 - 29 Dec 2025.

Cause

shift_to_nearest/3 sorts the two candidate dates by the signed Date.diff:

https://github.com/plausible/analytics/blob/57f734e3dbc5e96d868766dc1e61064984ee3a80/lib/plausible/stats/comparisons.ex#L241-L250

Date.diff(date, next) is always negative and Date.diff(date, previous) is always positive, so the next occurrence always sorts first. For 19 Sep and a Thursday target it compares -5 (24 Sep) with 2 (17 Sep) and picks 24 Sep. This sort has been there since #2822, which introduced the feature to shift the comparison "to start on the same day of the week as the source query".

Sorting by abs(Date.diff(date, &1)) would pick the nearest day. I checked this in a Python model of previous_period, year_over_year and shift_to_nearest, not by running the Elixir test suite. The model reproduces the current ranges above, gives the expected ranges with the change, and gives unchanged results for the match-day-of-week cases in test/plausible/stats/comparisons_test.exs (March 2023 with the rejected start date, February 2023, January 2023, and year over year to 1 March 2023). Those tests only cover cases where the later weekday is also the nearest one.

Screenshots

The replay link above shows the dashboard.

Environment
- OS: Linux
- Browser: Chrome
- Browser Version: 151

This report was drafted with Claude Code (Claude Opus 5.5). The bug was reproduced on the demo, and the cause was checked against the linked source and a Python model of the function.

Lingua principale
Elixir
Stelle
29.7k
Fork
1.9k
Merge medio
3g 22h
PR unite (30g)
50

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di plausible/analytics

Tutte le issue di plausible/analytics

Issue simili

Altre issue su Elixir

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.