To have the resolution you want using the M code it is necessary to try to join the two tables to bring the column that participates in the if condition.
Below is a topic that deals with the same subject. @Melissa and I solved the topic in question using different approaches.