Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
bullius
Helper V
Helper V

Count duplicate values using a DAX measure

Hi

 

I have data that looks like this:

 

EmploymentIDPersonID
1A
2A
3B
4C
5D
6E
7F
8G
9G
10G

 

I want to count how many employments each person has, which would result in something like the following:

 

EmploymentIDPersonIDNo. Of Employments
1A2
2A2
3B1
4C1
5D1
6E1
7F1
8G3
9G3
10G3

 

The challenge is, I want the No. Of Employments value to be effected by filters, so I need it to be a measure, rather than a calculated column.

 

This is the formula I used to create a calculated column:

 

No. Of Employments = 
CALCULATE (
	COUNT (
		[PersonID]
	),
	FILTER (
		Dim_Employments,
		[PersonID] = EARLIER (
			[PersonID]
		)
	)
)

 

However, this does not work as a measure; it returns the following error message:
"EARLIER/EARLIEST refers to an earlier row context which doesn't exist."

 

Any thoughts?

 

Thanks!

 

1 ACCEPTED SOLUTION

Hi @bullius,

 

Add you columns to the all except.

 

The new measure would be something like this:

 

Measure = CALCULATE( COUNT(Table1[PersonID]), ALLEXCEPT(Table1,Table1[PersonID],Table1[status]))

as you can see below the top half is raw data and measure without interacting with slicer, bottom part table is filter by status B

 

allexcept.png

Regards,

MFelix


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

10 REPLIES 10
MFelix
Super User
Super User

Hi @bullius,

 

Use the following measure:

 

N.º Employments  =
CALCULATE ( COUNT ( Table1[PersonID] ), ALLEXCEPT ( Table1, Table1[PersonID] ) )

Regards,

MFelix


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Anonymous
Not applicable

How All Except Function is working in that function

Hi @Anonymous ,

 

The ALLEXCEPT function removes all context filters in the table except filters that have been applied to the specified columns. In this case when making the count it will only keep the ID and Status filter on the table.

 

In this case the calculation is picking up all users with the selected status B.

 

Regards,

MFelix 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Thanks @MFelix

 

I created a matrix with the measure in the values bucket, but it does not seem to be affected by visual level filters...

What do  you mean to be affected by visual level filters?

 

What type of iteractions you want to have and expected result.

 

Regards

Mfelix


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



My data also includes other fields like Status (Active/Inactive).

 

An example of the functionality I want would be:

 

I want to be able to filter the matrix to only include active emplyments and the No. of Employments per PersonID to change.

 

Make sense?

Hi @bullius,

 

Add you columns to the all except.

 

The new measure would be something like this:

 

Measure = CALCULATE( COUNT(Table1[PersonID]), ALLEXCEPT(Table1,Table1[PersonID],Table1[status]))

as you can see below the top half is raw data and measure without interacting with slicer, bottom part table is filter by status B

 

allexcept.png

Regards,

MFelix


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Hi @MFelix 

 

i'm trying to do something similar to this. Would you know how to create the same output but without having the Person ID displayed on the table? i would like a dax expression that automatically looksup the corresponding personID related to the ID and returns a count. so final table would look like

 

ID     Count

1       2

2       2

3       1

4       1

5       1

6       1 

7       1

8       3

9       3

10     3

with the above DAX expression, i am getting a 7 in every row. any help appreciated, thanks

Hi @gsingh67 ,

 

What do you mean by not having the person ID displayed in the table?

 

The calculation of measures is based on context so if you don't have in this case the person ID the result will be different.

You also mentioned that you have 7 in all rows, what is the value for each row? is it another column on your data?

 

Dou you want to have a table only with the values  on the count measure?


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Thanks again!

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.