DAX去年至今 [英] DAX Last Year to Date
问题描述
所以我知道这个问题已经问过几次了,我虔诚地研究了不同的方法,但是我仍然不太明白为什么我得到的结果不正确。
So I know this question has been asked a few times, and I've religiously looked over different approaches, however I still don't quite understand why I'm getting an incorrect result.
案例:我有从〜2016-> 2019年的销售数据(直到2/18/2019)我有一个度量来显示年初至今,但是我正在寻找一个度量迄今为止的最后几年(在这种情况下为第18个)。
Case: I have Sales Data from ~2016 -> 2019 (up until the 2/18/2019) I'm have a Measure to show me the YTD, however I'm looking for a measure for Last Years to date(the 18th in this particular circumstance).
现在,我有:
Total Sales LYTD =
CALCULATE (
[Total Sales],
SAMEPERIODLASTYEAR (
FILTER (
VALUES ( Sales[Completed Date] ),
Sales[Completed Date] <= MAX ( Sales[Completed Date] )
)
)
)
对我来说,逻辑很有意义,但我敢肯定我错过了一些东西,看来它在整个2018年吸引了我, m寻找01/01/2018-> 2/18/2018
The logic to me makes sense, but I'm sure I'm missing something, has it appears it's grabbing the ENTIRE total of 2018 when in reality i'm looking for 01/01/2018 -> 2/18/2018
动态地上传新的销售数据
This is going to be dynamically uploaded with new sales data
我缺少什么?
推荐答案
不确定我是否了解您的表设置,因此让我们看一下这种情况,希望对您有帮助。
Not sure I understand your table setup so lets look at this scenario and hopefully it helps.
假设您有两个表Sales和Calendar中的数据,并且日历和sales表之间存在1:*的关系。然后我将这样写:
Suppose you have the data in two tables, Sales and Calendar, and there's a 1:* relationship between the calendar and the sales tables. Then I would write the measures like this:
SalesToDateThisYear =
calculate(
Sum(Sales[Sales]);
Calendar[Year] = Year(Today())
)
and
SalesToDateLastYear =
var dateLastYear = Today() - 365
return
calculate(
Sum(Sales[Sales]);
Calendar[Year] = Year(dateLatsYear);
Calendar[Date] < dateLastYear
)
两个过滤器参数与逻辑AND组合。因此,只包括从去年第一天到去年今天的日期。
The two filter arguments are combined with a logic AND. So only dates from the first of last year to today's date last year will be included.
如果要使用SamePeriod函数,您可能可以编写如下内容
If you want to use the SamePeriod-function you can probably write something like this
SPLY =
calculate =
Sum(Sales[Sales]);
SamePeriodLastYear(
Filter(
Values(Calendar[Date]);
Calendar[Date] >= Date(year(today()); 1; 1) && Calendar[Date] < Today()
)
)
)
SamePeriod函数采用一组日期(今年)并将其转换为去年的日期。
The SamePeriod-function takes a set of dates (this year) and converts them to dates last year.
欢呼声
这篇关于DAX去年至今的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!