Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Learner1585
Helper I
Helper I

undefinedCrew another custom column to count how many times each row value occurs in power query

Hi,

 

I want to create a custom column to count how many times a specic row value occurs in a column.

I dont want to create a calculated column but a custom colum as a transformation step in power query only.

Below is the example of what i am looking for.

  1.  There is a column name UID
  2. I want to create another custom column where each row shows how many times the adjacent value occurs.

Learner1585_0-1697816730090.png

 

Below is how I would do this in excel.

 

Learner1585_1-1697816743183.png

 

thank you very much

1 ACCEPTED SOLUTION

Hi,
Thank you very much for your kind reply.

Pardon me for my follow up, but being not so pro , can I seek your help again on how to add your codes into my existing codes.

Learner1585_0-1697881049070.png

Should I just add your code after my last step " 

    #"Added Index" = Table.AddIndexColumn(#"Merged Columns1", "Index", 1, 1, Int64.Type)

 

  • PreviousStepName = <Your existing code here>,
    BufferedList = List.Buffer(PreviousStepName[4sector UID]),
    AddColumn = Table.AddColumn(PreviousStepName, "Count", each List.Count(List.Select(BufferedList, (l)=> l=[4sector UID])))

Thanks again

View solution in original post

4 REPLIES 4
ImkeF
Community Champion
Community Champion

Yes, exactly.
So the end of your code would look like so:


  #"Added Index" = Table.AddIndexColumn(#"Merged Columns1", "Index", 1, 1, Int64.Type),
BufferedList = List.Buffer(  #"Added Index" [4sector UID]),
AddColumn = Table.AddColumn(  #"Added Index" , "Count", each List.Count(List.Select(BufferedList, (l)=> l=[4sector UID])))
in
AddColumn

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

thank you @ImkeF 

ImkeF
Community Champion
Community Champion

Hi @Learner1585 ,
you can do it like so:

PreviousStepName = <Your existing code here>,
BufferedList = List.Buffer(PreviousStepName[4sector UID]),
AddColumn = Table.AddColumn(PreviousStepName, "Count", each List.Count(List.Select(BufferedList, (l)=> l=[4sector UID])))

For performance reasons, you are adding an intermediate step where you buffer the column that you want to search through in each row.
Then you add the column where you count the values that match the current row.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi,
Thank you very much for your kind reply.

Pardon me for my follow up, but being not so pro , can I seek your help again on how to add your codes into my existing codes.

Learner1585_0-1697881049070.png

Should I just add your code after my last step " 

    #"Added Index" = Table.AddIndexColumn(#"Merged Columns1", "Index", 1, 1, Int64.Type)

 

  • PreviousStepName = <Your existing code here>,
    BufferedList = List.Buffer(PreviousStepName[4sector UID]),
    AddColumn = Table.AddColumn(PreviousStepName, "Count", each List.Count(List.Select(BufferedList, (l)=> l=[4sector UID])))

Thanks again

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.