Forum Discussion

kongyuancn's avatar
kongyuancn
Helper II
5 years ago
Solved

Check some column have some values

Hello,

I have a problem

 

 

I want to add a column "Yes_Col_Have_B_OR_C"  to check the columns whose name begins with "YES" and check if any of the value is "b" or "c".

In this table, I only wants to check column Yes_1, Yes_2 and Yes_4.

If value under these col is "b" or "c", then I set the result as "TRUE"

 

How to write the Power Query M statements?

 

Thanks,

Yuan Kong

 

  • Jimmy801's avatar
    Jimmy801
    5 years ago

    Hello kongyuancn 

     

    check out this dynamic solution. Use variable ColStartWith to identify all columns that start with that word. Use variable ContentSearchFor to define a list of words to check for

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUUoC4mQ4jtWBiIIwjAKJJUH4yVCxRLBgMkQwCSqYpBQbCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [No_1 = _t, Yes_1 = _t, Yes_2 = _t, No_3 = _t, Yes_4 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"No_1", type text}, {"Yes_1", type text}, {"Yes_2", type text}, {"No_3", type text}, {"Yes_4", type text}}),
        ColStartWIth = "Yes",
        ContentSearchFor = {"b", "c"},
        GetYesColumns = List.Select(Table.ColumnNames(#"Changed Type"),  each Text.StartsWith(_, ColStartWIth)),
        CheckIfColHasContent = Table.AddColumn
        (
            #"Changed Type",
            "CheckYesColumnsForContent",
            (row)=> 
            let 
                GetTable = Record.ToTable(row),
                FilterForColumns = Table.SelectRows(GetTable, (rowsel)=> List.Contains(GetYesColumns,  rowsel[Name])),
                FilterForContent = Table.SelectRows(FilterForColumns, (rowsel)=> List.Contains(ContentSearchFor, rowsel[Value]))
            in 
                if Table.IsEmpty(FilterForContent) then false else true
    
        )
    in
        CheckIfColHasContent

     

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

     

4 Replies

  • Hi kongyuancn 

    Try this code.  You can also download this PBIX file with the same example code

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUUoC4mQ4jtWBiIIwjAKJJUH4yVCxRLBgMkQwCSqYpBQbCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [No_1 = _t, Yes_1 = _t, Yes_2 = _t, No_3 = _t, Yes_4 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"No_1", type text}, {"Yes_1", type text}, {"Yes_2", type text}, {"No_3", type text}, {"Yes_4", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Yes_Col_Have_B_or_C", each if ([Yes_1] ="b" or [Yes_2] = "b" or [Yes_4] = "b") or ([Yes_1] ="c" or [Yes_2] = "c" or [Yes_4] = "c") then true else false)
    in
        #"Added Custom"

     

     

    Regards

    Phil


    If I answered your question please mark my post as the solution.
    If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.

    • kongyuancn's avatar
      kongyuancn
      Helper II

      Thank you Phil for your answer. But I still have a question:

      There maybe a lot of "Yes" columns, how can I use a function (Ex: List.FindText(Table.ColumnNames(#"Table"), "Yes")) instead of specifying those columns one by one?

       

      Further more, I have a matching list (not only "b" and "c"). Is it possible that using the list in the formular instead of specifying them one by one?

      • PhilipTreacy's avatar
        PhilipTreacy
        Super User

        Hi kongyuancn 

        You can use this code - here's the PBIX file

        It searches for any column names containing "Yes"and then just uses those to create the True/False column

         

        let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUUoC4mQ4jtWBiIIwjAKJJUH4yVCxRLBgMkQwCSqYpBQbCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [No_1 = _t, Yes_1 = _t, Yes_2 = _t, No_3 = _t, Yes_4 = _t]),
            Tab = Table.SelectColumns(Source, List.FindText(Table.ColumnNames(Source),"Yes")),
            TF = List.Transform(Table.ToList(Tab), each Text.Contains(_,"b") or Text.Contains(_,"c")),
            Columns = List.Combine({Table.ToColumns(Source),{TF}}),
            #"Converted to Table" = Table.FromColumns(Columns,List.Combine({Table.ColumnNames(Source),{"Yes_Col_Have_B_or_C"}}))
        
        in
            #"Converted to Table"

         

        Phil


        If I answered your question please mark my post as the solution.
        If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.