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
Anonymous
Not applicable

Filter top 3 rows for each userID in PowerBI

Hi all,   I am new to PowerBI. 

I am trying to get below desired output in 3-steps.

 

Screenshot: 

skondi_0-1592941969055.png

 

T1-Raw data : 

UserIDGroupSpecialty
101a1S-51
101a2S-51
101a3S-51
101a2S-51
101a5S-51
101a1S-51
101a2S-51
201a1S-9
201a3S-9
201a3S-9
201a4S-9
201a5S-9
201a5S-9
201a1S-9
301a1S-15
301a1S-15
301a5S-15

 

Step -1 (T2-count per group per user) : 

Here I can use a measure , but I am making a summary table for understanding purpose.

 

summary = SUMMARIZE(T1-Raw data,SampleData[UserID],T1-Raw data[Specialty],T1-Raw data[Group],"countof",count(T1-Raw data[Group]))

 

Step -2 (T3- Sorted and top-3 rows filtered for each UserID) : 

I am not getting any logic for this ..I tried this simple ranking ,but its not working.

 

Rank = RANKX('T1-Raw data','T1-Raw data'[countof],,ASC,Dense)

 

 Step -3 (T4-Final output using CONCATENATEX) : 

Here I can use CONCATENATEX function to show the the data in single row for each user.

 

So can someone help me for step-2?

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous ,

 

Create 2 measures as below:

 

Measure= CALCULATE(COUNT('Table'[Group]),FILTER('Table','Table'[UserID]=MAX('Table'[UserID])))
T2 = IF(RANKX(FILTER(ALL('Table'),'Table'[UserID]=MAX('Table'[UserID])&&'Table'[Specialty]=MAX('Table'[Specialty])), 
RANKX(FILTER(ALL('Table'),'Table'[UserID]=MAX('Table'[UserID])&&'Table'[Specialty]=MAX('Table'[Specialty])),CALCULATE(COUNTA('Table'[Group])),,DESC,Dense)+
RANKX(FILTER(ALL('Table'),'Table'[UserID]=MAX('Table'[UserID])&&'Table'[Specialty]=MAX('Table'[Specialty])),CALCULATE(MAX('Table'[Group])),,ASC,Dense)
/COUNTROWS(FILTER(ALL('Table'),'Table'[UserID]=MAX('Table'[UserID]))),,ASC,Dense)<=3,'Table'[Measure 2],BLANK())

 

And you will see:

Annotation 2020-06-24 154239.png

And for D1 and D2,see below:

 

_D1 = CONCATENATEX(FILTER(DISTINCT('Table'),'Table'[T2]<>BLANK()),'Table'[Group],",")
_D2 = CONCATENATEX(FILTER(DISTINCT('Table'),'Table'[T2]<>BLANK()),'Table'[Measure],",")

 

And you will see:

Annotation 2020-06-24 154159.png

For the related .pbix file,pls click here.

 

Best Regards,
Kelly
Did I answer your question? Mark my post as a solution!

 

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Hi @Anonymous ,

 

Create 2 measures as below:

 

Measure= CALCULATE(COUNT('Table'[Group]),FILTER('Table','Table'[UserID]=MAX('Table'[UserID])))
T2 = IF(RANKX(FILTER(ALL('Table'),'Table'[UserID]=MAX('Table'[UserID])&&'Table'[Specialty]=MAX('Table'[Specialty])), 
RANKX(FILTER(ALL('Table'),'Table'[UserID]=MAX('Table'[UserID])&&'Table'[Specialty]=MAX('Table'[Specialty])),CALCULATE(COUNTA('Table'[Group])),,DESC,Dense)+
RANKX(FILTER(ALL('Table'),'Table'[UserID]=MAX('Table'[UserID])&&'Table'[Specialty]=MAX('Table'[Specialty])),CALCULATE(MAX('Table'[Group])),,ASC,Dense)
/COUNTROWS(FILTER(ALL('Table'),'Table'[UserID]=MAX('Table'[UserID]))),,ASC,Dense)<=3,'Table'[Measure 2],BLANK())

 

And you will see:

Annotation 2020-06-24 154239.png

And for D1 and D2,see below:

 

_D1 = CONCATENATEX(FILTER(DISTINCT('Table'),'Table'[T2]<>BLANK()),'Table'[Group],",")
_D2 = CONCATENATEX(FILTER(DISTINCT('Table'),'Table'[T2]<>BLANK()),'Table'[Measure],",")

 

And you will see:

Annotation 2020-06-24 154159.png

For the related .pbix file,pls click here.

 

Best Regards,
Kelly
Did I answer your question? Mark my post as a solution!

 

Anonymous
Not applicable

 

Thanks for providing me this solution, But I am not able to understand how it can work if there is no specailty  column. I think you are groupby back with specailty  which I am not willing. I want to rank and concat the top3 values only w.r.t each user.

Can you provide some explaination for the same?

harshnathani
Community Champion
Community Champion

HI @Anonymous ,

 

You can try these measures.

 

D1 =
CONCATENATEX (
    TOPN (
        3,
        SUMMARIZE (
            'Table',
            'Table'[UserID],
            'Table'[Specialty],
            'Table'[Group],
            "countof", CALCULATE (
                COUNT ( 'Table'[Group] ),
                FILTER (
                    'Table',
                    'Table'[UserID]
                        = MAX ( 'Table'[UserID] )
                        && 'Table'[Specialty]
                            = MAX ( 'Table'[Specialty] )
                )
            )
        ),
        [countof]
    ),
    'Table'[Group],
    ","
)

 

 

 

D2 =
CONCATENATEX (
    TOPN (
        3,
        SUMMARIZE (
            'Table',
            'Table'[UserID],
            'Table'[Specialty],
            'Table'[Group],
            "countof", CALCULATE (
                COUNT ( 'Table'[Group] ),
                FILTER (
                    'Table',
                    'Table'[UserID]
                        = MAX ( 'Table'[UserID] )
                        && 'Table'[Specialty]
                            = MAX ( 'Table'[Specialty] )
                )
            )
        ),
        [countof]
    ),
    [countof],
    ","
)

 

 

1.jpg

 

 

Regards,
Harsh Nathani

Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)

Anonymous
Not applicable

Thanks for providing me this solution, But I am not able to understand how it can work if there is no specailty  column. I think you are groupby back with specailty  which I am not willing. I want to rank and concat the top3 values only w.r.t each user.

Can you provide some explaination for the same?

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.