简体   繁体   English

使用DAX对数组求和的Powerpivot 2016度量

[英]Powerpivot 2016 measure using DAX to sum an array

I want to sum the 7 preceding values of a row as a measure like so: 我想将某行的7个先前值相加,作为一种度量,如下所示:

| Wk_number | Value A | Measure |   Array |
-------------------------------------------
|        01 |       1 |   N/A#  |   N/A#  |
|        02 |       1 |       1 |    {01} |
|        03 |      10 |       2 | {01-02} |  
|        04 |       3 |      12 | {01-03} |
|        05 |       5 |      15 | {01-04} |
|        06 |      10 |      20 | {01-05} |
|        07 |       1 |      30 | {01-06} |
|        08 |       4 |      31 | {01-07} |
|        09 |     -10 |      34 | {02-08} |
|        10 |       3 |      26 | {03-09} |
|        11 |       2 |      18 | {04-10} |
etc...

I added the array column just to clarify the example how of the sum is comprised, notice that from wk09 it's not simply a running total. 我添加了数组列只是为了阐明示例如何构成总和,请注意,从wk09开始,这不仅仅是简单的总计。

How to do this using DAX statements? 如何使用DAX语句执行此操作?

Two ways to do this: you can either create a calculated column, or a measure. 有两种方法:创建计算列或度量。

For the column: 对于列:

=CALCULATE(SUM([Value A]),FILTER(Table,Table[Wk_number]<EARLIER(Table[Wk_number]) && Table[Wk_number] >= (EARLIER(Table[Wk_number])-7)))

For the measure, it's a very similar formula but instead of using EARLIER(), we use MAX(): 对于此度量,这是一个非常相似的公式,但是我们使用MAX()代替了EARLIER():

=CALCULATE(SUM([Value A]),FILTER(ALL(Table3),Table3[Wk_number]<MAX(Table3[Wk_number]) && Table3[Wk_number] >= (MAX(Table3[Wk_number])-7)))

Below is a screenshot of the results. 以下是结果的屏幕截图。 A few of the numbers in your example table seem to be off based on the math: 根据数学公式,示例表中的一些数字似乎不正确:

在此处输入图片说明

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

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