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

TopN of TopN

I have a table, Outpatient Referrals with fkMedClassID and fkAdmittingDiagnosisID columns. It has a many to one relationship to the pks on the dim tables MedClass and Diagnosis_Admitting tables. There are also many to one relationships to the department and date dim tables used to filter.

 

I have a business need to give, using the Outpatient Referrals table as the base, the top 5 MedClass.PrimaryMedClasDscr and for each of the 5 I need the top 3 Diagnosis_Admitting.MedClassDxSubCategory. Both need to be filterable by any report or visual filters.

 

I've only been doing this a month or so and have managed to get by so far with examples online. I can't seem to find a relevant example for this with counts. All of the topn and rankx examples I've found seem to be with sums of columns from the main table like a total amount.  I thought i'd be able to use the 2 dim table fields and do a top n in the filter but it will only let me do one per filter. I'm not even sure what visuals would best display this at this point.  Any help at all would be greatly appreciated.

OP Table

fkMedClassIDfkDiagnosisID
11
11
12
12
12
15
459
4545
459
456
456
457
4545
966
966
967
967
967
961
8788
8788
877
874
874
991
992
993
5255
4868

 

MedClass table

pkMedClassIDDesc
1class 1
45class 45
96class 96
87class 87
99class 99
52class 52 
48class 48

 

Diagnosis Table (Dx)

pkDiagnosisIDDesc
1Dx1
2Dx2
3Dx3
4Dx4
5Dx5
6Dx6
7Dx7
9Dx9
45Dx45
55Dx55
68Dx68
88Dx88

 

Results (counts)

Top 5 MedClass by countTop 3 Dx by count
Class 45 (7)Dx9 (2)
 Dx45 (2)
 Dx6 (2)
class 1 (6)Dx2 (3)
 Dx1 (2)
 Dx5 (1)
class 96 (6)Dx7 (3)
 Dx6 (2)
 Dx1 (1)
class 87 (5)Dx88 (2)
 Dx4 (2)
 Dx 7 (1)
class 99 (3)Dx 1 (1)
 Dx 2 (1)
 Dx 3 (1)

 

the results would be displayed in maybe a stacked bar graph based on the actual counts from the OP table of the Top 5 and the actual counts fromt the OP table of the Top 3 Dx within those Top 5. So, not a count of all the Top 3 records, just a count of the Top 3 only within each MedClass in the Top 5.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @tviv23 ,

Please refer to my pbix file to see if it helps you.

Create relationships between the tables.

vpollymsft_0-1651648094246.png

Then create columns.

times =
CALCULATE (
    COUNT ( 'OP Table'[fkMedClassID] ),
    FILTER (
        ALLSELECTED ( 'OP Table' ),
        'OP Table'[fkMedClassID] = EARLIER ( 'OP Table'[fkMedClassID] )
    )
)
sort =
RANKX (
    'OP Table',
    'OP Table'[times] * 1000 + 'OP Table'[fkMedClassID],
    ,
    DESC,
    DENSE
)
column_sort =
VAR _times = 'OP Table'[times]
VAR text_ = "(" & _times & ")"
RETURN
    text_

 Then create two measures.

medclassdesc =
VAR _times =
    MAX ( 'OP Table'[times] )
VAR text_ = "(" & _times & ")"
VAR classmeddesc =
    MAX ( 'Medclass Table'[Desc] )
RETURN
    classmeddesc & MAX ( 'OP Table'[column_sort] )
diagnosis = MAX('Diagnosis Table (Dx)'[Desc])

vpollymsft_1-1651648397728.png

If I have misunderstood your meaning, please provide more details with youe desired output.

 

Best Regards

Community Support Team _ Polly

 

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

 

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Hi @tviv23 ,

Does that make sense? If so, kindly mark my answer as the solution to close the case please. Thanks in advance.

 

Best Regards

Community Support Team _ Polly

 

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

Anonymous
Not applicable

Hi @tviv23 ,

Please refer to my pbix file to see if it helps you.

Create relationships between the tables.

vpollymsft_0-1651648094246.png

Then create columns.

times =
CALCULATE (
    COUNT ( 'OP Table'[fkMedClassID] ),
    FILTER (
        ALLSELECTED ( 'OP Table' ),
        'OP Table'[fkMedClassID] = EARLIER ( 'OP Table'[fkMedClassID] )
    )
)
sort =
RANKX (
    'OP Table',
    'OP Table'[times] * 1000 + 'OP Table'[fkMedClassID],
    ,
    DESC,
    DENSE
)
column_sort =
VAR _times = 'OP Table'[times]
VAR text_ = "(" & _times & ")"
RETURN
    text_

 Then create two measures.

medclassdesc =
VAR _times =
    MAX ( 'OP Table'[times] )
VAR text_ = "(" & _times & ")"
VAR classmeddesc =
    MAX ( 'Medclass Table'[Desc] )
RETURN
    classmeddesc & MAX ( 'OP Table'[column_sort] )
diagnosis = MAX('Diagnosis Table (Dx)'[Desc])

vpollymsft_1-1651648397728.png

If I have misunderstood your meaning, please provide more details with youe desired output.

 

Best Regards

Community Support Team _ Polly

 

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

 

Thanks for taking the time to propose a solution. Trying to get some time to evaluate.

Whitewater100
Solution Sage
Solution Sage

Hi:

I think some sample data would help with an example of expected results. Thanks..

Thanks added. Hopefully that helps.

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.