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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
req77
Helper I
Helper I

Count the number of occurrences in another table

Hi,

I have two tables that follow this structure:

Table: Employee

ID  Name
1  John
2  Jack
3  Bob

 

Table: Feedback

ID Name
1 John
1 John
2 Jack
2 Jack
2 Jack
3 Bob


I want to know the number of feedbacks (number of rows in the feedback table) for each person (ID), but I want 4 different measures:

1st measure: count of occurrences of IDs in table Feedback that appear at least 1 time.

2nd measure: count of occurrences of IDs in table Feedback that appear at least 1 or 2 times.

3rd measure: count of occurrences of IDs in the Feedback table that appear at least 1, 2 or 3 times.

4th measure: count of occurrences of IDs in the Feedback table that appear at least 1, 2, 3 or 4 times.


So based on the example tables above:

1st measure = 3  (Jhon, Jack and Bob)

2nd measure = 2 (Jhon and Jack)

3nd measure = 1 (Jack)

4nd measure = 0

Can someone help me?

2 ACCEPTED SOLUTIONS
littlemojopuppy
Community Champion
Community Champion

Hi @req77 

Try this...

FeedbackCount = COUNT(Feedback[Name])

Measure XX =
VAR	EmployeeSummary =
	ADDCOLUMNS(
		Employee,
		"FeedbackCount",
		FeedbackCount
	)
RETURN

COUNTROWS(
	FILTER(
		EmployeeSummary,
		FeedbackCount > XX
	)
)

In the second measure (named Measure XX) substitute XX with the count you want to filter for.  Hope this helps!

View solution in original post

bcdobbs
Super User
Super User

Similar solution to @littlemojopuppy (started playing when it came in).

 

Main difference is no need to build the summary table. Won't make any difference on small datasets but should work faster if you have large amounts of data.

2+ Feedback = 
COUNTROWS (
    FILTER (
        VALUES ( Employee[ID] ),
        CALCULATE ( COUNTROWS ( Feedback ) ) >= 2
    )
)

bcdobbs_0-1646254624842.png

You could also add a disconnected parameter table with numbers from 1 to n which the measure could read so you don't need multiple versions of the code:

bcdobbs_3-1646255262562.png

 

bcdobbs_2-1646255127086.png

 

Employees With Feedback = 

VAR SelectedNumber = SELECTEDVALUE ( Parameter[Number] )

VAR Result = 
COUNTROWS (
    FILTER (
        VALUES ( Employee[ID] ),
        CALCULATE ( COUNTROWS ( Feedback ) ) >= SelectedNumber
    )
)

RETURN Result

 

bcdobbs_4-1646255343058.png

 



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!

View solution in original post

4 REPLIES 4
CNENFRNL
Community Champion
Community Champion

Excel worksheet formula is powerful enough to resolve such a simple quesiton,

CNENFRNL_2-1646260441277.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

bcdobbs
Super User
Super User

Similar solution to @littlemojopuppy (started playing when it came in).

 

Main difference is no need to build the summary table. Won't make any difference on small datasets but should work faster if you have large amounts of data.

2+ Feedback = 
COUNTROWS (
    FILTER (
        VALUES ( Employee[ID] ),
        CALCULATE ( COUNTROWS ( Feedback ) ) >= 2
    )
)

bcdobbs_0-1646254624842.png

You could also add a disconnected parameter table with numbers from 1 to n which the measure could read so you don't need multiple versions of the code:

bcdobbs_3-1646255262562.png

 

bcdobbs_2-1646255127086.png

 

Employees With Feedback = 

VAR SelectedNumber = SELECTEDVALUE ( Parameter[Number] )

VAR Result = 
COUNTROWS (
    FILTER (
        VALUES ( Employee[ID] ),
        CALCULATE ( COUNTROWS ( Feedback ) ) >= SelectedNumber
    )
)

RETURN Result

 

bcdobbs_4-1646255343058.png

 



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!

@bcdobbs you're right...I've totally gotten into this habit of creating summary tables.  I need to get out of that...  😉

littlemojopuppy
Community Champion
Community Champion

Hi @req77 

Try this...

FeedbackCount = COUNT(Feedback[Name])

Measure XX =
VAR	EmployeeSummary =
	ADDCOLUMNS(
		Employee,
		"FeedbackCount",
		FeedbackCount
	)
RETURN

COUNTROWS(
	FILTER(
		EmployeeSummary,
		FeedbackCount > XX
	)
)

In the second measure (named Measure XX) substitute XX with the count you want to filter for.  Hope this helps!

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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