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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
SINGHTX9
Helper II
Helper II

Urgent help needed with groupby and distinct count

Hi All

 

Please, can anyone really help me to solve my issue. I have already posted my issue few time but no one could give me a solution.Actual dataActual dataMatrix dataMatrix data

 

I have the records for a laboratory receiving two type of tests: CC or IA.

So each Accession ID can have:

  • CC tests only
  • IA tests only
  • CC IA tests both 
  • No tests

So i want to unpivot the test type column but doing transpose doesnt help me.

 i tried to create a matrix table (Attached pic) so as you can see it returns the count of "CC" and "IA" tests, but my end goal is to 

get a tabel with 

 

Accession ID     CC    IA

 

and the columns CC and IA should have only binary number i.e if that accession ID has "CC" tests then return 1 else 0. Similarly with IA tests. I dont care about the number, all i care is whether the accession ID has the "CC" test ordered or not.

End resultEnd result

2 ACCEPTED SOLUTIONS

Also I had alreday creating the two columns CC and IA using If statements as you had suggested above but this was the result i got. As expected it was missing IA and CC common accesion id and returning it as zero

 

Venn diagram using the IF statements.png

View solution in original post

Anonymous
Not applicable

Okay. Then after creating the CC and IA columns, you create one calculated table called "FinalTable" with the following expression.

 

FinalTable = ADDCOLUMNS(DISTINCT(ActualData[AccessionID]),"CCS",sumx(RELATEDTABLE(ActualData),ActualData[CC]),"IAS",SUMX(RELATEDTABLE(ActualData),ActualData[IA]))

This will create the table as per your requirement.

View solution in original post

14 REPLIES 14
Anonymous
Not applicable

Hi,

 

Based on what I have understood from your post, your issue can be solved as follows...

 

Your Table

 

AccessionIDTestType
214326616IA
214326625CC
214326625IA
214326637CC
214326641IA
214326656IA

 

First you have to add two calculated columns to Actual Data table as suggested below..

 

CC = IF(ActualData[TestType]="CC",1,0)

and

IA = IF(ActualData[TestType]="IA",1,0)

After adding these calculated columns, your table will look like this...

 

AccessionIDTestTypeCCIA
214326616IA01
214326625CC10
214326625IA01
214326637CC10
214326641IA01
214326656IA01

 

Now you may use either a Matrix or Table visualisation in Power BI Desktop and add AccessionID to the rows, CC and IA calculated columns to the values. which will give the following result.

 

AccessionIDCCIA
21432661601
21432662511
21432663710
21432664101
21432665601
Grand Total24

 

 

Hope this solves your difficulty.

Hi shree 

 

Thanks for your response but this doesnt help my solution. As you can clearly see in the solution you posted there are no common accession ID with IA and CC tests both.

 

I want a table like below:

 

essionIDTestTypeCCIA
214326616IA01
214326625CC11
    
214326637CC10
214326641IA01
214326656IA01

 

My aim is to create a venn diagram with  sum(cc) and sum(ia) and sum(accession id).

 

Do you know how i can create a table like above? I could only think of groupby and then putting distinct count on accession id

Anonymous
Not applicable

In your example, the accessionID 214326625 has both IA and CC. So against that accession ID, 1 will appear under both IA and CC. Correct? 

 

 

Yes, this is exactly what i want. Do you know how to do it?

Anonymous
Not applicable

Hi, 


We have already did it. Please observe my first reply. The last table shows the result that you want.

Anonymous
Not applicable

Let me clarify, the table will show 2 rows for that Accession ID. One for IA and one for CC. But when you use this table's data in a visualisation like Matrix or Table, the result will be the desired one.

Hi Shree 

 

Sorry if i couldnt explain in my previous reply so my final aim is to create a venn digramvenn diagram.png

 

and for that i need to load in catergory sum(CC) and Sum(IA) and CC and IA need to be in columns i.e why instead of having a matrix table or table visualization..i need an actual table to be created from my exsisting data

Also I had alreday creating the two columns CC and IA using If statements as you had suggested above but this was the result i got. As expected it was missing IA and CC common accesion id and returning it as zero

 

Venn diagram using the IF statements.png

Anonymous
Not applicable

Okay. Then after creating the CC and IA columns, you create one calculated table called "FinalTable" with the following expression.

 

FinalTable = ADDCOLUMNS(DISTINCT(ActualData[AccessionID]),"CCS",sumx(RELATEDTABLE(ActualData),ActualData[CC]),"IAS",SUMX(RELATEDTABLE(ActualData),ActualData[IA]))

This will create the table as per your requirement.

Thanks a lot sree. It was a big help, i was struggling with it for past few days, If i want to extract another column from my original table (Called Priority) into my new table (Final table), how will your formula change.

 

That is i want:

 

Accession ID(From actual data)  Priority(From actual data)    CCS    ICS

Anonymous
Not applicable

Hi, There is no need to add fields unnecessarily. I think you can just create a relationship between the Final Table's AccessionID and the original table's accession ID and use the Priority field in your reports. Just try and let me know.

 

By the way, please avoid thanks and acknowledgements. Just click the Kudos button if the post is helpful to you.

Hi do you think you can help me solve this issue?

Anonymous
Not applicable

The screen-shots are not really of any help. I am not able to figure-out what's wrong with these screenshots. 

I cant find the table in the relationship tab.table data.png

 

 

Also can you help me why my pie chart is not picking the right data.

pie chart.pngFinal table.png

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.