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
Siboska
Helper II
Helper II

Sort Column in Power Query

Hi, 

My question is very straight forward. 
How can I replicate the following DAX calculated column in Power Query:

Siboska_0-1666357969633.png

SortColumn = CALCULATE(MAX(HistoricalDatesInitiatives[TestSort]),ALLEXCEPT(HistoricalDatesInitiatives,HistoricalDatesInitiatives[SnapShotName))


I need the column for sorting another column but because I only can figure out how to do it in DAX I get the Circular dependency error. 


1 ACCEPTED SOLUTION
ImkeF
Community Champion
Community Champion

Hi @Siboska ,
if you group on columm "SnapShotName" you can use the MAX function on the "TestSort"-column.
Then add the "All Rows"-aggregation to return all the other columns of the table as well.
Expand all columns afterwards:

ImkeF_0-1666363747848.png

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

4 REPLIES 4
wdx223_Daniel
Super User
Super User

NewStep=let a=Table.Buffer(Table.Group(HistoricalDatesInitiatives,"SnapShotName",{"n",each List.Max([TestSort])})) in Table.AddColumn(HistoricalDatesInitiatives,"SortColumn",each a{[SnapShotName=[SnapShotName]]}[n])

Anonymous
Not applicable

Hi @Siboska ,


Is Pete or Imke's answer helpful? If so, please consider marking the reply as the solution, this will help future searchers of similar questions in the forum to find the answer faster. Thanks in advance!‌‌

If the problem is still not resolved, you can continue to add details to the question below and feel free to contact us.

 

Best Regards,
Gao
Community Support Team

ImkeF
Community Champion
Community Champion

Hi @Siboska ,
if you group on columm "SnapShotName" you can use the MAX function on the "TestSort"-column.
Then add the "All Rows"-aggregation to return all the other columns of the table as well.
Expand all columns afterwards:

ImkeF_0-1666363747848.png

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

BA_Pete
Super User
Super User

 

Hi @Siboska ,

 

*EDIT* The below will just give you the rows that contain the max value of [TestSort] per each [SnapShotName] group. If you actually need all the rows, but just need the max group value duplicated over the rows, then @ImkeF 's solution below is the correct one. 👍

 

Without an example of your source data I can't give you the exact code, but the general steps would be:

1) Select [SnapShotName] column in PQ and go to Home tab > Group By.

2) In the Group By dialog, call the aggregate column 'data' and use the All Rows operator.

3) Create a new custom column with the following code:

 

Table.Max([data], "TestSort")

 

 4) Expand the new Record column, selecting the columns that you want to reinstate to the table.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




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!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

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.