Forum Discussion
Combining two table with different columns name
- 4 years ago
I'll walk your through it step-by-step to see what's happening at each step.
To demonstrate how we are preping the replacement values for use in List.ReplacementMatchingItems:
- Create a new blank query: = tblReplacement[Sipee]
- Rename the query "Sipee"
- Do the same for tblReplacement[Siebel]
- What you you should have are two queries containing a list of values for each respective column in your replacement table
- Create another blank query: = List.Zip( { Sipee, Siebel} )
- This query contains a list of lists of old/new pairs, for example {Column1_Old, Column1_New}
- To check, click in the gray area next any "List" row (clicking of the "List word will expand that list) to see which values are in that list.
- This query contains a list of lists of old/new pairs, for example {Column1_Old, Column1_New}
- *List.Buffer() simply puts this list of lists in memory, so that the matching step is faster
- So ultimately, we are creating a list of each row-value pair for Sipee and Siebel
Now for what List.ReplaceMatchingItems is doing:
- Table.TransformColumnNames(#"En-tetes promus", each...
- Translation: "For each column name in the table", represented further by {_}
- List.ReplaceMatchingItems(list as list, replacements as list, optional equationCriteria as any) as list
- Lists as list: for this parameter we use is {_}, which represents each column name
- Replacements as list: using our replacement table, essentially what this does is if any of the column names matches any the first (old/sipee) items in the old/new pair, replace it with the second (new/siebel) item for that match
- equationCriteria is Comparer.OrdinalIgnoreCase, which just says don't mind mismatched cases
- Finally, we wrap the list of replaced values in Text.Combine which converts each list item back to a text value.
As for why it isn't working, it's hard to say by looking at the error you received and without having your file. But try those test queries above to get a feel for it and see if it makes more sense.
- Create a new blank query: = tblReplacement[Sipee]
- 4 years ago
Nice work! You're very close on the last past, it's just a matter of adjusting the closing brackets:
#"Replace Field Value in column" = Table.TransformColumns(#"Replace Cols Names",{ "Column_B", each Text.Combine(List.ReplaceMatchingItems({_},List.Buffer(List.Zip({tblBeforeAfter[Before],tblBeforeAfter[After]})))), type text } ,null,MissingField.Ignore)I recreated your Excel data and walked through it step-by-step to make sure it worked. You'll notice that I added "type text" before the "}" to skip having to change the type in another step.
Something I would caution is to have one replacement table for column names and another for column values - for example, if tblBeforeAfter has "" (blank) instead of null for [Before] with [After] = Col_H1, it will swap any blank cell with "Col_H1". You can easily do this by duplicating tblBeforeAfter and filtering accordingly.
If you have a replacement table with Old/New columns and both it and the two tables of interest are loaded to to Power Query, the following should work:
- As an example, let's say your query holding the replacement table is called "Replacement" and it has two columns, "Old" and "New" -
- Create a new step in whichever table that you want to change the columns names and enter the following code:
- #"Replace Cols Names" = Table.TransformColumnNames(#"Previous Step", each Text.Combine(List.ReplaceMatchingItems({_},List.Buffer(List.Zip( { Old,New } )), Comparer.OrdinalIgnoreCase)))
- Then you should be able to use Table.Combine to create a single 59 column table, with rows from the 51 table simply being null in the additional 8 columns.
supremo_pdx , I get the error "Old" (in my case Sipee) is not recognize.
Where do I make reference of the replacement table name (Replacement) ???
All these functions are new to me
PS: my Power Editor is in French
let
Source = Excel.CurrentWorkbook(){[Name="tblProjetSipee"]}[Content],
#"Type modifié" = Table.TransformColumnTypes(Source,{{"NUMÉRO_DU_SYSTÈME_SOURCE", Int64.Type}, {"PARTENAIRE", Int64.Type}, {"RESPONSABLE_TECHNIQUE", type text}}),
//#"Replace Cols Names" = Table.TransformColumnNames(#"Previous Step", each Text.Combine(List.ReplaceMatchingItems({_},List.Buffer(List.Zip( { Old,New } )), Comparer.OrdinalIgnoreCase)))
#"Replace Cols Names" = Table.TransformColumnNames(#"Type modifié", each Text.Combine(List.ReplaceMatchingItems({_},List.Buffer(List.Zip( { Sipee,Siebel } )), Comparer.OrdinalIgnoreCase)))
in
//#"Type modifié"
#"Replace Cols Names"
//Syntax: List.ReplaceMatchingItems(list as list, replacements as list, optional equationCriteria as any) as list
- supremo_pdx4 years agoFrequent Visitor
Ah that was my mistake, I had already assigned the column lists to a variable in my editor. In general, you can access the list of values in a table column like so: Table[Column] = list of values in that column. Therefore:
let
Source = Excel.CurrentWorkbook(){[Name="tblProjetSipee"]}[Content],
#"Type modifié" = Table.TransformColumnTypes(Source,{{"NUMÉRO_DU_SYSTÈME_SOURCE", Int64.Type}, {"PARTENAIRE", Int64.Type}, {"RESPONSABLE_TECHNIQUE", type text}}),
#"Replace Cols Names" = Table.TransformColumnNames(#"Type modifié", each Text.Combine(List.ReplaceMatchingItems({_},List.Buffer(List.Zip( { Replacement[Sipee],Replacement[Siebel] } )), Comparer.OrdinalIgnoreCase)))
in
#"Replace Cols Names"- DanFromMontreal4 years agoHelper IV
Nope, still get a huge error message.
I tried to understand each step of the formula.
I do not get what the underscore does in ...List.ReplaceMatchingItems({_}...
Isn't the {_} suppose to be my Header columns?
- supremo_pdx4 years agoFrequent Visitor
I'll walk your through it step-by-step to see what's happening at each step.
To demonstrate how we are preping the replacement values for use in List.ReplacementMatchingItems:
- Create a new blank query: = tblReplacement[Sipee]
- Rename the query "Sipee"
- Do the same for tblReplacement[Siebel]
- What you you should have are two queries containing a list of values for each respective column in your replacement table
- Create another blank query: = List.Zip( { Sipee, Siebel} )
- This query contains a list of lists of old/new pairs, for example {Column1_Old, Column1_New}
- To check, click in the gray area next any "List" row (clicking of the "List word will expand that list) to see which values are in that list.
- This query contains a list of lists of old/new pairs, for example {Column1_Old, Column1_New}
- *List.Buffer() simply puts this list of lists in memory, so that the matching step is faster
- So ultimately, we are creating a list of each row-value pair for Sipee and Siebel
Now for what List.ReplaceMatchingItems is doing:
- Table.TransformColumnNames(#"En-tetes promus", each...
- Translation: "For each column name in the table", represented further by {_}
- List.ReplaceMatchingItems(list as list, replacements as list, optional equationCriteria as any) as list
- Lists as list: for this parameter we use is {_}, which represents each column name
- Replacements as list: using our replacement table, essentially what this does is if any of the column names matches any the first (old/sipee) items in the old/new pair, replace it with the second (new/siebel) item for that match
- equationCriteria is Comparer.OrdinalIgnoreCase, which just says don't mind mismatched cases
- Finally, we wrap the list of replaced values in Text.Combine which converts each list item back to a text value.
As for why it isn't working, it's hard to say by looking at the error you received and without having your file. But try those test queries above to get a feel for it and see if it makes more sense.
- Create a new blank query: = tblReplacement[Sipee]