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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Mederic
Helper V
Helper V

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.