cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
TiagoTED
Frequent Visitor

Count values in one column based on values in another column using DAX

Hi everyone!
I need to generate a new column in my report using DAX. This new column should contain the sum of occurrences of a value in Column A based on the values in Column B.

 

Can someone help me?

 

Table example.


Column A          Column B (only unique values)
------------------------------------
Car                     Jet
Jet                      Car
Bus                     Bus
Bus                     Bike
Car                     Plane
Car                     Boat

 

 

I would like the output to be:
Column A        Column B         Column C
----------------------------------------------------------
Car                  Jet                         1
Jet                   Car                        3
Bus                  Bus                        2
Bus                  Bike                       0
Car                  Plane                     0
Car                  Boat                      0

Here is an example in python of what I need to do in DAX.

 

List = []
for value in df['Column B']:
      List.append (df[Column A].str.count(value).sum())
df['nova coluna'] = List
 

 PS. I can't use python script in power bi.

 

 

 

1 ACCEPTED SOLUTION
VahidDM
Resident Rockstar
Resident Rockstar

Hi @TiagoTED 

 

Try this code to add a new column:

Column C =
VAR _B =
    FIRSTNONBLANK ( 'Table'[Column B], "" )
VAR _C =
    CALCULATE (
        COUNTROWS ( 'Table' ),
        FILTER ( ALL ( 'Table' ), 'Table'[Column A] = _B )
    )
RETURN
    IF ( ISBLANK ( _C ), 0, _C )

 

Output:

VahidDM_0-1631786978850.png

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.

Appreciate your Kudos✌️!!

 

 

View solution in original post

2 REPLIES 2
VahidDM
Resident Rockstar
Resident Rockstar

Hi @TiagoTED 

 

Try this code to add a new column:

Column C =
VAR _B =
    FIRSTNONBLANK ( 'Table'[Column B], "" )
VAR _C =
    CALCULATE (
        COUNTROWS ( 'Table' ),
        FILTER ( ALL ( 'Table' ), 'Table'[Column A] = _B )
    )
RETURN
    IF ( ISBLANK ( _C ), 0, _C )

 

Output:

VahidDM_0-1631786978850.png

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.

Appreciate your Kudos✌️!!

 

 

View solution in original post

TiagoTED
Frequent Visitor

Thanks so much for the help, the code worked perfectly!
I hope this answer can help others!

Helpful resources

Announcements
PBI_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Power BI October Update 2021.jpg

Power BI Release

Click here to read more about the October 2021 Release!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Teds Dev Camp Oct. 2021 768x460.jpg

Power BI Dev Camp - October 28th, 2021

Mark your calendars and join us for our next Power BI Dev Camp!