You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DAX同比对比需求:实现与今年同期而非整月的去年销售计数对比

Fixing Same Period Last Year for Incomplete Months in DAX

Great question—this is a super common pain point when working with time intelligence in Power BI/DAX, especially when dealing with partial periods like your 2018 January data (only up to the 16th). Let's break down how to fix this.

The Root of the Problem

Your original PY Sales Count uses SAMEPERIODLASTYEAR, which works perfectly for full periods but fails here because it returns the entire matching period from last year (all of January 2017) instead of matching the exact date range of your incomplete current period (January 1–16, 2018).

The Solution: Match Exact Date Ranges

We need to create a measure that dynamically captures the exact date range of your current data, then maps that range to the same dates in the previous year. Here are two robust approaches:

Approach 1: Dynamic Date Range Mapping (Most Flexible)

This method works for any date range (days, weeks, partial months, etc.) by converting each date in your current context to its equivalent in the prior year:

PY Sales Count :=
VAR CurrentDateRange = DATESBETWEEN(dCalendar[Dates], MIN(dCalendar[Dates]), MAX(dCalendar[Dates]))
VAR PriorYearDateRange = 
    SELECTCOLUMNS(
        CurrentDateRange,
        "PriorYearDate", DATE(YEAR([Dates]) - 1, MONTH([Dates]), DAY([Dates]))
    )
RETURN
CALCULATE([Sales Count], PriorYearDateRange)

How It Works:

  1. CurrentDateRange grabs all dates in your current filter context (e.g., Jan 1–16, 2018)
  2. PriorYearDateRange converts each of those dates to the same day/month in the prior year (Jan 1–16, 2017)
  3. CALCULATE runs your original Sales Count measure against this filtered prior year range

Approach 2: Partial Month Specific (Simpler for Monthly Views)

If you only need this for partial months, you can explicitly calculate the start and end dates of the prior year's matching partial period:

PY Sales Count (Partial Month) :=
VAR CurrentMaxDate = MAX(dCalendar[Dates])
VAR PriorYearEndDate = DATE(YEAR(CurrentMaxDate) - 1, MONTH(CurrentMaxDate), DAY(CurrentMaxDate))
VAR PriorYearStartDate = DATE(YEAR(PriorYearEndDate), MONTH(PriorYearEndDate), 1)
RETURN
CALCULATE(
    [Sales Count],
    DATESBETWEEN(dCalendar[Dates], PriorYearStartDate, PriorYearEndDate)
)

How It Works:

  1. CurrentMaxDate gets the last date with data in your current context (Jan 16, 2018)
  2. PriorYearEndDate calculates the same day/month in the prior year (Jan 16, 2017)
  3. PriorYearStartDate sets the start of the prior year's month (Jan 1, 2017)
  4. CALCULATE filters the prior year to Jan 1–16, 2017 and runs your sales count

Testing the Measure

When you select January 2018 in your report, both measures will return sales counts from only January 1–16, 2017 instead of the entire month, perfectly aligning with your current period's data.

内容的提问来源于stack exchange,提问作者Dries Weytjens

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:52:23