[英]Sum amount base from previous value on slicer power bi
I have a table like this: 我有一张这样的桌子:
Year Month Code Amount
---------------------------------------
2017 11 a 7368
2017 11 b 3542
2017 12 a 4552
2017 12 b 7541
2018 1 a 6352
2018 1 b 8376
2018 2 a 1287
2018 2 b 3625
I make slicer base on Year and Month (ignore the Code), and I want to show SUM of Amount like this : 我基于Year和Month制作切片器(忽略代码),我想显示如下的SUM of Amount:
Year 2017 and Month 12
, the value to be shown is SUM Amount base on 2017-11, and select on slicer Year 2018 and Month 1
should be SUM Amount base on 2017-12 Year 2017 and Month 12
,则显示的值是基于2017-11的SUM Amount,而在Slicer Year 2018 and Month 1
应该是基于2017-12的SUM Amount I have tried this one for testing with, but this not allowed: 我已经尝试过使用此工具进行测试,但这是不允许的:
Last Month = CALCULATE(SUM(Table[Amount]); Table[Month] = SELECTEDVALUE(Table[Month]) - 1)
How to do it right? 怎么做对?
I want something like this 我想要这样的东西
NB: I use direct query to SQL Server 注意:我使用直接查询SQL Server
Update: At this far, I added Last_Amount
column in SQL Server Table by sub-query, maybe you guys have a better way for my issue 更新:到目前为止,我通过子查询在SQL Server表中添加了
Last_Amount
列,也许你们有解决我问题的更好方法
The filters in a CALCULATE
statement are only designed to take simple statements that don't have further calculations involved. CALCULATE
语句中的过滤器仅设计为采用不涉及进一步计算的简单语句。 There are a couple of possible remedies. 有两种可能的补救措施。
1. Use a variable to compute the previous month number before you use it in the CALCULATE
function. 1.在
CALCULATE
函数中使用变量之前,请先使用变量计算前一个月份的数字。
Last Month =
VAR PrevMonth = SELECTEDVALUE(Table[Month]) - 1
RETURN CALCULATE(SUM(Table[Amount]), Table[Month] = PrevMonth)
2. Use a FILTER()
function. 2.使用
FILTER()
函数。 This is an iterator that allows more complex filtering. 这是一个迭代器,可以进行更复杂的过滤。
Last Month = CALCULATE(SUM(Table[Amount]),
FILTER(ALL(Table),
Table[Month] = SELECTEDVALUE(Table[Month]) - 1))
Edit: Since you are using year and month, you need to have a special case for January. 编辑:由于您使用的是年份和月份,因此您需要为一月份设置特殊情况。
Last Month =
VAR MonthFilter = MOD(SELECTEDVALUE(Table[Month]) - 2, 12) + 1
VAR YearFilter = IF(PrevMonth = 12,
SELECTEDVALUE(Table[Year]) - 1,
SELECTEDVALUE(Table[Year]))
RETURN CALCULATE(SUM(Table[Amount]),
Table[Month] = MonthFilter,
Table[Year] = YearFilter)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.