Forum Discussion
Replace Values in Columns that Change
- 2 years ago
Please consider what is the expected result. Based on the script, it looks like your intent is to check to see if the file name includes the date for last week, and if so, replace the Source.Name value with only the formatted version of the date (MMddyy). If that really is your intent, then there are other ways of going this transformation that are more straight-forward. If that is not your intent, then I think the exlaination below will provide you with the insight needed to customize the script provided to meet your actual needs.
Your script says...
Table.ReplaceValue(#"Changed Type","inventory_"& LW &".csv","LW",Replacer.ReplaceValue,{"Source.Name"})If LW = "052424", then your script translates to:
- For every value in the Source.Name column, replace values equal to "inventory_052424.csv" with "LW" (literally the text LW, not the value of the LW variable).
- Replacer.ReplaceValue means that the replacement should only be made if the entire cell value is match.
If the intent is to replace instances of "inventory_052424.csv" with the value of the variable LW, then your script will need to be revised to what is shown below, however, that does mean that if the replacement is made and if there are other values in rows, then your column values will be inconsistent with each other. Some will have a file name convention (like including the .csv) and others will just have the formatted date portion.
Table.ReplaceValue(Source, each [Source.Name], each if [Source.Name] = "inventory_" & LW & ".csv" then LW else [Source.Name], Replacer.ReplaceValue,{"Source.Name"})When each row in a table needs to be evaluated and then conditionally modified, the 'each' keyword must be present to make that happen (in this circumstance). The Table.ReplaceValue function does not include a row-by-row evaluation by default; for efficiency, it evaluates a list of old values against a list of new values.
Syntax:
Table.ReplaceValue(table as table, oldValue as any, newValue as any, replacer as function, columnsToSearch as list) as tableIf you still need more assistance, please provide an explaination or example of the expected value. Thanks!
Please consider what is the expected result. Based on the script, it looks like your intent is to check to see if the file name includes the date for last week, and if so, replace the Source.Name value with only the formatted version of the date (MMddyy). If that really is your intent, then there are other ways of going this transformation that are more straight-forward. If that is not your intent, then I think the exlaination below will provide you with the insight needed to customize the script provided to meet your actual needs.
Your script says...
Table.ReplaceValue(#"Changed Type","inventory_"& LW &".csv","LW",Replacer.ReplaceValue,{"Source.Name"})
If LW = "052424", then your script translates to:
- For every value in the Source.Name column, replace values equal to "inventory_052424.csv" with "LW" (literally the text LW, not the value of the LW variable).
- Replacer.ReplaceValue means that the replacement should only be made if the entire cell value is match.
If the intent is to replace instances of "inventory_052424.csv" with the value of the variable LW, then your script will need to be revised to what is shown below, however, that does mean that if the replacement is made and if there are other values in rows, then your column values will be inconsistent with each other. Some will have a file name convention (like including the .csv) and others will just have the formatted date portion.
Table.ReplaceValue(Source, each [Source.Name], each if [Source.Name] = "inventory_" & LW & ".csv" then LW else [Source.Name], Replacer.ReplaceValue,{"Source.Name"})
When each row in a table needs to be evaluated and then conditionally modified, the 'each' keyword must be present to make that happen (in this circumstance). The Table.ReplaceValue function does not include a row-by-row evaluation by default; for efficiency, it evaluates a list of old values against a list of new values.
Syntax:
Table.ReplaceValue(table as table, oldValue as any, newValue as any, replacer as function, columnsToSearch as list) as table
If you still need more assistance, please provide an explaination or example of the expected value. Thanks!
Yes, thank you! I got it working is the first code you provided.