Forum

Custom column with ...
 
Notifications
Clear all

Custom column with conditions on two special values (either one of the values exists, or both values exists) in Power Query

14 Posts
2 Users
0 Reactions
89 Views
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi Catalin

Thank for helping in my previous post

still got one query on custom column ED

ED - If Grp 1 …… Grp X consists of UserDomain, Everyone, then return "Yes", Else "No"

See my attached 

 

Thank you so much from learning PQ from you

 
Posted : 26/08/2019 10:41 pm
(@catalinb)
Posts: 1937
Member Admin
 

Use List.Contains. If you refer to a single column from a table, that is a list, you can use List functions on that column:

= Table.AddColumn(#"Added Custom2", "Custom", each if List.Contains([Grouped][User Group ID],"UserDomain users") then true else false)

 
Posted : 27/08/2019 11:48 pm
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi Catalin Sir,

Table.AddColumn("[Count]", "ED", each if List.Contains([Count][User Group ID], "Everyone") then true else false

Got error,  my syntax correct?

and how to incorporate the OR,  that is the following conditions fulfilled

"Everyone"

"UserDomain users"

"Everyone" and "UserDomain users"

 

Thank You

 
Posted : 28/08/2019 3:01 am
(@catalinb)
Posts: 1937
Member Admin
 

Hi Chris,

When you click the button to add new column, in the dialog window that shows up you should paste only the formula, not the entire step syntax. The entire step syntax can be used only if you edit the M code. The formula for adding this new column should be:

=if List.Contains([Count][User Group ID], "Everyone") then true else false

See the List functions description, there is a List.Contains function, List.ContainsAny, List.ContainsAll.

List.ContainsAny can be used in an OR scenario, List.ContainsAll can be used in a AND scenario.

 
Posted : 28/08/2019 3:18 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi Catalin,

my current ED 

Table.AddColumn(#"Added Custom2", "ED", each if List.ContainsAny([Grouped][User Group ID], {"Everyone","UserDomain users"}) then "Yes" else "No")

123,  345 and 998 should be "No"

 

realised that only equal to "Yes" if

"Everyone","UserDomain users" and all the rest null is equal

"UserDomain users" and all the rest null is equal

"Everyone" and all the rest null is equal

 

to "Yes"

 
Posted : 28/08/2019 10:56 pm
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Catalin,

Need your expertise to advise on the code to take care null

 
Posted : 28/08/2019 10:56 pm
(@catalinb)
Posts: 1937
Member Admin
 

Chris Yap said
and how to incorporate the OR,  that is the following conditions fulfilled
"Everyone"
"UserDomain users"
"Everyone" and "UserDomain users"
 
  

I think the query returns what you described above, not sure I understand what you mean.

From those User ID's, which one should return No and which should return Yes?

123
345
678
898
998
1190
1298
 
Posted : 28/08/2019 11:23 pm
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi Catalin,

123,  345 and 998 should return "No",  basically only

"Everyone" + the rest null
"UserDomain users"  + the rest null
"Everyone" and "UserDomain users"  + the rest null

return "Yes"

 

How to code it in the ListContainAll or ListcontainAny ?

 

Thank you !

 
Posted : 28/08/2019 11:40 pm
(@catalinb)
Posts: 1937
Member Admin
 

Ok, I understand that 998 should return no, but why 898 should return yes?

Add a new column with this formula:

if List.Count([Grouped][User Group ID]) = List.Count(List.Select([Grouped][User Group ID], each List.Contains({"Everyone","UserDomain users"}, _))) then "Yes" else "No"

 
Posted : 28/08/2019 11:50 pm
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

898 is return No

 
Posted : 29/08/2019 12:02 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Catalin,   thank you so much on your expertise in PQ,   really appreciate it 

 
Posted : 29/08/2019 12:08 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Catalin

are you able to explain your formula as layman as possible

Thank you very much

 
Posted : 29/08/2019 12:20 am
(@catalinb)
Posts: 1937
Member Admin
 

Chris Yap said
123,  345 and 998 should return "No",  basically only

Chris Yap said
898 is return No  

898 returns no, but it was not in the list you mentioned that should return no.

1. Left side of the equation: formula:  List.Count([Grouped][User Group ID]) will return a number that represents how many values are in the User Group ID column (excludes null values from that column).

2. Right side of the equation: formula: List.Select([Grouped][User Group ID], each List.Contains({"Everyone","UserDomain users"}, _)) will return another list with only those entries from [User Group ID] that can be found in this list: {"Everyone","UserDomain users"}, obviously there can be between 0 or 2 items in this list produced, if there is no match or one or both values are found.

If you count the items from the list produced with this last formula, you will be able to compare it with the full list count. If the count is the same, then your conditions are met.

if List.Count([Grouped][User Group ID]) = List.Count(List.Select([Grouped][User Group ID], each List.Contains({"Everyone","UserDomain users"}, _))) then "Yes" else "No"

 
Posted : 29/08/2019 12:44 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi Catalin,

I get what you mean,   898 got no "Everyone","UserDomain user" at all,  this is not true in real environment,  I didn't mock the data accurately  haha

 

Thank you !

 
Posted : 29/08/2019 2:24 am
Share: