首页 > 解决方案 > 在 Power BI 中使用多个条件和表创建列

问题描述

我想根据 Power BI 中的其他一些表为产品列表创建一个列。table1是我要调查的产品的完整列表,

table1

id  product_name 
1   xl1
2   xl2
3   s11
4   m11
...

我想city基于另一个表 table2 创建一个列:

id  product_name   city
1   xl1            D
2   xl1            A 
3   xl1            A 
4   xl1            A 
5   xl1            A 
6   s11            B
7   s11            B
8   s11            B
9   m11            C

如果product_nameintable1也找到 in table2,则查找city,如果只有一个值,则用该值填充新列,如果有两个值,则写 'both',如果product_name找不到 in table2,则写 'no value'

预期输出:

id  product_name  new_col
1   xl1           both   (because city =D,A in table2)
2   xl2           no value
3   s11           B
4   m11           C
...

我应该如何在 Power BI 中解决此类问题?任何建议表示赞赏。

标签: powerbi

解决方案


您可以使用以下dax措施来实现您的结果,因此当您知道product出现在 1 个以上独特的城市时,它会返回两者并在有帮助的情况下接受解决方案:)

Column = 
var result1 = CALCULATE(FIRSTNONBLANK(Table2[city],1),
                FILTER(ALL(Table2),Table2[product_name] = EARLIER(Table1[product_name])))
var distint = CALCULATE(DISTINCTCOUNT(Table2[city]),
                FILTER(ALL(Table2),Table2[product_name]=EARLIER(Table1[product_name])))
return
IF(distint>1,"both",IF(ISBLANK(result1),"no value",result1))

在此处输入图像描述


推荐阅读