• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Adding counter if lines have same values|Power Query|Excel Forum|My Online Training Hub

You are here: Home / Adding counter if lines have same values|Power Query|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumPower QueryAdding counter if lines have same v…
sp_PrintTopic sp_TopicIcon
Adding counter if lines have same values
Avatar
Amir Begovic

Active Member
Members
Level 0
Forum Posts: 5
Member Since:
November 17, 2021
sp_UserOfflineSmall Offline
1
November 17, 2021 - 6:04 am
sp_Permalink sp_Print

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.

sp_AnswersTopicSeeAnswer See Answer
Avatar
Alan Sidman
Steamboat Springs, CO
Member
Members


Trusted Members
Level 0
Forum Posts: 132
Member Since:
October 18, 2018
sp_UserOfflineSmall Offline
2
November 17, 2021 - 1:30 pm
sp_Permalink sp_Print

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.

Avatar
Amir Begovic

Active Member
Members
Level 0
Forum Posts: 5
Member Since:
November 17, 2021
sp_UserOfflineSmall Offline
3
November 19, 2021 - 5:33 am
sp_Permalink sp_Print

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]

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1518
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
4
November 23, 2021 - 11:48 am
sp_Permalink sp_Print sp_EditHistory

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 dataImage Enlarger

  

Regards

Phil

sp_AnswersTopicAnswer
Answers Post
Avatar
Amir Begovic

Active Member
Members
Level 0
Forum Posts: 5
Member Since:
November 17, 2021
sp_UserOfflineSmall Offline
5
December 1, 2021 - 12:02 am
sp_Permalink sp_Print

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

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1518
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
6
December 1, 2021 - 9:26 am
sp_Permalink sp_Print

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

Avatar
Amir Begovic

Active Member
Members
Level 0
Forum Posts: 5
Member Since:
November 17, 2021
sp_UserOfflineSmall Offline
7
December 3, 2021 - 6:00 am
sp_Permalink sp_Print

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. 

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Brian Pham, Riny van Eekelen
Guest(s) 9
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 871
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
Jessica Stewart: 205
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
stuart burge
Bruce Tang Nian
Scot C
Othman AL MUTAIRI
Misael Gutierrez Sr.
Attif Ihsan
Kieran Fee
Murat Hasanoglu
Brett Dryland
Saeed Aldousari
Forum Stats:
Groups: 3
Forums: 24
Topics: 6223
Posts: 27295

 

Member Stats:
Guest Posters: 49
Members: 31920
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.