Line Sequence based on Column Value

jarett

Board Regular
Joined
Apr 12, 2021
Messages
165
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
In my data set I have line key/sequence sets but they are sorted and do not represent the data that I export in the way I need. In my final query I need to be able to assign a number starting with 1 to each line of the query, and if a new "Purchase Order Number" value is found in the query, restart the line count. In my example attached the first line would have New Column "Row Number" = 1, then on line 2 (new "PurchaseOrder...#) "Row Number" = 1, line 3 "Row Number= 2, line 4 "Row Number"=3, line 5 "Row Number" = 4, line 6 "Row Number" = 1 because the value in "PurchaseOrder.." started a new value of "0062907".
 

Attachments

  • snippet.png
    snippet.png
    20 KB · Views: 22

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Why would you not use the linekey that's already provided?

You can use the following formula to remove the leading zeros, should that be necessary:
=Val([TableName].[LineKey])
 
Upvote 0
Why would you not use the linekey that's already provided?

You can use the following formula to remove the leading zeros, should that be necessary:
=Val([TableName].[LineKey])
Long story, we could possibly use the "line sequence" column and insert your formula, not sure if the program we are exporting to will be able to accommodate if there are missing values in a sequence. I've played around with the "DCount" function, but when I think I have a good formula and run the query Access just freezes up.
 
Upvote 0
What you could try, is to sort the data by order number, and then add a column with autonumbering. If you are unable to change the table (I am going to guess it is ODBC linked to SAP or another ERP system), then you could copy all data sorted by purchaseorder to another table with a autonumber column.

Create a query that shows the lowest autonumber per purchaseorder. Let's call that field Min_autonumber.

Then in a second query, show all data and add a column that for each purchaseorder deducts the applicable Min_autonumber from the autonumber. That would give you a sequence from 0 to ~. You could add +1 to the formula if you need your sequence to go from 1 to ~.

Again, for this to work, make sure the list is sorted by purchaseorder before adding the autonumbering!
 
Upvote 0

Forum statistics

Threads
1,214,927
Messages
6,122,309
Members
449,080
Latest member
jmsotelo

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