How can I achieve the same calculation in Power Query?
In Excel I would use: =COUNTIF($A$2:A2,A2)
Name Occurrence
A 1
A 2
B 1
A 3
B 2
Thanks, Tamir
How can I achieve the same calculation in Power Query?
In Excel I would use: =COUNTIF($A$2:A2,A2)
Name Occurrence
A 1
A 2
B 1
A 3
B 2
Thanks, Tamir
This could be reached via "buttons" only (almost - except some custom column formula):
AddedIndex = Table.AddIndexColumn(Source, "Index", 0, 1),
GroupedRows = Table.Group(AddedIndex, {"Name"}, {{"tmp", each _, type table}}),
AddedCustom = Table.AddColumn(GroupedRows, "Custom", each Table.AddIndexColumn([tmp],"Occurrence", 1,1)),
RemovedOtherColumns = Table.SelectColumns(AddedCustom,{"Custom"}),
Expanded = Table.ExpandTableColumn(RemovedOtherColumns, "Custom", {"Name", "Occurrence"}, {"Name", "Occurrence"})
Or, shorter:
AddedIndex = Table.AddIndexColumn(Source, "Index", 0, 1),
GroupedRows = Table.Group(AddedIndex, {"Name"}, {{"tmp", each Table.AddIndexColumn(_, "Occurence", 1,1), type table}}),
Expanded = Table.ExpandTableColumn(GroupedRows, "tmp", {"Occurrence"}, {"Occurrence"})
There you can also extract Index
column and sort by it, if you need to preserve initial row order. Also all other necessary columns have to be extracted on the last step
Keeping a running count is definitely possible in PQ, though one of those things that is not super simple due to how PQ is designed to look at data. There is probably a more efficient way, but this is what I came up with.
First add an Index column that starts at 1, so we can easily track the "row" we are on. Then add a custom column with this in it
Number.Abs(List.Count(List.RemoveItems(List.Range(#"Added Index"[Name], 0, [Index]), {[Name]}))-List.Count(List.Range(#"Added Index"[Name], 0, [Index])))
I didn't see a simple list function in PQ that counts matching items in a list, so instead we get the count of items in a list by taking the difference in count between a list with the matching items removed and the base count of the list. The index is used to so we can check against a list only up to our current "row" by using List.Range.
The full M code when I pulled in a table of sample data looked like this:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1),
#"Added Custom" = Table.AddColumn(#"Added Index", "Occurence", each Number.Abs(List.Count(List.RemoveItems(List.Range(#"Added Index"[Name], 0, [Index]), {[Name]}))-List.Count(List.Range(#"Added Index"[Name], 0, [Index]))))
in
#"Added Custom"