简体   繁体   English

Power BI(DAX)中具有当前行值和先前日期的计算列

[英]Calculated Column with Current Row Values and Previous Dates in Power BI (DAX)

I'm trying to get a calculated column (not a measure) that gets the sum of a column based on the values in the current row of that table for dates that are 1 month lagged to the date on the current row. 我正在尝试获取一个计算列(而非度量值),该列基于该表当前行中的值(相对于当前行中的日期滞后1个月)获得该列的总和。 My table has dates that are the 1st day of every month only .. no other days in the month. 我的表中的日期仅是每个月的第一天。 I'm asking the question about DAX; 我在问有关DAX的问题; however, I have no problem implementing in M Language in Power Query (actually would probably prefer) if there is a solution that way as well. 但是,如果有这样的解决方案,我也可以在Power Query中的M语言中实现(实际上可能会更喜欢)。

I have been able to get a measure to work using something like this.. 我已经能够使用这种方法来采取措施。

CALCULATE(SUM(AMT), DATEADD(DATECOLUMN, -1, MONTH))

But I'd like to be a new column instead. 但是我想成为一个新专栏。

Assuming the table looks something like this.. 假设表看起来像这样。

A    B        C          D     AMT
6    BAC456   5/1/2019   TEST  25
2    EPS123   4/1/2019   TEST  45
2    EPS123   3/1/2019   TEST  65
6    BAC456   4/1/2019   TEST  43
6    BAC456   4/1/2019   TEST  88
7    GRE123   4/1/2019   TEST  90
9    BAC456   4/1/2019   TEST  43

I'd like to have another column in this table where the first row would be: 我想在此表中还有另一列,其中第一行是:

A    B         C          D      AMT   NEWCOL
6    BAC456    5/1/2019   TEST   25    131

Second row would be: 第二行是:

A    B         C          D      AMT   NEWCOL
2    EPS123    4/1/2019   TEST   45    65

etc.. 等等..

In cases where the month column is the first month in the entire table NEWCOL would be 0 如果月份列是整个表中的第一个月,则NEWCOL为0

To achieve this easily in a calculated column, you need something like this before writing the final DAX. 为了在计算列中轻松实现这一点,在编写最终DAX之前,您需要类似以下内容。

Month Num = MONTH(MyTable[C])
Month Diff = DATEDIFF(MyTable[C],MAX(MyTable[C]),MONTH)

And now you have these Month differences and Month numbers defined, you can write a DAX like this - 现在,您已经定义了这些月份差异和月份编号,您可以编写这样的DAX-

Amount - New Column = 
Var selectedValue_B = MyTable[B]
Var SelectedValue_MonthDiff = MyTable[Month Diff]
Var out1 = CALCULATE(SUM(MyTable[AMT]), FILTER(ALL(MyTable), MyTable[Month Diff] = SelectedValue_MonthDiff+1 && MyTable[B] = selectedValue_B)) + 0
return out1

This makes my table to look something like, 这使我的桌子看起来像

在此处输入图片说明

I have used Var(Variables) in my formula to help you understand what is happening inside the formula. 我在公式中使用了Var(变量),以帮助您了解公式中正在发生的事情。

Kindly accept the answer if it solves your problem. 如果可以解决您的问题,请接受答案。

To get 131 , I'm assuming that you are requiring a match on both columns A and B . 要获得131 ,我假设您在AB列上都需要匹配。

NewCol =
CALCULATE (
    SUM ( Table1[AMT] ),
    ALLEXCEPT ( Table1, Table1[A], Table1[B] ),
    PREVIOUSMONTH ( Table1[C] )
)

This sums the column AMT keeping the row context of columns A and B and specifies the previous month as a filter on C . 这将对AMT列求和,并保持AB列的行上下文,并指定上个月作为C的过滤器。 Note that this returns a blank for rows that don't have a previous month. 请注意,对于没有上个月的行,这将返回空白。 If you'd prefer 0 then add + 0 after the last closing parenthesis. 如果您希望使用0 ,请在最后一个右括号后面加上+ 0


If PREVIOUSMONTH doesn't work, then try this: 如果PREVIOUSMONTH不起作用,请尝试以下操作:

NewCol =
CALCULATE (
    SUM ( Table1[AMT] ),
    ALLEXCEPT ( Table1, Table1[A], Table1[B] ),
    Table1[C] = EOMONTH ( EARLIER( Table1[C] ), -2 ) + 1
)

For date = 5/1/2019 , EOMONTH ( date, -2 ) returns 3/31/2019 . 对于date = 5/1/2019EOMONTH ( date, -2 )返回3/31/2019 Add one day to get 4/1/2019 . 加一天即可获得4/1/2019

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

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