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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
anvikuttu
Advocate I
Advocate I

Need help on M Query

Hi Team,

I need your help in solving in Power query.

I have 2 tables Table A and Table B.  I am trying to update Table A (Result field)  by searching values from Table B in Table A. If the values are found then I want the lookup value to be updated in the Result field of Table A.

My code didn't work, as the output is either producing a list. I am unable to get the iterative value.

 

Table.AddColumn(#"Changed Type1", "Custom", each if Table.Contains(#"Rejection_Codes",[LookupValue= [Test]]) then Rejection_Codes[LookupValue]{0} else [Test])

 

 

anvikuttu_0-1644865248118.png

 

anvikuttu_1-1644865270377.png

 

Thanks in advance

2 ACCEPTED SOLUTIONS
AlexisOlson
Super User
Super User

You aren't too far off but I think I'd write it like this:

= Table.AddColumn(
    #"Changed Type1",
    "Custom",
    (t) => List.Last(
               List.Select(
                   TableB[LookupValue],
                   each Text.Contains(t[Test], _)
               )
           ) ?? "Not Found",
    type text
)

 The "??" part indicates what to return if the preceding expression is null.

View solution in original post

Compute them separately and use if/then logic to return the text you want.

 

(t) =>
    [
        Partial = List.Last(List.Select(TableB[LookupValue], each Text.Contains(t[Test], _))),
        Exact   = List.Last(List.Select(TableB[LookupValue], each t[Test] = _)),
        Text    =
            if Exact <> null then "Exact match " & Exact
            else if Partial <> null then "Partial match " & Partial
            else "No match"
    ][Text]

 

View solution in original post

10 REPLIES 10
AlexisOlson
Super User
Super User

You aren't too far off but I think I'd write it like this:

= Table.AddColumn(
    #"Changed Type1",
    "Custom",
    (t) => List.Last(
               List.Select(
                   TableB[LookupValue],
                   each Text.Contains(t[Test], _)
               )
           ) ?? "Not Found",
    type text
)

 The "??" part indicates what to return if the preceding expression is null.

Hi AlexisOlson,

I thought I could tweak your code to do multiple conditions but didn't work. Could you please look into this, i would like a solution similar to the one you had provided but checking the below conditions? 

First Check for 

1. Exact Match

else

2. Partial Match (which you have provided)

else

""

How about this?

(t) => List.Last(
        List.Select(
            TableB[LookupValue],
            each Text.Contains(t[Test], _)
        ) &
        List.Select(
            TableB[LookupValue],
            each t[Test] = _
        )
    ) ?? ""

 This checks both partial and exact matches and List.Last means it will return the last exact match if one exists since I appended it after the list of partial matches.

Thank AlexisOlson, my apologies for not making my question clear. I would like the output to make the distinction between Exact and partial....like for e.g. "Exact match ???"  or "Partial match C/O" 

Thank you for taking the effort to help me out 

Compute them separately and use if/then logic to return the text you want.

 

(t) =>
    [
        Partial = List.Last(List.Select(TableB[LookupValue], each Text.Contains(t[Test], _))),
        Exact   = List.Last(List.Select(TableB[LookupValue], each t[Test] = _)),
        Text    =
            if Exact <> null then "Exact match " & Exact
            else if Partial <> null then "Partial match " & Partial
            else "No match"
    ][Text]

 

You are genius ...thanks a lot...have been breaking my head for last few hours. 🙂 

I have learnt a lot from you... could you please share some links where i could improve my knowledge on M query.

I'd recommend this blog series and lots of practice solving real problems.
https://bengribaudo.com/blog/2017/11/17/4107/power-query-m-primer-part1-introduction-simple-expressi...

Wow!!...simple and elegant....Thank you for your help!

ronrsnfld
Super User
Super User

let

//read in lookup table and create a list
    SourceLookup = Excel.CurrentWorkbook(){[Name="Table16"]}[Content],
    lookup = Table.TransformColumnTypes(SourceLookup, {"LookupValue", type text})[LookupValue],

//read in Table A
    Source = Excel.CurrentWorkbook(){[Name="Table15"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Test", type text}}),

//see if there is a match
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Result", each 
        let 
            matchPos =List.PositionOf(
                        List.Transform(lookup, (ss)=> 
                            Text.Contains([Test],ss)),true,Occurrence.Last)
        in  
            if matchPos = -1 then "Not Found" else lookup{matchPos})
in
    #"Added Custom"

ronrsnfld_0-1644873103089.png

 

 

Thank you very much for your help.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors