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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
mellomoon3
Regular Visitor

Pie Chart on the Count of ID based on how many different ID_2

I have the following table:

ID ID_2 Date

1 234 Jan '24

1 829 Feb '24

2 981 Jan '24

2 146 Mar '24

2 286 Nov '24

3 321 May '24

3 743 Feb '24

 

I want to be able to create a pie chart where it counts the number of ID based on how many different ID_2 there are but take into account for any filter, in this case the date filter.

 

For example, if we use all the data in the table, there are 2 IDs where count ID_2 = 2 and 1 ID where count ID_2 = 3.

If we limit the date to just Jan '24 - Feb '24 then there are 2 IDs where count ID_2 = 1 and 1 ID where 1 ID where count ID_2 = 2.

 

How would I write the measure(s) for this so that I can create a pie chart for this?

 

Appreciate any guidance. Thanks in advance!

1 ACCEPTED SOLUTION
danextian
Super User
Super User

Hi @mellomoon3 

 

If you intend to use  the count of ID_2 per ID as a legend or category then you can't be using a measure for that as measures don't have row context and their result are dependen on the dimensions added to a  visual.

 

This calculated column formula counts the ID_2 for each ID

Count of ID_2 by ID = 
COUNTROWS ( FILTER ( 'Table', 'Table'[ID] = EARLIER ( 'Table'[ID] ) ) )

danextian_0-1739612824964.png

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

4 REPLIES 4
rohit1991
Super User
Super User

Hi @mellomoon3 ,
To create a pie chart in Power BI that counts the number of IDs based on the number of distinct ID_2 values while considering date filters, you need to define two DAX measures. The first measure calculates the distinct count of ID_2 per ID, ensuring it dynamically responds to any applied filters. This can be achieved using CALCULATE and DISTINCTCOUNT, preserving the grouping at the ID level while allowing filters such as date selection to take effect. The measure is written as follows:

DistinctID2Count = 
VAR UniqueID2 = CALCULATE(
    DISTINCTCOUNT('Table'[ID_2]),
    ALLEXCEPT('Table', 'Table'[ID])
)
RETURN UniqueID2

Next, a second measure is required to count how many IDs belong to each distinct ID_2 count category, which enables proper grouping for visualization in the pie chart. This measure aggregates the count of IDs for each distinct ID_2 count value, ensuring that it dynamically adjusts when date filters are applied:

IDCountByID2 = 
VAR ID2Groups = SUMMARIZE(
    'Table',
    'Table'[ID],
    "DistinctID2Count", [DistinctID2Count]
)
RETURN COUNTROWS(ID2Groups)

To use these measures in the pie chart, place DistinctID2Count as the category (legend) and IDCountByID2 as the values. This setup ensures that the chart dynamically adjusts to any applied date filter, correctly reflecting the count of IDs based on the number of distinct ID_2 values in the filtered data.



 


Did it work? ✔ Give a Kudo • Mark as Solution – help others too!

Thank you so much for your help!

 

I am not sure why, but it is not letting me place DistinctID2Count as the category (legend) in a pie chart. Do you know any reason why that is?

danextian
Super User
Super User

Hi @mellomoon3 

 

If you intend to use  the count of ID_2 per ID as a legend or category then you can't be using a measure for that as measures don't have row context and their result are dependen on the dimensions added to a  visual.

 

This calculated column formula counts the ID_2 for each ID

Count of ID_2 by ID = 
COUNTROWS ( FILTER ( 'Table', 'Table'[ID] = EARLIER ( 'Table'[ID] ) ) )

danextian_0-1739612824964.png

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Thank you so much for your help! I tried this and it worked.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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