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

TamirBasin

New Member
Joined
Apr 11, 2017
Messages
49
Office Version
  1. 365
Platform
  1. Windows
Hi,
How can I achieve the same calculation in Power Query?
In Excel I would use: =COUNTIF($A$2:A2,A2)

NameOccurrence
A1
A2
B1
A3
B2

<tbody>
[TD="colspan: 2"][/TD]

</tbody>


Regards,
Tamir
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Here's what I did:
• I made your sample data a table named Table1
• Added that table to Power Query (as a query named SourceData)
• Created this query to assign the clustered indexes:
Code:
let
    Source = SourceData,
    InsertedIndex = Table.AddIndexColumn(Source,"Index"),
    AddGroupingTableToEachRows = Table.Group(InsertedIndex, {"Name"}, {{"Values", each _, type table}}),
    AddIndexToEachGroupedTable = Table.TransformColumns(AddGroupingTableToEachRows,{{"Values", each Table.AddIndexColumn(Table.Sort(_, "Index"),"GroupIndex",1)}}),
    ExposeCols = Table.ExpandTableColumn(AddIndexToEachGroupedTable, "Values", {"Index", "GroupIndex"}, {"Index", "GroupIndex"}),
    MergeIndexedDataWithGroupedIndex = Table.NestedJoin(InsertedIndex,{"Index"},ExposeCols,{"Index"},"NewColumn",JoinKind.LeftOuter),
    ExposeGroupIndexCol = Table.ExpandTableColumn(MergeIndexedDataWithGroupedIndex, "NewColumn", {"GroupIndex"}, {"GroupIndex"}),
    SortedIntoOrigOrder = Table.Sort(ExposeGroupIndexCol,{{"Index", Order.Ascending}})
in
    SortedIntoOrigOrder
• These are the results:
[TABLE="width: 190"]
<tbody>[TR]
[TD]Name
[/TD]
[TD]Index
[/TD]
[TD]GroupIndex
[/TD]
[/TR]
[TR]
[TD]A
[/TD]
[TD="align: right"][/TD]
[TD="align: right"]1
[/TD]
[/TR]
[TR]
[TD]A
[/TD]
[TD="align: right"]1
[/TD]
[TD="align: right"]2
[/TD]
[/TR]
[TR]
[TD]B
[/TD]
[TD="align: right"]2
[/TD]
[TD="align: right"]1
[/TD]
[/TR]
[TR]
[TD]A
[/TD]
[TD="align: right"]3
[/TD]
[TD="align: right"]3
[/TD]
[/TR]
[TR]
[TD]B
[/TD]
[TD="align: right"]4
[/TD]
[TD="align: right"]2
[/TD]
[/TR]
</tbody>[/TABLE]

Is that something you can work with?
 
Upvote 0
Thanks Ron,

The code seems to be exactly what I need.

I will try and work it out.

Could you provide some more explanations?

Regards,
Tamir


 
Upvote 0
This one:

Code:
AddIndexToEachGroupedTable = Table.TransformColumns(AddGroupingTableToEachRows,{{"Values", each Table.AddIndexColumn(Table.Sort(_, "Index"),"GroupIndex",1)}}),
 
Upvote 0
It might be easier if you look at what's happening via the edit window.
At the AddGroupingTableToEachRows step....click on one of the "Table" cells. You'll see that it contains all of the records for the specific Name...and it's position in the original listing.
Next, click on the AddIndexToEachGroupedTable step and click on the same "Table" cell. It now contains the Name, original position...and an incremental ordinal! That's the value we really want.

In the following steps we reference the InsertedIndex data and merged our new seq num table and sort the data into its original order.
Does that help?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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