Forum Discussion
Duplicate Invoice No and Use Data from 2nd record
- 1 year ago
Here is another pair of code blocks that might work on what you have:
to Copy
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{ {"Processing Began", type date}, {"Vendor", type text}, {"WDDate", type date}, {"Invoice #", type text}, {"Invoice No Stripped", type text}, {"Invoice amount", type number}, {"Currency", type text}, {"Invoice date", type date}, {"Invoice due date", type date}, {"Company Code", type text}, {"Budget Category", type text}, {"Invoice Status", type text}, {"Goods Receipt", Int64.Type}, {"Project ", type text}, {"Source", type text}, {"Funding Date", type any}, {"DDNo", type any}, {"USD Amount", type any}, {"Account", type any}, {"TypeDraw", type any}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"Invoice No Stripped", "Invoice amount"}, { {"Count", (t)=>Table.ReplaceValue( t, List.RemoveNulls(List.Distinct(t[Goods Receipt])){0}?, null, (x,y,z)=>y, {"Goods Receipt"} ), type table [Processing Began=nullable date, Vendor=nullable text, WDDate=nullable date, #"Invoice #"=nullable text, Invoice No Stripped=nullable text, Invoice amount=nullable number, Currency=nullable text, Invoice date=nullable date, Invoice due date=nullable date, Company Code=nullable text, Budget Category=nullable text, Invoice Status=nullable text, Goods Receipt=nullable number, #"Project "=nullable text, Source=nullable text, Funding Date=nullable text, DDNo=nullable text, USD Amount=nullable text, Account=nullable text, TypeDraw=nullable text]}}), #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Invoice No Stripped", "Invoice amount"}), #"Expanded Count" = Table.ExpandTableColumn(#"Removed Columns", "Count", {"Processing Began", "Vendor", "WDDate", "Invoice #", "Invoice No Stripped", "Invoice amount", "Currency", "Invoice date", "Invoice due date", "Company Code", "Budget Category", "Invoice Status", "Goods Receipt", "Project ", "Source", "Funding Date", "DDNo", "USD Amount", "Account", "TypeDraw"}) in #"Expanded Count"to Move to 1-PC
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{ {"Processing Began", type date}, {"Vendor", type text}, {"WDDate", type date}, {"Invoice #", type text}, {"Invoice No Stripped", type text}, {"Invoice amount", type number}, {"Currency", type text}, {"Invoice date", type date}, {"Invoice due date", type date}, {"Company Code", type text}, {"Budget Category", type text}, {"Invoice Status", type text}, {"Goods Receipt", Int64.Type}, {"Project ", type text}, {"Source", type text}, {"Funding Date", type any}, {"DDNo", type any}, {"USD Amount", type any}, {"Account", type any}, {"TypeDraw", type any}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"Invoice No Stripped", "Invoice amount"}, { {"Count", (t)=>Table.ReplaceValue( t, each [Source], List.RemoveNulls(List.Distinct(t[Goods Receipt])){0}?, (x,y,z)=> if y = "1-PC" then z else null, {"Goods Receipt"} ), type table [Processing Began=nullable date, Vendor=nullable text, WDDate=nullable date, #"Invoice #"=nullable text, Invoice No Stripped=nullable text, Invoice amount=nullable number, Currency=nullable text, Invoice date=nullable date, Invoice due date=nullable date, Company Code=nullable text, Budget Category=nullable text, Invoice Status=nullable text, Goods Receipt=nullable number, #"Project "=nullable text, Source=nullable text, Funding Date=nullable text, DDNo=nullable text, USD Amount=nullable text, Account=nullable text, TypeDraw=nullable text]}}), #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Invoice No Stripped", "Invoice amount"}), #"Expanded Count" = Table.ExpandTableColumn(#"Removed Columns", "Count", {"Processing Began", "Vendor", "WDDate", "Invoice #", "Invoice No Stripped", "Invoice amount", "Currency", "Invoice date", "Invoice due date", "Company Code", "Budget Category", "Invoice Status", "Goods Receipt", "Project ", "Source", "Funding Date", "DDNo", "USD Amount", "Account", "TypeDraw"}) in #"Expanded Count"Again both of the above work without a problem on the sample data you provided.
wow Rohit, that is amazing. I am sorry to say that it looks like I have an error in my first line of code ("RemovedDuplicates") after your last line ("RemoveHelper"). I did refer to the previous step ("RemoveHelper") however that did not fix it as there is still a "Token expected" error and higlights my first line of code ("RemovedDuplicates") after the ("RemoveHelper") line.
Please see my entire code with your code.
I am showing a blank row to better offset your code (starting with "GroupedRows" and ending on "RemoveHelper".
Do you happen to see the reason I have the expected token error on the "RemovedDuplicates" row?
NOTE: I confirm if I remove all the code after your last line the token error is removed and no syntax errors either (and also changed the "In" line to refer to #RemoveHelper#) however I do have an error on the Expanded step once I step out of the Advanced Editor:
let
Source = Table.Combine({StampliALLPaid, #"Project Costs"}),
#"Uppercased Text" = Table.TransformColumns(Source,{{"Invoice No Stripped", Text.Upper, type text}}),
#"Reordered Columns3" = Table.ReorderColumns(#"Uppercased Text",{"Processing Began", "Vendor", "WDDate", "Invoice #", "Invoice No Stripped", "Source", "Invoice amount", "Currency", "Invoice date", "Invoice due date", "Company Code", "Budget Category", "Invoice Status", "Goods Receipt", "Project ", "Funding Date", "DDNo", "USD Amount", "Account", "TypeDraw"}),
#"Sorted Rows" = Table.Sort(#"Reordered Columns3",{{"Invoice No Stripped", Order.Ascending}, {"Source", Order.Ascending}}),
GroupedRows = Table.Group(#"Sorted Rows", {"Invoice No Stripped", "Invoice amount"}, {{"AllData", each _, type table [Invoice No Stripped=nullable text, Source=nullable text, Invoice amount=nullable number, Goods Receipt=nullable text]}}),
AddGRColumn = Table.AddColumn(GroupedRows, "GoodsReceiptToKeep", each
let
groupTable = [AllData],
grValue = try Record.Field(Table.SelectRows(groupTable, each Text.Contains([Source], "Stampli")){0}, "Goods Receipt") otherwise null
in grValue),
Expanded = Table.ExpandTableColumn(AddGRColumn, "AllData", {"Invoice No Stripped", "Source", "Invoice amount", "Goods Receipt"}, {"Invoice No Stripped", "Source", "Invoice amount", "Goods Receipt"}),
UpdatedGR = Table.AddColumn(Expanded, "Final Goods Receipt", each if Text.Contains([Source], "1-PC") then [GoodsReceiptToKeep] else [Goods Receipt]),
RemoveHelper = Table.RemoveColumns(UpdatedGR, {"GoodsReceiptToKeep", "Goods Receipt"})
#"Removed Duplicates" = Table.Distinct(#"RemoveHelper", {"Invoice No Stripped", "Invoice amount"}),
#"Add Column" = Table.AddColumn(#"Removed Duplicates", "Custom", each if [WDDate] = null then [Invoice due date] else [WDDate]),
#"Reordered Columns" = Table.ReorderColumns(#"Add Column",{"Processing Began", "Vendor", "WDDate", "Custom", "Invoice #", "Invoice No Stripped", "Invoice amount", "Currency", "Invoice date", "Invoice due date", "Company Code", "Budget Category", "Invoice Status", "Goods Receipt", "Project ", "Funding Date", "DDNo", "USD Amount", "Account", "TypeDraw"}),
Custom1 = Table.AddColumn(#"Reordered Columns", "Custom2", each if [Custom] = null then [Funding Date] else [Custom]),
#"Reordered Columns1" = Table.ReorderColumns(Custom1,{"Processing Began", "Vendor", "WDDate", "Custom2", "Custom", "Invoice #", "Invoice No Stripped", "Invoice amount", "Currency", "Invoice date", "Invoice due date", "Company Code", "Budget Category", "Invoice Status", "Goods Receipt", "Project ", "Funding Date", "DDNo", "USD Amount", "Account", "TypeDraw"}),
#"Removed Columns" = Table.RemoveColumns(#"Reordered Columns1",{"WDDate", "Custom"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom2", "WDDate"}}),
#"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"WDDate", type date}}),
#"Reordered Columns2" = Table.ReorderColumns(#"Changed Type",{"Vendor", "WDDate", "Invoice #", "Invoice No Stripped", "Invoice amount", "Currency", "Invoice date", "Invoice due date", "Company Code", "Budget Category", "Invoice Status", "Goods Receipt", "Project ", "Funding Date", "DDNo", "USD Amount", "Account", "TypeDraw", "Processing Began"}),
#"Renamed Columns1" = Table.RenameColumns(#"Reordered Columns2",{{"WDDate", "WDDate_UseThis"}})
in
#"Renamed Columns1"
thank you very much!
Greatly appreciate your expert advice.
Hi Centaur ,
You're very close! The issue you're running into—"Token expected"—is due to a missing comma at the end of the previous step. In Power Query M code, each step must be separated by a comma, except for the final one before the in keyword.
In your code, you ended the RemoveHelper step like this:
RemoveHelper = Table.RemoveColumns(UpdatedGR, {"GoodsReceiptToKeep", "Goods Receipt"})
But then immediately started the next step with:
#"Removed Duplicates" = Table.Distinct(#"RemoveHelper", {"Invoice No Stripped", "Invoice amount"}),
Since "Removed Duplicates" is technically the next step after RemoveHelper, you need to put a comma at the end of the RemoveHelper line to separate the steps properly. Here’s the corrected version of that section:
RemoveHelper = Table.RemoveColumns(UpdatedGR, {"GoodsReceiptToKeep", "Goods Receipt"}),
#"Removed Duplicates" = Table.Distinct(RemoveHelper, {"Invoice No Stripped", "Invoice amount"}),
Also, you don't need the #"" syntax for RemoveHelper unless it includes spaces or special characters—RemoveHelper works fine as a reference on its own, and using #"RemoveHelper" can sometimes cause confusion if it was never defined with quotes.
Here is what i believe would solve your issue:
let
Source = Table.Combine({StampliALLPaid, #"Project Costs"}),
#"Uppercased Text" = Table.TransformColumns(Source,{{"Invoice No Stripped", Text.Upper, type text}}),
#"Reordered Columns3" = Table.ReorderColumns(#"Uppercased Text",{
"Processing Began", "Vendor", "WDDate", "Invoice #", "Invoice No Stripped", "Source", "Invoice amount",
"Currency", "Invoice date", "Invoice due date", "Company Code", "Budget Category", "Invoice Status",
"Goods Receipt", "Project ", "Funding Date", "DDNo", "USD Amount", "Account", "TypeDraw"}),
#"Sorted Rows" = Table.Sort(#"Reordered Columns3",{
{"Invoice No Stripped", Order.Ascending},
{"Source", Order.Ascending}}),
GroupedRows = Table.Group(#"Sorted Rows", {"Invoice No Stripped", "Invoice amount"}, {
{"AllData", each _, type table [
Invoice No Stripped=nullable text,
Source=nullable text,
Invoice amount=nullable number,
Goods Receipt=nullable text,
Processing Began=nullable datetime, Vendor=nullable text, WDDate=nullable date,
Invoice #=nullable text, Currency=nullable text, Invoice date=nullable date,
Invoice due date=nullable date, Company Code=nullable text, Budget Category=nullable text,
Invoice Status=nullable text, Project = nullable text, Funding Date=nullable date,
DDNo=nullable text, USD Amount=nullable number, Account=nullable text, TypeDraw=nullable text
]}
}),
AddGRColumn = Table.AddColumn(GroupedRows, "GoodsReceiptToKeep", each
let
groupTable = [AllData],
grValue = try Record.Field(
Table.SelectRows(groupTable, each Text.Contains([Source], "Stampli")){0},
"Goods Receipt"
) otherwise null
in grValue
),
Expanded = Table.ExpandTableColumn(AddGRColumn, "AllData", {
"Processing Began", "Vendor", "WDDate", "Invoice #", "Invoice No Stripped", "Source",
"Invoice amount", "Currency", "Invoice date", "Invoice due date", "Company Code",
"Budget Category", "Invoice Status", "Goods Receipt", "Project ", "Funding Date", "DDNo",
"USD Amount", "Account", "TypeDraw"
}),
UpdatedGR = Table.AddColumn(Expanded, "Final Goods Receipt", each
if Text.Contains([Source], "1-PC") then [GoodsReceiptToKeep] else [Goods Receipt]
),
RemoveHelper = Table.RemoveColumns(UpdatedGR, {"GoodsReceiptToKeep", "Goods Receipt"}),
#"Removed Duplicates" = Table.Distinct(RemoveHelper, {"Invoice No Stripped", "Invoice amount"}),
#"Add Column" = Table.AddColumn(#"Removed Duplicates", "Custom", each
if [WDDate] = null then [Invoice due date] else [WDDate]
),
#"Reordered Columns" = Table.ReorderColumns(#"Add Column",{
"Processing Began", "Vendor", "WDDate", "Custom", "Invoice #", "Invoice No Stripped",
"Invoice amount", "Currency", "Invoice date", "Invoice due date", "Company Code",
"Budget Category", "Invoice Status", "Final Goods Receipt", "Project ", "Funding Date",
"DDNo", "USD Amount", "Account", "TypeDraw"
}),
Custom1 = Table.AddColumn(#"Reordered Columns", "Custom2", each
if [Custom] = null then [Funding Date] else [Custom]
),
#"Reordered Columns1" = Table.ReorderColumns(Custom1,{
"Processing Began", "Vendor", "WDDate", "Custom2", "Custom", "Invoice #",
"Invoice No Stripped", "Invoice amount", "Currency", "Invoice date", "Invoice due date",
"Company Code", "Budget Category", "Invoice Status", "Final Goods Receipt", "Project ",
"Funding Date", "DDNo", "USD Amount", "Account", "TypeDraw"
}),
#"Removed Columns" = Table.RemoveColumns(#"Reordered Columns1",{"WDDate", "Custom"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom2", "WDDate"}}),
#"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"WDDate", type date}}),
#"Reordered Columns2" = Table.ReorderColumns(#"Changed Type",{
"Vendor", "WDDate", "Invoice #", "Invoice No Stripped", "Invoice amount", "Currency",
"Invoice date", "Invoice due date", "Company Code", "Budget Category", "Invoice Status",
"Final Goods Receipt", "Project ", "Funding Date", "DDNo", "USD Amount", "Account",
"TypeDraw", "Processing Began"
}),
#"Renamed Columns1" = Table.RenameColumns(#"Reordered Columns2",{{"WDDate", "WDDate_UseThis"}})
in
#"Renamed Columns1"
- Centaur1 year agoHelper V
HI Rohit, thank you very much for the response. Ok I see where you pointed out I had missed a comma. Argh!
I have pasted your revised code "as is".
It does look like I have an error here:
its on the following line:
Invoice #=nullable text, Currency=nullable text, Invoice date=nullable date,
I thought that the error might be because I do not have a field named "Invoice" but I dont think that is the reason (I changed it to one of the fields to no avail) . I assume that line is similar to a "like" invoice.
Do you happen to see where the issue is?
Once again, I thank you very much for your help.
GroupedRows = Table.Group(#"Sorted Rows", {"Invoice No Stripped", "Invoice amount"}, { {"AllData", each _, type table [ Invoice No Stripped=nullable text, Source=nullable text, Invoice amount=nullable number, Goods Receipt=nullable text, Processing Began=nullable datetime, Vendor=nullable text, WDDate=nullable date, Invoice #=nullable text, Currency=nullable text, Invoice date=nullable date, Invoice due date=nullable date, Company Code=nullable text, Budget Category=nullable text, Invoice Status=nullable text, Project = nullable text, Funding Date=nullable date, DDNo=nullable text, USD Amount=nullable number, Account=nullable text, TypeDraw=nullable text ]} }),