Forum Discussion
Check some column have some values
- 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 CheckIfColHasContentCopy 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
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?
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