Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

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
  • = Table.AddColumn(Source, "Custom", each if List.Contains(Table.Column(Source, "Col A"), [Col B]) then 1 else 0)

     

    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"

     

  • bolfri's avatar
    bolfri
    3 years ago

    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.

6 Replies

  • bolfri's avatar
    bolfri
    Solution Sage

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

     

    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"

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      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.

      • bolfri's avatar
        bolfri
        Solution Sage

        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.

  • YukiK's avatar
    YukiK
    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!

    • Anonymous's avatar
      Anonymous
      Not applicable

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