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 on...
  • 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