Table looks like this:
AddNonExistingColumns.png
I need to check if there already is an existing column named "Colors", if so, it should do nothing, otherwise it should add a new column named "Colors".
Then I need it to do the same with columns "GreenFruits" and "Fruits".
This is the code is use in Table (2):
let
Source = Table,
Colors = if List.Count(List.FindText(Table.ColumnNames(Source), "Colors" ))= 1 then Source else Table.AddColumn(Source, "Colors", each "", type text),
GreenFruits = if List.Count(List.FindText(Table.ColumnNames(Colors), "GreenFruits" ))= 1 then Colors else Table.AddColumn(Colors, "GreenFruits", each "", type text),
Fruits = if List.Count(List.FindText(Table.ColumnNames(GreenFruits), "Fruits" ))= 1 then GreenFruits else Table.AddColumn(GreenFruits, "Fruits", each "", type text)
in
Fruits
The first step goes well. List.Count(List.FindText(Table.ColumnNames(Source), "Colors" )) is 1, so it does nothing.
The second step goes well. List.Count(List.FindText(Table.ColumnNames(Colors), "GreenFruits" )) is 0, so it creates the column named "GreenFruits".
The last step does not go well. List.Count(List.FindText(Table.ColumnNames(GreenFruits), "Fruits" )) counts 3, as it finds both "Fruits", "GreenFruits" and "BlueFruits". Which means it tries to create a new column called "Fruits". But that one already exists, so it returns this error:
AddNonExistingColumns2.png
Instead of getting 3 counts on "Fruits", ie "Fruits", "GreenFruits" and "BlueFruits", I need it to only get 1 count, ie an exact match for "Fruits".
The expected result should be 4 columns in the end:
Fruits |
BlueFruits |
Colors |
GreenFruits |
|
|
|
|
Bookmarks