【发布时间】:2023-02-10 07:07:08
【问题描述】:
如何根据条件返回 DAX 表? IF 函数无法返回 DAX 中的表。
IF( 1=1, table_1, table_2 )
它引发了一个错误:表达式引用多个列。不能将多个列转换为标量值。
我想使用切片器在多个表之间进行选择,稍后可以将哪个表用于 CALCULATE 中的交替动态过滤器上下文。
CALCULATE( [Measure],
IF( Condition,
table_1,
table_2
)
)
为了使问题更具挑战性,我希望 table_1 和 table_2 具有不同的列集。所以结合使用 UNION 和 FILTER 函数是行不通的。
UNION(
FILTER( table_1, condition ),
FILTER( table_2, NOT condition)
)
作为一种可能的方法,我们可能会根据切片器的选择选择一个度量:
IF( Slicer_Selection_Condition,
[M1], // Measure with filter set 1
[M2] // Measure with filter set 2
)
但我不想这样做,因为它会增加每个切片器组合所需措施的数量。
如果我们能够超越 IF 限制,我们就可以非常有用地应用它。想象一下,我们有一个切片器可以在 [Quantity]、[Value]、[Cost] 中选择一个度量。我们还有另一个切片器来选择过滤器集。我们可以用一个措施来处理它:
CALCULATE(
SWITCH( Measure_Slicer, 1, [Quantity], 2, [Value], [Cost] ), // measure choice
SWITCH( Filter_Slicer, 1, table_1, 2, table_2, table_3 ) // filter choice
)
这是重现问题的表格:
Table =
DATATABLE (
"Color", STRING,
"Shape", STRING,
"Quantity", INTEGER,
"Value", INTEGER,
{
{ "Red" , "Circle" , 1, 10 },
{ "Red" , "Triangle", 1, 10 },
{ "Blue" , "Circle" , 1, 10 },
{ "Blue" , "Triangle", 1, 10 },
{ "Yellow", "Square" , 1, 10 }
}
)
及措施:
M_Quantity = SUM( 'Table'[Quantity] )
M_Value = SUM( 'Table'[Value] )
Desired Measure =
VAR Measure_Slicer = 1 // Either 1 OR 2
VAR Filter_Slicer = 1 // Either 1 OR 2
VAR table_1 = SUMMARIZE( 'Table', 'Table'[Color] )
VAR table_2 = SUMMARIZE( 'Table', 'Table'[Color], 'Table'[Shape] )
RETURN
CALCULATE(
SWITCH( Measure_Slicer, 1, [M_Quantity], [M_Value]), // Measure choice
SWITCH( Filter_Slicer, 1, table_1 , table_2 ) // Filter choice
)
【问题讨论】: