Thursday 19 October 2023

Vlookups in Power Query (for power BI)

If you're familiar with Excel before moving to Power Query then it's a little maddening to find that PowerQuery doesn't have a built in "Vlookup" option. It's easily done via the M code though, you simply add this code:

Whole thing:
= Table.AddColumn(#"Previous Step", "Name of New Column", each #"Lookup Table"[Target Field]{List.PositionOf(#"Lookup Table"[LTMatching Field], [Matching Field])},Text.Type)

Where the "Target Field" is the one you want to get the value from using the "Matching field" in the current table which corresponds to the "LTMatching Field" in the other table.

Add a Column Box
Alternatively you can just do this through the add a column box where you'd just put:

#"Lookup Table"[Target Field]{List.PositionOf(#"Lookup Table"[LTMatching Field], [Matching Field])}


Usually though it's better just to merge the two queries. I do suspect this method might be a bit quicker though.

No comments:

Post a Comment