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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Mederic
Post Patron
Post Patron

Add condition to VlookUp M code

Hello everyone,

I would like to add a condition to the existing M code.
Can you please help me?
I would also like to know if Buffer() is necessary for this code, knowing that I actually have about 2,000 rows on one side and 40 rows for the lookup table

I'm sorry for the picture but I don't know how to insert an Excel file

 

Thanks in advance

RegardsVlooUp with Condition.jpg

1 ACCEPTED SOLUTION

Hello - you could add the condition at the step in which you lookup the value from the other table, however, because you are returning more than one column that then needs to be expanded, doing so would present a complication since one condition would return a table and the other would return text.  Instead, I recommend you add a conditional replacement after the expansion, like below.  Based on your dataset size, I don't think it would be necessary to buffer.

SCRIPT

 

Table.ReplaceValue(#"Expanded Custom",each [Warehouse], each if [Catégorie] = "B" then "P" else [Warehouse],Replacer.ReplaceText,{"Warehouse"})

 

RESULT

jennratten_0-1679141722265.png

 

View solution in original post

5 REPLIES 5
Mahesh0016
Super User
Super User

@Mederic Please Share your dummy M code or Dataset.

Helle @Mahesh0016 ,

Thanks for your message,

I have just seen that it is possible to insert tables
Here is the data, I hope it will be used
Thank you in advance

Regards

let
    Source = Excel.CurrentWorkbook(){[Name="Sales"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Catégorie", type text}, {"Product_ID", type text}, {"Sale", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", (x)=> Table.SelectRows(LookUp, (y)=> Text.Contains(x[Product_ID], y[ID]))),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Departement", "Warehouse"}, {"Departement", "Warehouse"})
in
    #"Expanded Custom"

 

 

IDDepartementWarehouse
1ABCL1
2DEFL2
3GHIL3
4JKLL4

 

CatégorieProduct_IDSale
AV115
AV238
BV134
AV315
AV532

Hello - you could add the condition at the step in which you lookup the value from the other table, however, because you are returning more than one column that then needs to be expanded, doing so would present a complication since one condition would return a table and the other would return text.  Instead, I recommend you add a conditional replacement after the expansion, like below.  Based on your dataset size, I don't think it would be necessary to buffer.

SCRIPT

 

Table.ReplaceValue(#"Expanded Custom",each [Warehouse], each if [Catégorie] = "B" then "P" else [Warehouse],Replacer.ReplaceText,{"Warehouse"})

 

RESULT

jennratten_0-1679141722265.png

 

Hello @jennratten ,
Thank you for your solution which works very well and the explanations 
Otherwise, it's fast overall with 2,000 lines (about 5-6 seconds to refresh)

Have a nice day

Regards

You're welcome!

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors