[英]Rolling Sum using two columns in Power BI
I've two columns containing sales from last year and this year and a data column with the number of weeks we are in. I want to calculate a yearly rolling sum in a new column from the week we are in back in the past till that same week.我有两列包含去年和今年的销售额以及一个包含我们所处周数的数据列。我想计算从过去一周到那一周的新列中的年度滚动总和同一周。
In my example if i'm in week 2 this year my rolling sum will be sum all the values of sales last year from week 2 til week 52 plus sales from this year until week 2 including!!在我的例子中,如果我今年在第 2 周,我的滚动总和将是去年第 2 周到第 52 周的所有销售额加上今年到第 2 周的销售额,包括!!
Here's an example in excel of what my table and results would look like:这是我的表格和结果的 Excel 示例:
Assuming your data look like this假设您的数据如下所示
Table
+------+----------+-------+
| week | sales_ly | sales |
+------+----------+-------+
| 1 | 65 | 100 |
+------+----------+-------+
| 2 | 93 | 130 |
+------+----------+-------+
| 3 | 83 | 134 |
+------+----------+-------+
| 4 | 3083 | 59 |
+------+----------+-------+
| 5 | 30984 | 39 |
+------+----------+-------+
| 6 | 38 | 580 |
+------+----------+-------+
| 7 | 28 | 94 |
+------+----------+-------+
| 8 | 48 | 93 |
+------+----------+-------+
| 9 | 24 | 984 |
+------+----------+-------+
| 10 | 49 | 95 |
+------+----------+-------+
You need to create two cumulatives and sum them in the same measure.您需要创建两个累积并在同一度量中对它们求和。
Rolling Sum =
VAR CurrentYearCumulative =
CALCULATE(
SUM('Table'[sales]),
FILTER(ALLSELECTED('Table'),'Table'[week] <= MAX('Table'[week] ) )
)
VAR LastYearCumulative =
CALCULATE(
SUM('Table'[sales_ly]),
FILTER(ALLSELECTED('Table'),'Table'[week] >= MAX('Table'[week]) )
)
RETURN
CurrentYearCumulative + LastYearCumulative
The output输出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.