Forum Discussion
Distinctcount GroupBy?
Hi,
I have a table lets call it Facts it has the following coulmns:
ProjectName Dep WorkerCode
X1 Sales 89
X1 Sales 63
X1 Admin 77
X1 Admin 99
X2 Purshase 01
I want a measure, a table, or a column where i can count total numer of workers groubed by Department and Project name, so the result is something like
ProjectName Dep Tot. NrOfWorkers
X1 Sales 2
X1 Admin 2
X2 purshase 1
I have tried the following but they are not working as I wished them to:
Column = CALCULATE(DISTINCTCOUNT(Facts[WorkerCode]),ALLEXCEPT(Facts,Facts[ProjectName],Facts[Dep]))
But its giving the right result
I have also tried to create a table from modeling:
Column = SUMMARIZE(Facts,Facts[ProjectName],Facts[Dep],"distinc",DISTINCTCOUNT(Facts[WorkerCode]))
But am getting an error saying the the expression refers to multiple columns. Multiple columns cannot be converted to a scalar value.
Your help is highly apprecitaed :)
- Anonymous7 years ago
or if you want the DAX version:
Table = ADDCOLUMNS( SUMMARIZECOLUMNS( 'Fact'[ProjectName ], 'Fact'[Dep ] ), "Distinct Count", CALCULATE( DISTINCTCOUNT('Fact'[WorkerCode]) ) ) I'll quickly note that if you're goal is to simply have a visual that shows this info in a report/dashboard, then you don't need to write any DAX.
Simply create a table visual, add in ProjectName, Dep, and WorkerCode, then hit dropdown on WorkerCode value and change aggregation setting to "Count (Distinct)" – you can change display name of the columns in the same dropdown menu, too (if for example you don't like "Count of WorkerCode").
7 Replies
- AnonymousNot applicable
if you are not oppose to using Power Query, this can be done with just a few clicks:
- Have your table loaded
- Reference that query (this will create a new query)
- Go to Transform--> Table--> GroupBy
- Then use these inputs:
- And your final table:
- AnonymousNot applicable
or if you want the DAX version:
Table = ADDCOLUMNS( SUMMARIZECOLUMNS( 'Fact'[ProjectName ], 'Fact'[Dep ] ), "Distinct Count", CALCULATE( DISTINCTCOUNT('Fact'[WorkerCode]) ) )- AnonymousNot applicable
Many thanks for your answer. However, Iam still getting the following when using DAX: "The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value"
Any suggestions?
- MarkLafSuper User
I'll quickly note that if you're goal is to simply have a visual that shows this info in a report/dashboard, then you don't need to write any DAX.
Simply create a table visual, add in ProjectName, Dep, and WorkerCode, then hit dropdown on WorkerCode value and change aggregation setting to "Count (Distinct)" – you can change display name of the columns in the same dropdown menu, too (if for example you don't like "Count of WorkerCode").
- v-jiascu-msftMicrosoft Employee
Hi Anonymous ,
Could you please mark the proper answers as solutions?
Best Regards,