Vlookup in Power Query with If statement

hananak

Board Regular
Joined
Feb 10, 2022
Messages
110
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Dear All,

I am trying to do a Vlookup with if statement in Power Query but unable to.

in Excel I have done in this way --> =IF(P3="Recruited",VLOOKUP(AA3,'Sheet1'!A:B,2,0),)

Please if you could help me on how to do this in Power Query?

Many Thanks.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
In PQ, you should be joining tables to achieve vlookup results. On the Home Tab, look to merge tables.

 
Upvote 0
Power Query doesn't understand Excel formulas. You need to solve it using M functions.

I created a small example with two tables:
Book1
ABCDE
1LookupTableDataTable
2Lookup ValueResultLookup LetterCondition
3A1FWhatever
4B2ARecruited
5C3FWhatever
6D4DWhatever
7E5BRecruited
8F6IWhatever
9G7IWhatever
10H8JWhatever
11I9BRecruited
12J10JWhatever
Sheet1
 
Upvote 0
Power Query doesn't understand Excel formulas. You need to solve it using M functions.

I created a small example with two tables:
Book1
ABCDE
1LookupTableDataTable
2Lookup ValueResultLookup LetterCondition
3A1FWhatever
4B2ARecruited
5C3FWhatever
6D4DWhatever
7E5BRecruited
8F6IWhatever
9G7IWhatever
10H8JWhatever
11I9BRecruited
12J10JWhatever
Sheet1


The tables are called LookupTable and DataTable and here's a Query that adds the matching value from the LookupTable[Result] if there's "Recruited" on the DataTable[Condition]:
Power Query:
let
    LookupTable= Table.Buffer(Excel.CurrentWorkbook(){[Name= "LookupTable"]}[Content]),
    Source = Excel.CurrentWorkbook(){[Name= "DataTable"]}[Content],
       ResultColumn = Table.AddColumn(
                            Source, "Result", 
                            each if [Condition]="Recruited" 
                            then LookupTable[Result]
                                 { List.PositionOf(LookupTable[Lookup Value], [Lookup Letter]) } 
                            else null)
in
    ResultColumn
The LookupTable -step at the beginning of my code is just the LookupTable. I wanted to buffer it in case it's actually a much bigger one than the one in my Example.
The lookup logic of the ResultColumn -step is a bit harder to understand so I'll try to explain it:
LookupTable[Result] is the list we want to return the result value from. The List.PositionOf -function returns the index number from the LookupTable[Lookup Value] -list that matches the contents of the DataTable[Lookup Letter] -field of that row. So basically it's the PowerQuery equivalent of Excel's INDEX/MATCH where the List.PositionOf is doing the MATCH part.
 
Upvote 0
Power Query doesn't understand Excel formulas. You need to solve it using M functions.

I created a small example with two tables:
Book1
ABCDE
1LookupTableDataTable
2Lookup ValueResultLookup LetterCondition
3A1FWhatever
4B2ARecruited
5C3FWhatever
6D4DWhatever
7E5BRecruited
8F6IWhatever
9G7IWhatever
10H8JWhatever
11I9BRecruited
12J10JWhatever
Sheet1


The tables are called LookupTable and DataTable and here's a Query that adds the matching value from the LookupTable[Result] if there's "Recruited" on the DataTable[Condition]:
Power Query:
let
    LookupTable= Table.Buffer(Excel.CurrentWorkbook(){[Name= "LookupTable"]}[Content]),
    Source = Excel.CurrentWorkbook(){[Name= "DataTable"]}[Content],
       ResultColumn = Table.AddColumn(
                            Source, "Result",
                            each if [Condition]="Recruited"
                            then LookupTable[Result]
                                 { List.PositionOf(LookupTable[Lookup Value], [Lookup Letter]) }
                            else null)
in
    ResultColumn
The LookupTable -step at the beginning of my code is just the LookupTable. I wanted to buffer it in case it's actually a much bigger one than the one in my Example.
The lookup logic of the ResultColumn -step is a bit harder to understand so I'll try to explain it:
LookupTable[Result] is the list we want to return the result value from. The List.PositionOf -function returns the index number from the LookupTable[Lookup Value] -list that matches the contents of the DataTable[Lookup Letter] -field of that row. So basically it's the PowerQuery equivalent of Excel's INDEX/MATCH where the List.PositionOf is doing the MATCH part.
Hi Misca,

Thank you. The code is working but the only problem is my file is very heavy and each month more and more rows will be added, I ran this query on the original file and it is taking forever and then crashes.

Any advice?
 
Upvote 0
Did you try the method presented in the video link in Post #2?
that is a simple merge. In my case I only want to merge when a criteria is met.

So a combination of IF + Vlookup.

Any help would be appreciated.
 
Upvote 0
Hi Misca,

Thank you. The code is working but the only problem is my file is very heavy and each month more and more rows will be added, I ran this query on the original file and it is taking forever and then crashes.

Any advice?
Try merging the columns. I've never tried conditional merging but use the merge column as a helper column and have the final column pick the value from the helper column only when the condition matches. Or if there's only one condition you need to check you can add a column with the "Recruited" on each row so you can merge the queries using two columns. This way you don't need a helper column:

1720622016336.png
 
Upvote 0
Solution
Try merging the columns. I've never tried conditional merging but use the merge column as a helper column and have the final column pick the value from the helper column only when the condition matches. Or if there's only one condition you need to check you can add a column with the "Recruited" on each row so you can merge the queries using two columns. This way you don't need a helper column:

View attachment 113905
Thanks, that worked for me.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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