给定某些条件从现有行计算的行
Row calculated from existing rows given some condition
我是 Power BI 的新手,我是一名数据分析师,通常使用代码,所以我不太熟悉 Excel 等公式
我有一个大约有 20 列的 table,我只需要 2 列来进行此计算,所以我将放大我需要的列:
我想添加属性为 Attribute1 - Attribute2 - Attribute3 的行。
结果应如下所示:
从昨天早上开始我就一直坚持这个问题
我通过以下措施实现了它:
- 正在创建包含不同属性的 table :
TableXX = UNION(DISTINCT('Table'[Attributes]),{{"Attribute5"}})
- 用一个度量做计算:
Measure =
SUMX (
DISTINCT ( 'Table 3'[Attributes] ),
SWITCH (
'Table 3'[Attributes],
"Attribute5", CALCULATE ( SUM ( 'Table'[Value] ), 'Table'[Attributes] = "Attribute1" )
- CALCULATE ( SUM ( 'Table'[Value] ), 'Table'[Attributes] = "Attribute2" )
- CALCULATE ( SUM ( 'Table'[Value] ), 'Table'[Attributes] = "Attribute3" ),
var a = 'Table 3'[Attributes] return
CALCULATE ( SUM ( 'Table'[Value] ),'Table'[Attributes]=a)
)
)
但我并不是真的想要一个度量,我想用这个度量替换值列,我该如何实现?
我想说最简单的方法是旋转 Values
列,这样您将得到一个包含 Date
、Attribute1
、[=13= 列的 table ], Attribute3
和 Attribute4
:
然后添加自定义列Attribute5
:
然后逆透视属性列以获得 table 返回:
您可以将新的 table 定义为现有 table 和具有所需新行的计算 table 的并集:
NewTable =
VAR Table5 =
SUMMARIZE (
FILTER (
'Table 3',
'Table 3'[Attributes] IN { "Attribute1", "Attribute2", "Attribute3" }
),
'Table 3'[Date],
"Attributes", "Attribute5",
"Value", SUMX (
'Table 3',
IF (
'Table 3'[Attributes] = "Attribute1",
'Table 3'[Value],
- 'Table 3'[Value]
)
)
)
RETURN
UNION ( 'Table 3', Table5 )
新计算的 table 存储在变量 Table5
中,该变量仅采用包含属性 1、2 或 3 的行,并且对于每个日期,采用除属性以外的值的总和1 得到一个负号。
我是 Power BI 的新手,我是一名数据分析师,通常使用代码,所以我不太熟悉 Excel 等公式
我有一个大约有 20 列的 table,我只需要 2 列来进行此计算,所以我将放大我需要的列:
我想添加属性为 Attribute1 - Attribute2 - Attribute3 的行。
结果应如下所示:
从昨天早上开始我就一直坚持这个问题
我通过以下措施实现了它:
- 正在创建包含不同属性的 table :
TableXX = UNION(DISTINCT('Table'[Attributes]),{{"Attribute5"}})
- 用一个度量做计算:
Measure =
SUMX (
DISTINCT ( 'Table 3'[Attributes] ),
SWITCH (
'Table 3'[Attributes],
"Attribute5", CALCULATE ( SUM ( 'Table'[Value] ), 'Table'[Attributes] = "Attribute1" )
- CALCULATE ( SUM ( 'Table'[Value] ), 'Table'[Attributes] = "Attribute2" )
- CALCULATE ( SUM ( 'Table'[Value] ), 'Table'[Attributes] = "Attribute3" ),
var a = 'Table 3'[Attributes] return
CALCULATE ( SUM ( 'Table'[Value] ),'Table'[Attributes]=a)
)
)
但我并不是真的想要一个度量,我想用这个度量替换值列,我该如何实现?
我想说最简单的方法是旋转 Values
列,这样您将得到一个包含 Date
、Attribute1
、[=13= 列的 table ], Attribute3
和 Attribute4
:
然后添加自定义列Attribute5
:
然后逆透视属性列以获得 table 返回:
您可以将新的 table 定义为现有 table 和具有所需新行的计算 table 的并集:
NewTable =
VAR Table5 =
SUMMARIZE (
FILTER (
'Table 3',
'Table 3'[Attributes] IN { "Attribute1", "Attribute2", "Attribute3" }
),
'Table 3'[Date],
"Attributes", "Attribute5",
"Value", SUMX (
'Table 3',
IF (
'Table 3'[Attributes] = "Attribute1",
'Table 3'[Value],
- 'Table 3'[Value]
)
)
)
RETURN
UNION ( 'Table 3', Table5 )
新计算的 table 存储在变量 Table5
中,该变量仅采用包含属性 1、2 或 3 的行,并且对于每个日期,采用除属性以外的值的总和1 得到一个负号。