Power Query Count Occurrence

ajw5173

New Member
Joined
Apr 7, 2016
Messages
45
Hi, I am looking to create a column that shows how many times a value has occurred so far in the table.

For example

Style Occurence
abc | 1
bcd | 1
edg | 1
abc | 2
abc | 3
bcd | 2
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
This works once it's sorted:

let
Source = #table({"Name"}, {{"a"},{"a"},{"a"},{"b"},{"b"},{"b"},{"c"}}),
List = Source[Name],
List2 = List.Generate (
()=> [Counter = 1, Occ = 1],
each _[Counter] <= List.Count(List),
each [Counter = _[Counter] + 1, Occ = if List{_[Counter]} = List{_[Counter]-1} then _[Occ] + 1 else 1],
each _[Occ]
),
#"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "Occurance", each List2{[Index]}),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"})
in
#"Removed Columns"
 
Upvote 0
Power Query:
let
    lst = List.Buffer(Excel.CurrentWorkbook(){[Name="Table4"]}[Content][Style]),
    lst1 = List.Accumulate(List.Skip(lst), {{lst{0},1, 2}}, (s,c)=> let ll = List.Last(s){2} in s & {{c, List.Count(List.Select(List.FirstN(lst, ll), each _ = c)), ll +1}}),
    Result = Table.FromColumns(List.FirstN(List.Zip(lst1),2), {"Style","Occurence"})
in
    Result

Book1
ABCDE
1StyleStyleOccurence
2BB1
3BB2
4CC1
5EE1
6DD1
7CC2
8CC3
9AA1
10BB3
11EE2
12EE3
13EE4
14AA2
15DD2
16AA3
17BB4
18DD3
19AA4
20DD4
21CC4
22
Sheet4
 
Upvote 0
Power Query:
let
    lst = List.Buffer(Excel.CurrentWorkbook(){[Name="Table4"]}[Content][Style]),
    lst1 = List.Accumulate(List.Skip(lst), {{lst{0},1, 2}}, (s,c)=> let ll = List.Last(s){2} in s & {{c, List.Count(List.Select(List.FirstN(lst, ll), each _ = c)), ll +1}}),
    Result = Table.FromColumns(List.FirstN(List.Zip(lst1),2), {"Style","Occurence"})
in
    Result

Book1
ABCDE
1StyleStyleOccurence
2BB1
3BB2
4CC1
5EE1
6DD1
7CC2
8CC3
9AA1
10BB3
11EE2
12EE3
13EE4
14AA2
15DD2
16AA3
17BB4
18DD3
19AA4
20DD4
21CC4
22
Sheet4
@JGordon11 I love your M Code, but it is REALLY obscure. I would love to see your code as above, and also a step by step using the UI as much as possible. Still, the code is amazing!
 
Upvote 0

Forum statistics

Threads
1,223,959
Messages
6,175,644
Members
452,663
Latest member
MEMEH

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