Reputation: 382
I want to create a conditional column in Excel Power Query that look up a list of companies and return a value like "Yes" if the company column in that given row corresponds with one of the companies in the list.
It should look something like this:
+----------+---------------------+-------+
| Company | List | Match |
+----------+---------------------+-------+
| Apple | Facebook, Instagram | No |
| Facebook | Facebook, Instagram | Yes |
+----------+---------------------+-------+
Constraint: I cannot create additional rows since the excel sheet will be used for importing information to a system, and therefore needs to be structured in a given way.
I have tried using the built in conditional column functionality in Power Query, but it can only look at a specific column, and not several. And it sees the companies list column as a big string like "Facebook, Instagram" and not separate values. Also, the company list is very long, so it would be very inefficient to create if-statements for each company.
Any help will be appreciated!
Upvotes: 0
Views: 3679
Reputation: 1
Hi just to check I understand correctly - do you have a list of Apps and a known list of companies?
If so, you could try:
if
List.AnyTrue(List.Transform({
"Instagram",
"Facebook",
"WhatsApp"
}
,(substring) => Text.Contains([List],substring,Comparer.OrdinalIgnoreCase)))
then "Facebook"
else if
//then repeat for list of other company products
else "No Company"
Though please note, this method is limited by order - i.e. if the [List] column contained both Apple and Facebook Apps/Products it would only designate it the first company listed in your query.
Upvotes: 0
Reputation: 4486
If your initial table looks something like:
then this code:
let
initialTable = Table.FromColumns({{"Apple", "Facebook"}, {"Facebook, Instagram", "Facebook, Instagram"}}, type table [Company = text, List = text]),
matchColumn = Table.AddColumn(initialTable, "Match", each List.Contains(Text.Split([List], ", "), [Company]), type logical)
in
matchColumn
should give expected output:
,
(there is a space after the comma), since that is what you've shown in your question. You can change the code to use ,
if there is no space.true
/false
(instead of "YES"
/"NO"
).Splitter.SplitTextByDelimiter
can help to return a better splitting function, but it requires your values to already be quoted (which they don't appear to be).Alternatively, try:
let
initialTable = Table.FromColumns({{"Apple", "Facebook"}, {"Facebook, Instagram", "Facebook, Instagram"}}, type table [Company = text, List = text]),
matchColumn = Table.AddColumn(initialTable, "Match", each if List.Contains(Text.Split([List], ", "), [Company]) then "AB119" else "AD119", type text)
in
matchColumn
Upvotes: 1