如何创建执行条件的度量以及结果的分组依据和该分组的计数(DAX / PowerBI)

时间:2019-01-15 18:53:31

标签: powerbi dax

我最近开始使用PowerBI并学习DAX。我想将条件伪代码转换为DAX,这将导致名为“ Percentage”的列。因此,我的表由公司列(Dole,Dyson,孟山都等),水果名称(苹果,香蕉,梨,西红柿,黄瓜等)和食品组(蔬菜,肉等)组成。 )

Company | Fruit Name | Food Group    
----------------------------------
Dole    | Apple      |  Fruit 
Dyson   | Chicken    |  Meat
Dole    | Pear       | Fruit
Dole    | Tomato     | Vegetable

这是条件代码的伪代码:

if(FruitName = 'Apple')   //for the rows that have 'Apples'
     then( 1/count( Food Group for Company) )   //percentage of apples within 
                             //Fruits distributed by Company: probably a groupby

else (1/count(Food Group for Company) )           //percentage of value within 'Food Group' by Company

结果将在度量内输出,是百分比列。

Company      | Fruit Name | Food Group | Percentage
-----------------------------------------------------
Dole         | Apple      | Fruit      |  33.33%
Dyson        | Chicken    | Meat       |  100%
Dole         | Pear       | Fruit      |  33.33%
Dole         | Banana     | Fruit      |  33.33%
Dole         | Tomato     | Vegetable  |  100%

您能告诉我一种用DAX编写此条件的方法吗?

谢谢

1 个答案:

答案 0 :(得分:0)

由于您的thenelse相同,因此我不确定您为什么要有条件。

无论如何,我认为您正在寻找这样的东西:

Percentage =
DIVIDE (
    COUNT ( Foods[Fruit] ),
    CALCULATE ( COUNT ( Foods[Fruit] ), ALLEXCEPT ( Foods, Foods[Group] ) )
)

ALLEXCEPT函数会删除除Group列之外的所有过滤器上下文,以便在该Fruit中获得所有Group

Result