Forum Discussion
How to distinct count with filter - counting measures
Hello,
Im trying to create a bar chart that counts how many of my Items are either A, B or C
I've tried the following fomula:
- Anonymous4 years ago
Hi Anonymous ,
Oh ,got it!
I built another data sample :
And the [Chart] is a measure not column:
Chart = IF(MAX('Table'[Column1])>10,"A","B")Then please try:
Count A = var _t=SUMMARIZE('Table',[Index],"Chart",[Chart]) return COUNTROWS(FILTER(_t,[Chart]="A"))Count B = var _t=SUMMARIZE('Table',[Index],"Chart",[Chart]) return COUNTROWS(FILTER(_t,[Chart]="B"))Output:
Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
17 Replies
- AnonymousNot applicable
Anonymous can you give an example of what you mean when you say "its not counting how many have A, B, C"?
Your code is working fine in my testing.CountA = CALCULATE( DISTINCTCOUNT('Table'[Item]), FILTER('Table', [Chart] = "A") )CountB = CALCULATE( DISTINCTCOUNT('Table'[Item]), FILTER('Table', [Chart] = "B") )- AnonymousNot applicable
I did each measure individually, maybe thats why. Im getting a red underline right now with CountB and CountC, How would I write this in full?
- AnonymousNot applicable
Anonymous I'm sorry I wasn't clear - those should be written as separate measures. I've edited to clarify.
- AnonymousNot applicable
Does it help to point out that 'chart' is a measure from a datediff measure then an if statement
- AnonymousNot applicable
Yes, that would've been very helpful to know beforehand. Your approach using CALCULATE is not going to work then.
- Ashish_Mathur
Super User
Hi,
Does this work?
Measure = CALCULATE(DISTINCTCOUNT('table'[Item]),'Table'[Chart] = "A"||'Table'[Chart] = "B"||'Table'[Chart] = "C")
- AnonymousNot applicable
It gives the error "A function 'Calculate' has been used in a True/False expression that is used as a table filter expression. This is not allowed."
- Ashish_Mathur
Super User
That is strange. Share the link from where i can download your PBI file.
- AnonymousNot applicable
Cant, sorry. But does it help to point out that 'chart' is a measure from a datediff measure then an if statement
- adrianc2Regular Visitor
How about:
CountA = CALCULATE( COUNTROWS('Table'),FILTER('Table', [Chart] = "A" )) CountB = CALCULATE( COUNTROWS('Table'),FILTER('Table', [Chart] = "B" ))- AnonymousNot applicable
No Luck, Its still counting all the rows
- AnonymousNot applicable
Hi Anonymous ,
If you want to calculate the count of each Chart, I have done it in two ways, please check.
1. Use the summarize type of "Count":
2. Create a Count measure:
Count Measure = CALCULATE(COUNTROWS('Table'),ALLEXCEPT('Table','Table'[Chart]) )Output:
Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.- AnonymousNot applicable
No luck unfortunetly. But to add more context 'Chart' is a measure calculated by an if statement i.e. if [value] > 10, "A" , "B"
- AnonymousNot applicable
Hi Anonymous ,
Oh ,got it!
I built another data sample :
And the [Chart] is a measure not column:
Chart = IF(MAX('Table'[Column1])>10,"A","B")Then please try:
Count A = var _t=SUMMARIZE('Table',[Index],"Chart",[Chart]) return COUNTROWS(FILTER(_t,[Chart]="A"))Count B = var _t=SUMMARIZE('Table',[Index],"Chart",[Chart]) return COUNTROWS(FILTER(_t,[Chart]="B"))Output:
Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.- AnonymousNot applicable
Perfect, thank you! can I ask what that first line _t is doing, escpecially the "chart", [chart] part?
- AnonymousNot applicable
Hi Anonymous ,
1.Since the measure returns a summarized value , we could not directly reference it for calculations later.
So in my measure, I used SUMMARIZE() to add [measure] in a table grouped by [Index]. In this case, each [Index] will have the corresponding [Chart].
2. "Chart" is just a name or said alias for [Chart], you could replace it with anything, like "XX":
Count A = var _t=SUMMARIZE('Table',[Index],"XX",[Chart]) return COUNTROWS(FILTER(_t,[XX]="A"))Best Regards,
Eyelyn Qin