Power Query order by groups

gargamalebarbosa

Board Regular
Joined
Aug 4, 2022
Messages
118
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have a list in column A. I want to add order numbers like column B with powery query. Is that possible ?

Thank you,

1677542304643.png
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
    tbl1 = Table.AddColumn(tbl, "Custom", each try if [Name]<> tbl[Name]{[Index]-1} then true else false otherwise true),
    tbl2 = Table.Group(tbl1, {"Name"}, {{"All", each _}}),
    tbl3 = Table.TransformColumns(tbl2, {"All", each Table.AddIndexColumn(_, "Index1", 1, 1, Int64.Type)}),
    CountTrue = (tbl as table, col as text, idx as number) as number => List.Count(List.Select(List.FirstN(Table.Column(tbl, col), idx), each _=true)),
    tbl4 = Table.TransformColumns(tbl3, {"All", each Table.AddColumn(_, "Order", (x)=> CountTrue(_, "Custom", x[Index1]))}),
    tbl5 = Table.ExpandTableColumn(tbl4, "All", {"Index", "Order"}),
    tbl6 = Table.Sort(tbl5,{{"Index", Order.Ascending}}),
    Result = Table.RemoveColumns(tbl6,{"Index"})
in
    Result

Book1
ABCDE
1Table1Query Output
2
3NameNameOrder
4AA1
5AA1
6AA1
7BB1
8BB1
9AA2
10AA2
11CC1
12CC1
13CC1
14AA3
15AA3
16AA3
17BB2
18BB2
19BB2
20
Sheet1
 
Upvote 0
Solution
another approach using List.Accumulate

Power Query:
let
    names = Excel.CurrentWorkbook(){[Name="Table1"]}[Content][Name],
    lst = List.Accumulate(List.Skip(names), {{names{0},1}}, (s,c)=> s &
        (let LLs = List.Last(s) in 
            if c = LLs{0} then 
                {{c, LLs{1}}} 
            else 
                let LSs = List.Select(s, each _{0} = c) in 
                    if List.Count(LSs)= 0 then 
                        {{c,1}}
                    else 
                        {{c, List.Last(LSs){1}+1}})),
    Result = Table.FromRows(lst, {"Name", "Order"})
in
    Result
 
Upvote 0

Forum statistics

Threads
1,223,367
Messages
6,171,669
Members
452,416
Latest member
johnog

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top