Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
strdst2090
Frequent Visitor

Multiple IF Conditions

I have a large dataset (about 300,000 records), and I have to build a custom column based on one of the columns in the dataset. The custom column is basically an 'IF-ELSE' condition, however I have too many conditions (about 1000ish). Clearly I cannot write an if-else with that many conditions. What are my other options?

 

An example if clause for my use case with a single condition would look like this - if Text.Contains([Line Description],"ABC")  then "AAA" else ""

1 ACCEPTED SOLUTION
PwerQueryKees
Super User
Super User

Even better would be to add some exception handling: Return all replacement values found, (separated by commas) and an empty string if no conditions are found:

 

=  Text.Combine(Table.SelectRows(ReplaceConditions, (current_row) => Text.Contains([Line Description], current_row[SearchText]))[ReplaceText],",")

View solution in original post

6 REPLIES 6
PwerQueryKees
Super User
Super User

And even better, better, buffering the ReplaceConditions for performance and I tested it now:

 

Created 2 tables in Excell. The purple one is your big dataset 😃

PwerQueryKees_2-1729864490069.png

 

Added column with this query:

// BigDataset
let
    Source = Excel.CurrentWorkbook(){[Name="BigDataset"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Line", Int64.Type}, {"Line Description", type text}}),
    // Load to memory
    BufferdConditions = Table.Buffer(ReplaceConditions),
    // Add the new column. I named it MatchResults
    #"Added Custom" = Table.AddColumn(#"Changed Type", "MatchResult", each Text.Combine(Table.SelectRows(BufferdConditions, (current_row) => Text.Contains([Line Description], current_row[SearchText]))[ReplaceText],","))
in
    #"Added Custom"

With this result, loaded back into Excel in 45 seconds on my reasonably fast laptop:

PwerQueryKees_3-1729864570465.png

 

And pleas keep in mind: PowerQuery is case sensitive!
If you don't want the match to be case sensitive use

Text.Contains([Line Description], current_row[SearchText],Comparer.OrdinalIgnoreCase)

Which loads in 60 seconds (so 30% slower)

 

PwerQueryKees
Super User
Super User

Even better would be to add some exception handling: Return all replacement values found, (separated by commas) and an empty string if no conditions are found:

 

=  Text.Combine(Table.SelectRows(ReplaceConditions, (current_row) => Text.Contains([Line Description], current_row[SearchText]))[ReplaceText],",")

Oh wow!! This works very well. Just tried. Mannnn I've got tears of happiness rolling down my eyes rn. I do need to straighten up my ReplaceConditions table though. Refine it so that each pattern has only one o/p. I will reach out to you separately if I have any trouble refining the code (if I have to). Thank you so much!

Ha ha, glad to make you so happy and I appreciate you sharing it!

You're welcome!

PwerQueryKees
Super User
Super User

I would create a separate table named ReplaceConditions with 2 columns SearchText and ReplaceText

Your add column formula would be something like

= Table.SelectRows(ReplaceConditions, (current_row) => Text.Contains([Line Description], current_row[SearchText])[ReplaceText]{0})

Haven't tested it.... If you have trouble implementing it, I can provide more help.

lbendlin
Super User
Super User

Where does the data come from?  Can you add the column at the source?

 

Is the condition something you can do with a regular expression?

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors