Forum Discussion
Create Index Column on Power Query based on 2 or 3 Columns
- 4 years ago
IN Power Query, first sort your data to make sure it's in a format similar to what you've shown.
Then do a 'Group BY' using the key columns (
Bill Customer ID Ship Customer ID Serial Number Bill+Ship+Serial ) - you might get away with the first 3 if it's enough to show correct groups -
with one aggregation on All rows called 'all'.
Then add a custom column with this:
Table.AddIndexColumn([all], "CountInd",1,1)This adds an index per Grouping.
You can then expand column headings to return the appropriate data.
A fair bit to get your head around but that should work.
- 4 years ago
Step 1
In Power Query, do...Group by : Bill+Ship+Serial
New Column Name : Table_1
Operation : All Raws
Step 2
Add Custom Column...
Table_2 = Table.AddIndexColumn([Table_1],"Index",1)
Step 3
Expand Table_2
That's it.
- 4 years ago
Infact, don't need that if data is sorted by Invoice number.
Paste this in advanced editor:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{{"Invoice #", type text}, {"Invoice Date", type datetime}, {"Bill Customer ID", type text}, {"Ship Customer ID", type text}, {"Serial Number", type text}, {"Bill+Ship+Serial", type text}, {"Index", Int64.Type}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"Bill+Ship+Serial"}, {{"Count", each _, type table}}), #"Added Custom" = Table.AddColumn(#"Grouped Rows", "IndexUse", each Table.AddIndexColumn([Count],"IndexNow",1)), #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Count"}), #"Expanded IndexUse" = Table.ExpandTableColumn(#"Removed Columns", "IndexUse", {"Invoice #", "Invoice Date", "Bill Customer ID", "Ship Customer ID", "Serial Number", "Bill+Ship+Serial", "Index", "IndexNow"}, {"Invoice #", "Invoice Date", "Bill Customer ID", "Ship Customer ID", "Serial Number", "Bill+Ship+Serial.1", "Index", "IndexNow"}) in #"Expanded IndexUse"Table1 is your sourcedata table name.
Hope it helps.
Hi hamzashafiq ,
Instead of invoice date, can you use Invoice number in your indexing forumlae. So as 'Bill+Ship+Serial' changes index shall set to 1 and since Invoice number keep incrementing, index has to increment as well.
Assuming you may have no time part in the Invoice date information.
Yeah we can use it if you think it's useful!
- mahenkj24 years agoSolution Sage
Infact, don't need that if data is sorted by Invoice number.
Paste this in advanced editor:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{{"Invoice #", type text}, {"Invoice Date", type datetime}, {"Bill Customer ID", type text}, {"Ship Customer ID", type text}, {"Serial Number", type text}, {"Bill+Ship+Serial", type text}, {"Index", Int64.Type}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"Bill+Ship+Serial"}, {{"Count", each _, type table}}), #"Added Custom" = Table.AddColumn(#"Grouped Rows", "IndexUse", each Table.AddIndexColumn([Count],"IndexNow",1)), #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Count"}), #"Expanded IndexUse" = Table.ExpandTableColumn(#"Removed Columns", "IndexUse", {"Invoice #", "Invoice Date", "Bill Customer ID", "Ship Customer ID", "Serial Number", "Bill+Ship+Serial", "Index", "IndexNow"}, {"Invoice #", "Invoice Date", "Bill Customer ID", "Ship Customer ID", "Serial Number", "Bill+Ship+Serial.1", "Index", "IndexNow"}) in #"Expanded IndexUse"Table1 is your sourcedata table name.
Hope it helps.