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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
Mederic
Post Patron
Post Patron

Subtotals and Sort

Hello everyone,
I tried to reproduce the exercise in the video in this link.
The problem is that in my case, the sorting is done in the Product column but not in the other columns.
For example, the Season column is no longer in the same order as the initial table
Thank you in advance for your solutions

Best regards

 

Below is the initial table, the code used in the video and the result I obtained :

ProductSeasonRegion 1Region 2
A15050
A21413
A33638
A42336
B14526
B23112
B34948
B14139
C12625
C22249
C33539
C42527
D12627
D21629
D34748
D44318

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    MainTable = Table.TransformColumnTypes(Source,{{"Product", type text}, {"Season", Int64.Type}, {"Region 1", Int64.Type}, {"Region 2", Int64.Type}}),
    Subtotals = Table.TransformColumns(Table.Group(MainTable, {"Product"}, {{"Region 1", each List.Sum([Region 1]), type nullable number}, {"Region 2", each List.Sum([Region 2]), type nullable number}}), {{"Product", each "Total " & _, type text}}),
    GrandTotal = Table.Group(Subtotals, {}, {{"Region 1", each List.Sum([Region 1]), type nullable number}, {"Region 2", each List.Sum([Region 2]), type nullable number}, {"Product", each "Grand Total", Int64.Type}}),
    Custom1 = MainTable & Subtotals & GrandTotal,
    #"Added Custom" = Table.AddColumn(Custom1, "Region Total", each [Region 1]+[Region 2]),
    #"Sorted Rows" = Table.Sort(#"Added Custom", each Text.Reverse([Product]))
in
    #"Sorted Rows"

 

Sort.jpg

1 ACCEPTED SOLUTION
ronrsnfld
Super User
Super User

Although I could not reproduce your problem with your data and your M-Code, Table.Group is not guaranteed to keep the original sort order.

 

So I cannot test this workaround. However, I would try adding an Index column after the Main Table step (and deleting after the last step).

 

View solution in original post

7 REPLIES 7
Mederic
Post Patron
Post Patron

@ronrsnfld , @lbendlin ,

I've just tested it in Office 365 and no problem, the sorting is preserved (see screenshot below).
So it depends on the version of Excel
Have a nice evening

Subtotals.png

 

Hi @Mederic ,

Glad that your query got resolved, please mark the helpful reply and accept it as solution, it will be helpful for other members of the community who have similar problems as yours to solve it faster .


Should you have any further questions, feel free to reach out.
Thank you for being a part of the Microsoft Fabric Community Forum!

ronrsnfld
Super User
Super User

Although I could not reproduce your problem with your data and your M-Code, Table.Group is not guaranteed to keep the original sort order.

 

So I cannot test this workaround. However, I would try adding an Index column after the Main Table step (and deleting after the last step).

 

lbendlin
Super User
Super User

ok, but why?  Sorting in Power Query is largely pointless as the sort order will be ignored once the data is loaded into Power BI. 

 

Grouping is ok but it comes with a loss of detail.

 

These things should be done in Power BI, not in Power Query.

Hello everyone,

Thank you for your answers,

@lbendlin , it's just an exercise, I'm surprised that in the video the sorting is maintained and in my case the order is no longer the same.

@ronrsnfld , I've tried it with an Index column too, but I can't get the desired result. I've probably got it wrong.

 

Also, in my 1st file, I didn't have the same "Season" numbers as in the video. I've just corrected this column
The test file can be found at this link

Thanks in advance
Best regards

In your uploaded file, I find:

  1. The worksheet table is incorrectly sorted as you show when I first open it.
  2. Both tables in Power Query are correctly sorted.
  3. Refreshing the table in the worksheet results in it showing the correct sort order on the worksheet.

I am at a loss since I cannot reproduce your issue.

 

Although, as @lbendlin pointed out, the sort order may not be retained in Power BI, I've never seen that happen in Excel.

 

Thanks @ronrsnfld 
Strange indeed,

I use the Excel 2019 Pro version,
I'll try it tomorrow on the Office 365 work version to see the result.
Best regards

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

Find out what's new and trending in the Fabric community.