Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago

New Column = COUNTDISTINCT categories per user transform

I am attempting to create a graph which shows the number of users which contain x number of different categories. By definition the data contains sessions where users have many sessions and sessions can have different labels. 

 

This is effectively the same as:

 

python:

df['label_count'] = df.groupby('userId')['category'].transform('nunique') 

df.groupby('userId')['label_count'].first().reset_index().groupby('label_count')['userId'].count()

 

sql:

with 

init as (

select userid, 

          count(distinct(category)) as label_count

as from table

group by userid)

 

select label_count,

          count(userId) as total_users

from init

group by label_count

 

Naturally the power bi graph will take care of the count and distinct users at the end, I just need to get the distinct count of categories per user id as a new column. 

2 Replies

  • Anonymous , Not clear. But seems like, you need

    sumx(Values(Table[User]),distinctCOUNT(Table[category]))

  • Anonymous's avatar
    Anonymous
    Not applicable

    Anonymous 

    Not clear with your expected result, maybe provide us a sample data and your expected output visually.


    Paul Zheng _ Community Support Team
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.