HASONEVALUE 和 HASONEFILTER 都是用来检查筛选状态的函数。
- HASONEVALUE 在参数列在当前筛选上下文中只有一个可见值时, 返回TRUE, 否则FALSE。
- HASONEFILTER 在参数列被直接筛选(direct-filter)并且只筛选一个值时,返回TRUE, 否则FALSE
它们有相似的语法,都只接受基列(存在于模型中的列)作为参数。
然而, 它们有一个显著的区别:
- HASONEVALUE 基于交叉筛选(cross-filter)工作。
- HASONEFILTER 基于直接筛选(direct-filter)工作。
我们通过2个示例,演示这一区别。
- 在参数列被直接筛选一个值时, 此时,HASONEFILTER 返回TRUE, 然而可能因为筛选的值在筛选上下文中不可见,HASONEVALUE 返回FALSE。
-- 一个列可能被直接筛选了一个值, 但是因为交叉筛选没有可见值。
DEFINE
MEASURE Sales[Has One Filter] =
CALCULATE (
HASONEFILTER ( 'Product'[Category] ),
'Product'[Category] = "Home Appliances",
'Product'[Product Name] = "Contoso 512MB MP3 Player E51 Silver"
)
MEASURE Sales[Has One Value] =
CALCULATE (
HASONEVALUE ( 'Product'[Category] ),
'Product'[Category] = "Home Appliances",
'Product'[Product Name] = "Contoso 512MB MP3 Player E51 Silver"
)
MEASURE Sales[Are there any products?] =
CALCULATE (
COUNTROWS ( 'Product' ) > 0,
'Product'[Category] = "Home Appliances",
'Product'[Product Name] = "Contoso 512MB MP3 Player E51 Silver"
)
EVALUATE
{
( "Has One Filter", [Has One Filter] ),
( "Has One Value", [Has One Value] ),
( "Are there any products?", [Are there any products?] )
}
- 一个列可能由于交叉筛选只有一个可见值,此时,HASONEVALUE 返回TRUE, 然而由于列没有被直接筛选, HASONEFILTER 返回FALSE。
-- 一个列可能由于交叉筛选只有一个可见值, 但并没被直接筛选。
DEFINE
MEASURE Sales[Has One Filter] =
CALCULATE (
HASONEFILTER ( 'Product'[Category] ),
'Product'[Product Name] = "Contoso 512MB MP3 Player E51 Silver"
)
MEASURE Sales[Has One Value] =
CALCULATE (
HASONEVALUE ( 'Product'[Category] ),
'Product'[Product Name] = "Contoso 512MB MP3 Player E51 Silver"
)
EVALUATE
{
( "Has One Filter", [Has One Filter] ),
( "Has One Value", [Has One Value] )
}
image.png