Power Query concatenate columns with conditional settings?

macfuller

Active Member
Joined
Apr 30, 2014
Messages
319
Office Version
  1. 365
Platform
  1. Windows
Our ERP system has vendor information in multiple formats, thanks to various tweaks over the years. In this case we are dealing with Business Enterprise info (minority, veteran, women-owned, ...) A vendor can have multiple categories but over the years our programmers have used either binary flags or text for the various categories.

I would like to get a single field with the various conditions, but I'm getting errors when i try to combine IF statements.

VBA Code:
if [#"Women-Owned"] = "Yes" then "Women-Owned " else "" 
& if [Veteran Flag] = "Y" then "Veteran " else ""
& if [Disabled Flag] = "Y" then "Disabled " else ""
& if [EEOC] <> "" then [EEOC] else ""

Is there a way to do this without having to create a new column for each flag and concatenate those?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Maybe add some brackets, like (if .... then .... else) & (if .... then .... else), ...

or perhaps
VBA Code:
[CODE]
= Text.Combine(
         List.Combine (
        {
            { if [#"Women-Owned"] = "Yes" then "Women-Owned " else "" }
           ,{ if [Veteran Flag] = "Y" then "Veteran " else "" }
           ,{ if [Disabled Flag] = "Y" then "Disabled " else "" }
           ,{ if [EEOC] <> "" then [EEOC] else "" }
        }
    )
    , " " )
 
Upvote 0
Solution
Yes thanks, that works.

Power Query:
 #"Added Custom" = Table.AddColumn(AddxBECol, "Test", each
        Text.Combine (
            List.Combine ( 
            {
                {if [#"Women-Owned"] = "Yes" then "Women-Owned" else ""},
                {if [Veteran Flag] = "Y" then "Veteran" else ""},
                {if [Disabled Flag] = "Y" then "Disabled" else ""}
            }
                ), " "
            )
        )

I'm not familiar with Text.Combine. It appears to be putting the delimiter space before the secondary arguments even if there's no primary. In other words, if "Veteran" is the only value in the list then it's returning " Veteran" with a space instead of "Veteran". Am I making a mistake in the syntax? I would have thought the function would be smart enough not to put a delimiter before the first value returned so I must be doing it wrong.
 
Upvote 0
Hi, try this.
Replace else "" by else null.
Use list.RemoveNulls before List.Combine. leave the text.combine as is.
 
Upvote 0
That's actually good to know. As I did not expect that, since the list does contain nulls and I somehow expected it would create a text "Women-Owner,,".
The Microsoft documentation does not explicitly tell Text.Combine dismisses null values. Implicitly it says a list of text values and null is not a text.
1598026811800.png


Glad I could help. And you learned me something too. Thanks!
 
Upvote 0
Your code is well on the way. It just needs some slight adjustment with the parenthesis. You can put each statement between parenthesis, and then use the Combine Operator to concatenate them.

Power Query:
 (if [#"Women-Owned"] = "Yes" then "Women-Owned " else "" )
& ( if [Veteran Flag] = "Y" then "Veteran " else "" )
& ( if [Disabled Flag] = "Y" then "Disabled " else "" )
& ( if [EEOC] <> "" then [EEOC] else "" )

An alternative would be to use the Text.Combine function:

Power Query:
Text.Combine(
  {
    (if [#"Women-Owned"] = "Yes" then "Women-Owned " else ""),
    (if [Veteran Flag] = "Y"     then "Veteran "     else ""),
    (if [Disabled Flag] = "Y"    then "Disabled "    else ""),
    (if [EEOC] <> ""             then [EEOC]         else "")
  }
)

Lastly, if you want to concatenate your values based on a condition, I think you'll enjoy reading this article.

It delves into concatenating values. But also focuses on different data types, using delimiters and concatenating null values.

Cheers,
Rick
 
Upvote 0

Forum statistics

Threads
1,214,667
Messages
6,120,822
Members
448,990
Latest member
rohitsomani

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top