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
HungryPowerBIer
Frequent Visitor

Count Multiple items by the item

Hello all! I am a bit new to Power BI and I am looking for a forumla to help me with another formula. I have items that I need to count, that have the same name and are linked to data that cannot be separated. For example

 

Orange

Orange

Orange

Orange

Banana

Banana

Banana

Kiwi

Kiwi

Plum

Plum

 

So, every version of count and/or sum I have tried gives me a total of 12 here for all items, instead of returning results like the following: 

Orange 4

Banana 3

Kiwi 2

Plum 2

 

I know this can be accomplished by visualizations, but I need either a measure or column that I can create that will would with the following formula: 

RANKX(ALLSELECTED(Query [Fruit]), [Total Fruit Count], , DESC)

 

This formula is returning a 1 value for all of my items, even though in visualizations it is returning the proper ranking. Any help would be appreciated!

1 ACCEPTED SOLUTION
BhaveshPatel
Community Champion
Community Champion

Hi @HungryPowerBIer,

 

You can also use the alternative approach by creating a new table on top of your base table.

Go to the Data tab on the left hand side, then go to modelling tab and under the calculations, you would see the new table.

Select and Paste the below DAX Syntax,

 

MyTable= SUMMARIZE(BaseTable,
                                        BaseTable[Fruit],
                                          "TotalRows",COUNTROWS(BaseTable))

 

And then create a calculated column in your newly created MyTable using the below syntax to get the ranking of the products.

RankingColumn= COUNTROWS(FILTER(MyTable,EARLIER([TotalRows])>[TotalRows]))+1

 

Thanks,

Bhavesh

 

 

Thanks & Regards,
Bhavesh

Love the Self Service BI.
Please use the 'Mark as answer' link to mark a post that answers your question. If you find a reply helpful, please remember to give Kudos.

View solution in original post

9 REPLIES 9
BhaveshPatel
Community Champion
Community Champion

Hi @HungryPowerBIer,

 

You can also use the alternative approach by creating a new table on top of your base table.

Go to the Data tab on the left hand side, then go to modelling tab and under the calculations, you would see the new table.

Select and Paste the below DAX Syntax,

 

MyTable= SUMMARIZE(BaseTable,
                                        BaseTable[Fruit],
                                          "TotalRows",COUNTROWS(BaseTable))

 

And then create a calculated column in your newly created MyTable using the below syntax to get the ranking of the products.

RankingColumn= COUNTROWS(FILTER(MyTable,EARLIER([TotalRows])>[TotalRows]))+1

 

Thanks,

Bhavesh

 

 

Thanks & Regards,
Bhavesh

Love the Self Service BI.
Please use the 'Mark as answer' link to mark a post that answers your question. If you find a reply helpful, please remember to give Kudos.

@BhaveshPatel actually I figured out how to flip it! RankingColumn= COUNTROWS(FILTER(MyTable,EARLIER([TotalRows])<[TotalRows]))+1 

 

If possible, I'd like to understand how to deal with ties in this equation but this was the correct solution. Thanks!

 

 

Hello all! I am a bit new to Power BI and I am looking for a forumla to help me with another formula. I have items that I need to count, that have the same name and are linked to data that cannot be separated. For example

 

Orange

Orange

Orange

Orange

Banana

Banana

Banana

Kiwi

Kiwi

Plum

Plum

 

So, every version of count and/or sum I have tried gives me a total of 12 here for all items, instead of returning results like the following: 

Orange 4

Banana 3

Kiwi 2

Plum 2

 

I know this can be accomplished by visualizations, but I need either a measure or column that I can create that will would with the following formula: 

RANKX(ALLSELECTED(Query [Fruit]), [Total Fruit Count], , DESC)

 

This formula is returning a 1 value for all of my items, even though in visualizations it is returning the proper ranking. Any help would be appreciated!

 

Minor Edit: The simplest way to ask this, now that I think about it, is I need something that performs what Excel does when you create a Pivot table, if possible. 

Anonymous
Not applicable

Have you tried Countrows?

 

countrows.JPG

Count Rows does the same thing. 
countrows.JPG

 

@BhaveshPatelThis worked but for two things: 

1. The ranking came back in reverse order. My highest count, came in with the highest number, instead of the highest count, showing as number 1. Is this because of the "Earlier" portion of COUNTROWS(FILTER(MyTable,EARLIER([TotalRows])>[TotalRows]))+1 ?

2. It doesn't seem to handle ties well, and duplicates the numbers, instead of continuing down or skipping like Rankx does. It's not likely to happen with my dataset but just in case it does, is there anyway to change that behavior? 

TomMartens
Super User
Super User

Hey,

 

first - welcome to this community.

 

I'm not sure if I understand your requirement correctly, but why not just create a measure that counts the items like in this screenshot:

image.png

 

Hope this is what you are looking for

 

Regards

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

A simple Count is giving problems with my second measure, which is counting all items, and not separating them out. This is hampering visuals I am working on, which require the ranking to be accurate. The ranking is returning all ones, as seen below, because it is working off the total count. countrows.JPG

Hi @HungryPowerBIer,

 

You are writing rank as a column.  Wite it as a measure

 

=RANKX(ALL(Query[Fruit]),[Just a count])

 

Does this work?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

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.