# Tags
**Software** : [[02 - Tags/Software/Power Query]]
**Functions** used : [[Excel.CurrentWorkbook]] [[Table.Sort]] [[List.Generate]] [[List.Count]] [[Table.AddIndexColumn]] [[Table.AddColumn]] [[Table.ReorderColumns]]
**Date** : 29-March-2025
# Description
📊 Welcome to today's tutorial where we dive deep into the world of Power Query to create a Running Occurrence Counter using the List.Generate function! 📊 👨🏫 What Will You Learn? 👨🏫 The basics of the List.Generate function 🌱 How to use List.Generate to create a running occurrence counter 🔢 Best practices for utilizing Power Query for data manipulation 🛠️ 📝 Why List.Generate? 📝 This function is not just a feature; it's a game-changer! List.Generate is a highly versatile tool that can save you time and enhance your data manipulation capabilities. By the end of this tutorial, you'll be able to create a running occurrence counter like a pro! 🥇
# Video Link
<iframe width="560" height="315" src="https://www.youtube.com/embed/2CgKjPc5WYg?si=cp5ozLwKkR2_F9vW" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
[GITHUB File Link](https://github.com/jbotes/powerbiTutorials/blob/main/PQ_OCCURANCE_COUNT_VIDEO.xlsx)
# M-Query
```mquery
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
SortedStocks = Table.Sort(Source,{{"StockCode", Order.Ascending}}),
StockList = SortedStocks[StockCode],
StockListInstance =
List.Generate(
()=> [LoopCounter = 1, InstanceCount = 1],
each _[LoopCounter] <= List.Count(StockList),
each [LoopCounter = _[LoopCounter] + 1, InstanceCount = if StockList{_[LoopCounter]} = StockList{_[LoopCounter] - 1} then _[InstanceCount] + 1 else 1],
each _[InstanceCount]
),
Custom1 = Table.AddIndexColumn(SortedStocks, "Index", 0, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(Custom1, "StockOccuranceCounter", each StockListInstance{[Index]}),
#"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"StockCode", "StockOccuranceCounter", "StockName", "PortfolioName", "Shares", "ReportedPrice", "Value", "Index"})
in
#"Reordered Columns"
```
## Optional Tags
**Tags** :
**Technique** : [[Looping]] [[Working with lists]]