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

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

Reply
zb134
Helper I
Helper I

Replicating a simple match function in query

I need to compare two columns and return a 1 if the value in Col B exists in Col A and 0 otherwise.

This is ofcourse easily achieved in excel using the MATCH funciton but I can't figure out how to do it in PowerQuery (need it within transformation not using DAX).

 

Any help appreciated!

 

Example Result

Col ACol BResult
1233211
3212110
2214120
2 ACCEPTED SOLUTIONS
bolfri
Solution Sage
Solution Sage

= Table.AddColumn(Source, "Custom", each if List.Contains(Table.Column(Source, "Col A"), [Col B]) then 1 else 0)

bolfri_0-1677771053708.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVtJRMjYyBJKGSrE60VC2kSGINACLGIFFTAyNICKxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Col A" = _t, #"Col B" = _t, Result = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each if List.Contains(Table.Column(Source, "Col A"), [Col B]) then 1 else 0),
    #"Changed Type" = Table.TransformColumnTypes(#"Added Custom",{{"Col A", Int64.Type}, {"Col B", Int64.Type}, {"Result", Int64.Type}, {"Custom", Int64.Type}})
in
    #"Changed Type"

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

Go to Advanced Editor.

 

Code Before:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVtJRMjYyBJKGSrE60VC2kSGINACLGIFFTAyNICKxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Col A" = _t, #"Col B" = _t, Result = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each if List.Contains(Table.Column(Source, "Col A"), [Col B]) then 1 else 0)
in
    #"Added Custom"

 

Change the #Added Custom step to:

= let
buffer_list = Table.Column(Source, "Col A"),
#"StepOne" = Table.AddColumn(Source, "Custom", each if List.Contains(buffer_list, [Col B]) then 1 else 0)
in #"StepOne"

 

Code After:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVtJRMjYyBJKGSrE60VC2kSGINACLGIFFTAyNICKxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Col A" = _t, #"Col B" = _t, Result = _t]),
    #"Added Custom" = 
        let
            buffer_list = Table.Column(Source, "Col A"),
            #"StepOne" = Table.AddColumn(Source, "Custom", each if List.Contains(buffer_list, [Col B]) then 1 else 0)
        in #"StepOne"
in
    #"Added Custom"

 

List.Buffer will work if you want to create your own list with values.

Doc: https://learn.microsoft.com/en-us/powerquery-m/list-buffer

In this case we are using a variable buffer_list that holds all values from Col A and then we are reffering to that variable, without creating it for every row. It will work faster.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

6 REPLIES 6
bolfri
Solution Sage
Solution Sage

= Table.AddColumn(Source, "Custom", each if List.Contains(Table.Column(Source, "Col A"), [Col B]) then 1 else 0)

bolfri_0-1677771053708.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVtJRMjYyBJKGSrE60VC2kSGINACLGIFFTAyNICKxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Col A" = _t, #"Col B" = _t, Result = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each if List.Contains(Table.Column(Source, "Col A"), [Col B]) then 1 else 0),
    #"Changed Type" = Table.TransformColumnTypes(#"Added Custom",{{"Col A", Int64.Type}, {"Col B", Int64.Type}, {"Result", Int64.Type}, {"Custom", Int64.Type}})
in
    #"Changed Type"

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Thanks, this is really helpful. However, with the actual data set it runs really slow. Is there any way to speed it up? I read something about list.buffer but don't know how to implement it.

Go to Advanced Editor.

 

Code Before:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVtJRMjYyBJKGSrE60VC2kSGINACLGIFFTAyNICKxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Col A" = _t, #"Col B" = _t, Result = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each if List.Contains(Table.Column(Source, "Col A"), [Col B]) then 1 else 0)
in
    #"Added Custom"

 

Change the #Added Custom step to:

= let
buffer_list = Table.Column(Source, "Col A"),
#"StepOne" = Table.AddColumn(Source, "Custom", each if List.Contains(buffer_list, [Col B]) then 1 else 0)
in #"StepOne"

 

Code After:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVtJRMjYyBJKGSrE60VC2kSGINACLGIFFTAyNICKxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Col A" = _t, #"Col B" = _t, Result = _t]),
    #"Added Custom" = 
        let
            buffer_list = Table.Column(Source, "Col A"),
            #"StepOne" = Table.AddColumn(Source, "Custom", each if List.Contains(buffer_list, [Col B]) then 1 else 0)
        in #"StepOne"
in
    #"Added Custom"

 

List.Buffer will work if you want to create your own list with values.

Doc: https://learn.microsoft.com/en-us/powerquery-m/list-buffer

In this case we are using a variable buffer_list that holds all values from Col A and then we are reffering to that variable, without creating it for every row. It will work faster.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Thanks for helping me with this. It did speed up things considerably. Howerver, my final step is to filter the Results column with just 0 staying and I run in to the same problem of it taking forever to load with almost a gigabyte + for a simple excel file. Any ideas?

YukiK
Impactful Individual
Impactful Individual

Create a calculated column like the following:

Result = IF( Table[Col A] in VALUES(Table[Col B]), 1, 0 )

 

Please consider giving it a thumbs up and accept as solution if this helps!

Thanks for the suggestion, but I need to do it within transformations with M Query not DAX

Helpful resources

Announcements
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