Forum

Adding counter if l...
 
Notifications
Clear all

Adding counter if lines have same values

7 Posts
3 Users
0 Reactions
200 Views
(@barim)
Posts: 5
Active Member
Topic starter
 

I would like to add column that would be kind of counter for the values that are repeating. For example, if number 1000 repeats 3 times in the column, the first instance of 1000 would be 1, second instance of 1000 would be 2 and third instance of 1000 would be 3, and so on. If there are 5 lines of number, let's say, 2000 it should go from 1 to 5. Is it possible to do this with power query or I would have to use excel formulas or vba. I prefer to use power query.

I appreciate help. Thank you in advance.

 
Posted : 17/11/2021 4:04 pm
Alan Sidman
(@alansidman)
Posts: 222
Member Moderator
 

Group your data and keep all rows.  Call the new grouped column "Data"

Add a column

=Table.AddIndexColumn([Data],"Index",1,1)

Delete the grouped column

Expand the new column.  It will number the rows as you have requested.

 
Posted : 17/11/2021 11:30 pm
(@barim)
Posts: 5
Active Member
Topic starter
 

Thanks Alan so much for your answer. I encountered the following error message. 

Expression.Error: We cannot convert the value "0000001" to type Table.
Details:
Value=0000001
Type=[Type]

 
Posted : 19/11/2021 3:33 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Amir,

You haven't provided a sample of the data, or your query, so it's nearly impossible to tell you what is going wrong.

If you look at the attached file, I've written a query that does what you want.  Here's the M code

 

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Index" = Table.AddIndexColumn(Source, "TempIndex", 0, 1, Int64.Type),
#"Grouped Rows" = Table.Group(#"Added Index", {"Values"}, {{"Data", each _, type table [Values=number]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Data],"Index",1,1)),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Values", "Data"}),
#"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Values", "TempIndex", "Index"}, {"Values", "TempIndex", "Index"}),
#"Sorted Rows" = Table.Sort(#"Expanded Custom",{{"TempIndex", Order.Ascending}}),
#"Removed Columns1" = Table.RemoveColumns(#"Sorted Rows",{"TempIndex"})
in
#"Removed Columns1"

 

index data

  

Regards

Phil

 
Posted : 23/11/2021 9:48 pm
(@barim)
Posts: 5
Active Member
Topic starter
 

Thanks Philip. I think my  source data was messed up. Is this possible to do with Excel formula?

 
Posted : 01/12/2021 10:02 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Amir,

You might be able to do this with formulae, I hadn't thought about it as you asked for a solution with Power Query?

Regards

Phil

 
Posted : 01/12/2021 7:26 pm
(@barim)
Posts: 5
Active Member
Topic starter
 

I can count this as resolved. Thanks Philip so much for posting your workbook. After I followed your steps I found where my mistake was. Thanks also to Alan who posted his answer as well. 

 
Posted : 03/12/2021 4:00 pm
Share: