PowerBI Count all Rows With Value In Group

时间:2019-01-09 22:33:13

标签: powerbi dax

This seems like one of the use cases for groups, but maybe I'm understanding them wrong.

I have a table that shows a count of all rows like this:

User | Completed Tasks
Bob  | 2
Jim  | 1
Pete | 1

The table it comes from looks like this:

User | Type
Bob  | A
Bob  | B
Jim  | A
Pete | C

This is very simplified - in reality there are about 80 different types - I'm hoping to get 5 of them in a group called Secondary and the rest in a group called Primary

For the example, say I want A and B to be considered "primary" and C to be secondary.

The new table would look like this:

User | Completed Tasks | Primary | Secondary
Bob  |        2        |    2    |     0
Jim  |        1        |    1    |     0   
Pete |        1        |    0    |     1

I tried creating a group of Type with 5 called Secondary and the rest called Primary, but I was having trouble figuring it out.

I just want a Count of types for that particular group based on the filtered values and everything.

Is there an easy way to do this or do I need to create a measure/calculated column?

2 个答案:

答案 0 :(得分:1)

另一种解决方法是为Group创建一个计算列

Group = IF(table[Type] IN {"A","B"}, "Primary", "Secondary")

然后您可以将Group用作矩阵上的列并计算Type列。

Matrix Visual

请注意,如果您想分成更多的组,则此方法的伸缩性更好。在这种情况下,您可能想使用SWITCH

Group = 
    SWITCH(TRUE(),
        Table1[Type] IN {"A","B"}, "Primary",
        Table1[Type] IN {"C"}, "Secondary",
        Table1[Type] IN {"D", "E", "F"}, "Tertiary",
        "Other"
    )

答案 1 :(得分:0)

我最终通过创建两个计算列来解决了这个问题。

对于不在“辅助”列表中的每一行,主要的Dax将为1

PrimaryCount = IF(table[Type] in {"C","D","E","F","G"},0,1)

辅助列表的Dax将是辅助列表中每一行的1

SecondaryCount = IF(table[Type] in {"C","D","E","F","G"},1,0)

然后,只需将它们添加到表值中,并确保选中Sum(默认值)。

我认为使用组会更容易,但是我想这很简单并且似乎可行。