首页 > 解决方案 > PowerBI:当满足任一 VAR 时返回计算

问题描述

我在 PowerBI 中有这样的表:

在此处输入图像描述

我想实现:2018年,19岁以上的人数总和;对于 2018 年之后的每一年,将正好 19 岁的人的总人数相加。

我下面的代码返回错误(“表达式引用多列。多列不能转换为标量值”):

nineteen_years =
VAR age_test_2018 =
    FILTER ( 'table', 'table'[age] >= 19 && 'table'[year] = 2018 )
VAR age_test_later =
    FILTER ( 'table', 'table'[age] = 19 && 'table'[year] > 2018 )
RETURN
    CALCULATE ( SUM ( 'Union'[head count] ), age_test_2018 || age_test_later )

任何人都可以帮我解决这个问题吗?感谢你的帮助!

标签: powerbidaxvar

解决方案


这里的问题是 age_test_2018 和 age_test_later 是表。您不能对表使用 OR 运算符 (||),它需要标量值(因此出现错误)。

要修复它,您可以将表合并为一个,例如:

nineteen_years =
VAR age_test_2018 =
    FILTER ( 'table', 'table'[age] >= 19 && 'table'[year] = 2018 )
VAR age_test_later =
    FILTER ( 'table', 'table'[age] = 19 && 'table'[year] > 2018 )
VAR all_age_tests =
    UNION ( age_test_2018, age_test_later )
RETURN
    CALCULATE ( SUM ( 'Union'[head count] ), all_age_tests )

推荐阅读