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>
</tbody>


Regards,
Tamir
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
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:
Name
Index
GroupIndex
A
1
A
1
2
B
2
1
A
3
3
B
4
2

<tbody>
</tbody>

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,214,872
Messages
6,122,025
Members
449,060
Latest member
LinusJE

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