简体   繁体   English

DAX计算年初至今-上个月的年份(年份更改)

[英]DAX Calculate Year To Date - Year to Previous Month (year change)

Trying to figure out how to calculate the equivalent to YTD but up til previous month when year changes. 试图弄清楚如何计算与年初至今的等效值,但要等到年份更改时才计算到上个月。

For example: 例如:

Date    | Value
2018-10 | 100 
2018-11 | 100 
2018-12 | 100 
2019-01 | 100
2019-02 | 100
2019-03 | 100

Results expected: 预期结果:

When 2019-03 is selected 当选择2019-03时

YTD = 300 (accumulated from 2019-01- to 2019-03) 年初至今= 300(从2019-01-到2019-03累积)

Previous month accumulated = 200 (accumulated from 2019-01 to 2019-02) 前一个月累计= 200(从2019-01到2019-02累计)

When 2019-01 is selected 当选择2019-01

YTD = 100 年初至今= 100

Previous month accumulated = 300 (accumulated from 2018-10 to 2018-12) 前一个月累计= 300(从2018-10到2018-12累计)

I've tried: 我试过了:

Value_Accum_PreviousMonth:=TOTALYTD([Value];Calendar[Date]) - [Value]

but with the change of year, it doesn't work 但是随着年份的变化,它不起作用

Any ideas? 有任何想法吗?

Disclaimer - I created this solution using Power BI. 免责声明-我使用Power BI创建了此解决方案。 The formula should transfer over to Excel/PowerPivot, but it may require some minor tweaks. 该公式应转移到Excel / PowerPivot,但可能需要进行一些细微调整。


While the time intelligence functions of DAX are super useful, I seem to be drawn towards the manual approach of calculating YTD, prior year, etc. With that in mind, this is how I would solve your problem. 虽然DAX的时间智能功能非常有用,但我似乎倾向于采用手动方法计算YTD,上一年等。因此,这就是我要解决的问题。

First, I would make a measure that is a simply sum of your Value column. 首先,我将做一个仅是您的Value列之和的度量。 Having this measure is just nice down the road; 采取这种措施在以后很不错。 not an absolute necessity. 并非绝对必要。

Total Value = SUM(Data[Value])

Next, for use to calculate YTD manually based of the prior month, we need to know two things, 1) what is the target month (ie the prior month) and 2) what is the year of that month. 接下来,要用于基于前一个月手动计算YTD,我们需要知道两件事:1)什么是目标月份(即,前一个月份)以及2)什么是该月的年份。

If you are going to use those values anywhere else, I would put them into there own measure and use them here. 如果您打算在其他任何地方使用这些值,我会把它们放在自己的度量标准中并在这里使用。 If this is the only place they will be used, I like to use variables to calculate these kinds of values. 如果这是唯一使用它们的地方,我喜欢使用变量来计算这些类型的值。

The first value we need is the selected (or max date in cases of no selection). 我们需要的第一个值是选定的(如果没有选择,则为最大日期)。

VAR SelectedDate = MAX(Data[Date])

With that date, we can calculate the TargetYear and TargetMonth. 在该日期之后,我们可以计算TargetYear和TargetMonth。 Both of them are simple IF statements to catch the January/December crossover. 它们都是简单的IF语句,可以捕获一月/十二月的交叉。

VAR TargetYear = IF(MONTH(SelectedDate) = 1, YEAR(SelectedDate) - 1, YEAR(SelectedDate))
VAR TargetMonth = IF(MONTH(SelectedDate) = 1, 12, MONTH(SelectedDate) - 1)

Having all of the values we need, we write a CALCULATE statement that filters the data to the TargetYear and month is less than or equal to TargetMonth. 有了我们需要的所有值之后,我们编写了CALCULATE语句,该语句将数据过滤到TargetYear,并且month小于或等于TargetMonth。

CALCULATE([Total Value], FILTER(ALL(Data), YEAR([Date]) = TargetYear && MONTH([Date]) <= TargetMonth))

Putting it all together looks like this. 将它们放在一起看起来像这样。

Prior Month YTD = 
VAR SelectedDate = MAX(Data[Date])
VAR TargetYear = IF(MONTH(SelectedDate) = 1, YEAR(SelectedDate) - 1, YEAR(SelectedDate))
VAR TargetMonth = IF(MONTH(SelectedDate) = 1, 12, MONTH(SelectedDate) - 1)

RETURN
    CALCULATE([Total Value], FILTER(ALL(Data), YEAR([Date]) = TargetYear && MONTH([Date]) <= TargetMonth))

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM