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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors