Assign a number of occurrence to each record (sort of Count if)

*爱你&永不变心* 提交于 2019-12-08 11:52:49

问题


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


回答1:


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




回答2:


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"


来源:https://stackoverflow.com/questions/50158654/assign-a-number-of-occurrence-to-each-record-sort-of-count-if

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!