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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
ksobota
Regular Visitor

DAX Function for COUNTIF and/or CALCULATE

Hello,

 

I am fairly new to Power BI and am even newer to the use of DAX functions. For years I have worked in Excel and know how I would do this there, but am finding the same formula I would use there does not work in Power BI. Basically, I need the DAX equivalent of the COUNTIF function (such as =COUNTIF(B$1:B$50,D11)) in Excel.

 

I have two tables. In my EMPLOYMENTS table, the Member_C column contains the User IDs of people who have/had a job. If a person has had more than one job, their User ID would appear more than once in this column, once for each job they have/had. In my INDIVIDUAL table, the ID column lists all User IDs in the system. 

 

What I want to do is set up a new column in the INDIVIDUAL table that counts the number of times a User ID appears in the EMPLOYMENTS table if it matches the User ID of each row in the INDIVIDUAL table.

 

If I were to do this in Excel, I would create a column on my INDIVIDUAL table and each cell would have the formula: =COUNTIF('EMPLOYMENTS'!B$1:B$10,D9)

 

I need to figure out what the equivalent of doing that in DAX would be so that for each row in INDIVIDUAL table, it will look at the value in the ID column and count how many times that specific value appears in the EMPLOYMENTS table.

 

Any suggestions would be much appreciated!

 

Thanks!

1 ACCEPTED SOLUTION
v-caliao-msft
Microsoft Employee
Microsoft Employee

Hi @ksobota,

 

Based on your scenario, you can use the DAX below on your INDIVIDUAL table.

CountUser = CALCULATE(COUNTA(EMPLOYMENTS[Member_C]),FILTER(ALL(EMPLOYMENTS),EMPLOYMENTS[Member_C]=INDIVIDUAL[User_ID]))

 

Capture.PNG

 

Regards,

Charlie Liao

View solution in original post

33 REPLIES 33
Anonymous
Not applicable

Thanks for prompt reply.

 

I tried to change the data type but due to "0101_150190142_DRX0062220_1720.91" type of values it's giving error as "Cannot convert value '0101_150190142_DRX0062220_1720.91' of type Text to type Integer".

Anonymous
Not applicable

Then that's not the column you have to sum. Find the column where you have the numeric value.

Anonymous
Not applicable

ok.. actually i want to find "toal shipment" for particular plant & Material and rest other calculations are based on that shipment count. Instead of using SUM i tried using Count/Distinct but it's not giving correct output. I am also trying to find out the other way. 

Thanks 🙂

Anonymous
Not applicable

It seems that the column Plant_Ship-to_Shipment_Material type is set to Text. Change it to Number

v-caliao-msft
Microsoft Employee
Microsoft Employee

Hi @ksobota,

 

Based on your scenario, you can use the DAX below on your INDIVIDUAL table.

CountUser = CALCULATE(COUNTA(EMPLOYMENTS[Member_C]),FILTER(ALL(EMPLOYMENTS),EMPLOYMENTS[Member_C]=INDIVIDUAL[User_ID]))

 

Capture.PNG

 

Regards,

Charlie Liao

Hi, 

I'm new to Power BI. I have been trying to figure if there is a way to use the formula var.p (to calculate variance) in order to calculate variance of approved quantity for each unique item code out of  the 300 unique item codes in total. The items were repeated but I have already grouped them into just the unique values in a new table along with their count values to the no'of time they have occured. All other data remains in the main sheet. Now, I need to figure how to calculate variance of the approved quantities for each item code.

 

Any suggestions would be much appreciated:)

 

Thank you 

Anonymous
Not applicable

Hi @v-caliao-msft ,

 

I am facing a smiliar issue while replicating COUNTIF of excel in DAX and i saw your solution however when i applied the same DAX i am not getting the table name from which i need to compare my value :

 

Please see below screenshot of my dax where i am trying to get counts the number of times a SF ID appears in the Sheet 1 table if it matches the Taleo Req ID of each row in "Referral Tracker":

ashishkr14_0-1647233946307.png

 

Request you to please help.

 

Regards,

Ashish

 

CahabaData
Memorable Member
Memorable Member

Assuming there is join line on ID field between the 2 tables; have you explored the results of a table visual?  Start a table visual and drag the Employments ID field onto it twice - in the second field change its Values option to Count.

 

This might give you what you seek.

 

 

www.CahabaData.com

Thanks for the suggestion. That does solve my initial issue in that it calculates the number of times the Member_c appears in the EMPLOYMENTS table, but I am wondering then how I would go about my next steps, which would then be to be able to calculate an average number of employments per Member_C. Without creating a measure or a calculate column, how would I determine the average, as the Count  of Member_c only exists in that table. 

 

In the below example, I would need to be able to total my number of Member_c and divide the sum of my Count of Member_c (140) by the total number of Member_c.

 

How would I do that?

well I would then revert to establishing this as part of the table model.  In your Individuals table, add a calculated column that is the count of the ID in the Employments table.

EmploymentCount = COUNTROWS(
                                                          FILTER(Employments,Employments[ID]=Individuals[ID])
                                                          )  

 

then add another calculated column that calculates the average

www.CahabaData.com
wonga
Continued Contributor
Continued Contributor

@ksobota Not too sure if this would be the right way to go about it without some sample data to work with. There should be a many to one relationship between EMPLOYMENTS and INDIVIDUALS.  As for the DAX formula, maybe something like:

 

UserIDMatchCount =

CALCULATE
(
    COUNT(EMPLOYMENTS[UserID]),
    EMPLOYMENTS[UserID] =  INDIVIDUAL[UserID]
)
Sieiro1
Microsoft Employee
Microsoft Employee

Works like a charm!

 

I replaced the last part: 

INDIVIDUAL[UserID]

with "Text" , and made it into a measure. Just what I needed.

 

Thanks so much!

I tried the expression given, but received the following error: The expression contains multiple columns, but only a single column can be used in a True/False expression that is used as a table filter expression.

 

Not sure if including screenshots of the data would help--so I want to create a column on the INDIVIDUAL table that will look at the Id column and count how many times that same value appears on the EMPLOYMENT table in the Member_c column (screenshots below):

 

2016-09-08 10_42_22-MVW Metrics - KS - Power BI Desktop.pngINDIVIDUAL Table (Id column is far left)

 

 

 

 

 

 

 

 

 

 

 

 

 

2016-09-08 10_40_01-MVW Metrics - KS - Power BI Desktop.png

EMPLOYMENT table (Member_c column is highlighted)

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.