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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
jlarques
Resolver II
Resolver II

Count rows with same code

Dear community,

I have a fact table with same country code in many rows, and I want to create a new calculated column that:

  • If country code appears one time, new column will have 0
  • If country code appears two times, new column will have 1 
  • If country code appears three times, new column will have 2, etc. 

How can I do it? In DAX language or in Power Query? I have tried many times but I have not succeeded. 

 

Can you help me? 

 

Kind regards,

 

José Luis 

2 ACCEPTED SOLUTIONS
ryan_mayu
Super User
Super User

@jlarques 

you can try to create an index column in pq

 

then use DAX to create a column

 

Column = CALCULATE(count('Table'[country]),FILTER('Table','Table'[country]=EARLIER('Table'[country])&&'Table'[Index]<=EARLIER('Table'[Index])))
 
1.PNG
 
if you have a datetime column, you can use that instead of the index column




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

Proud to be a Super User!




View solution in original post

Hi @ryan_mayu ,

 

thanks for your answer. I tested and it works! It's amazing!. I would never have achieved it and I am extremely grateful that knowledge can be shared in this community. Thank you very much for your help and @v-kongfanf-msft  and @vicky_ who have also wanted to help me with this, for me, difficult problem.

 

Thanks again to this community.

 

José Luis 

View solution in original post

6 REPLIES 6
ryan_mayu
Super User
Super User

@jlarques 

you can try to create an index column in pq

 

then use DAX to create a column

 

Column = CALCULATE(count('Table'[country]),FILTER('Table','Table'[country]=EARLIER('Table'[country])&&'Table'[Index]<=EARLIER('Table'[Index])))
 
1.PNG
 
if you have a datetime column, you can use that instead of the index column




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

Proud to be a Super User!




Hi @ryan_mayu ,

 

thanks for your answer. I tested and it works! It's amazing!. I would never have achieved it and I am extremely grateful that knowledge can be shared in this community. Thank you very much for your help and @v-kongfanf-msft  and @vicky_ who have also wanted to help me with this, for me, difficult problem.

 

Thanks again to this community.

 

José Luis 

you are welcome





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

Proud to be a Super User!




v-kongfanf-msft
Community Support
Community Support

Thanks for the reply from @vicky_ , please allow me to provide another insight:

 

Hi @jlarques ,

 

Maybe create  calculated column is a good and easy choose. You can try below formula:

Column =
VAR CUR_ = 'Table'[Country Code]
RETURN
    COUNTROWS ( FILTER ( ALL ( 'Table' ), 'Table'[Country Code] = CUR_ ) ) - 1

vkongfanfmsft_0-1714100478764.png

Best Regards,
Adamk Kong

 

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

Hi @vicky_ and @v-kongfanf-msft ,

 

thanks for your help. The @vicky_ solution gives me one everytime that same country code appears, not increase the number of times that appears.

 

The @v-kongfanf-msft solution gives me the total times that the country code appears. So, these are not exactly the solution that I'm looking for. In your screenshot,every time that appears a country counts the total times that this country appears and my idea is that gives me the number of times that appears the country in this row. For instance, first time that appears GER would be 0, second time would be 1, next time would be 2, etc., etc.

 

Thanks again.

 

Best regards, 

 

José Luis

vicky_
Super User
Super User

You can create a DAX column (or measure) with:
Count of Country Code = CALCULATE(COUNT(Table[Country Code]), ALLEXCEPT(Table, Table[Country Code]))

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.