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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
cyborgdroid
Frequent Visitor

Python - groupby function not working

I am trying to rank within a group in python. I know how to do this outside of python in powerBI, and also how to do it in python but the python script function yields errors. I tried using groupby + rank and also groupby + transform and both give errors. When inspecting the advanced editor, additional quotes were added to the function by powerBI, I removed them and it still didn't fix the error.

 

dataset['rank'] = dataset.groupby(""Category"")[""value""].rank(method=""dense"", ascending=False)


https://stackoverflow.com/questions/46998281/how-to-rank-within-a-group-in-python

 

groupbyerror.PNG


dataerror.PNG

2 REPLIES 2
cyborgdroid
Frequent Visitor

Maybe there is some syntax issue that I am missing? If this isn't working in python then perhaps a lot of things won't work with python in PBI since this is a very basic pandas function that does not require importing any external libraries. PBI imports pandas by default and uses pandas dataframes as the default data structure.

v-juanli-msft
Community Support
Community Support

Hi @cyborgdroid

Since I am not the expert of Python, I can't figure out how to do the rank task with Python.

In Power BI, there is a simple way to do that.

Out of Edit queries, in the Report view, create measures

Measure = SUM(Table1[value])

Measure 2 = RANKX(ALLEXCEPT(Table1,Table1[category]),[Measure],,DESC)

2.png

 

 

Best regards

Maggie

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors