Forum Discussion

bullius's avatar
bullius
Helper V
8 years ago
Solved

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!

 

  • 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

     

    Regards,

    MFelix

10 Replies

  • Hi bullius,

     

    Use the following measure:

     

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

    Regards,

    MFelix

    • bullius's avatar
      bullius
      Helper V

      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...

      • MFelix's avatar
        MFelix
        Super User

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

         

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

         

        Regards

        Mfelix

    • Anonymous's avatar
      Anonymous
      Not applicable

      How All Except Function is working in that function

      • MFelix's avatar
        MFelix
        Super User

        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