Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Connor888
Frequent Visitor

Filtering a column based on a list

Hi! I'm trying to filter some data I have in a particular way. Namely I have a long list of ID numbers, with a test result (either Y or N), and I'd like to filter out not just every row where the result is "Y", but every row for each ID with at least one "Y", since IDs often appear more than once in the list with different results. 

 

The code I'm using is as follows: 

 

#"Select Rows" =
let
    TestList =
        {Table.Column(
            Table.SelectRows(#"Table", each [Test] = "Y"),
            "ID"
        )},
Output = Table.SelectRows(#"Table", each List.Contains(TestList, [ID]) = false)

in
    Output

 

However, this doesn't seem to filter my list at all, let alone correctly. Does anyone have any idea why/how I can make this work?

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Ho @Connor888,

The simplest way, I think, to first filter your table for "Y", then Table.Distinct it by ID and then do inner join back on the source table. This should leave only ID with at least one "Y".

The way you do it, in my view a bit more compute-hungry. I think the issue in your code is that you have Table.Column in curly brackets. From what I remember it returns a list, and you put it in another list again. Then you List.Contains on this top-level list and no wander it always returns false as it only contains a list, not scalar values that you want to look up for. Try removing the curly brackets.

You can also improve performance of you version by adding List.Distinct to TestList. It will reduce the number of elements to search through.

Also in the List.Contains... Did you mean to search for [ID]? List.Contains(TestList, [Item]) = false filters IDs with no "Y" result, is this what you intend to do?

 

Added the code similar to yours. This returns all [ID] which at least once have "Y" in the [Test] field. Depends on the size of your data I would probably also buffered the lookup list ( TestList = List.Buffer (List.Distinct(...)) )

TestList = List.Distinct( Table.SelectRows(Table, each ([Test] = "Y"))[ID]),
Output = Table.SelectRows(Table, each List.Contains(TestList, [ID]))

 

Kind regards,
JB

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Ho @Connor888,

The simplest way, I think, to first filter your table for "Y", then Table.Distinct it by ID and then do inner join back on the source table. This should leave only ID with at least one "Y".

The way you do it, in my view a bit more compute-hungry. I think the issue in your code is that you have Table.Column in curly brackets. From what I remember it returns a list, and you put it in another list again. Then you List.Contains on this top-level list and no wander it always returns false as it only contains a list, not scalar values that you want to look up for. Try removing the curly brackets.

You can also improve performance of you version by adding List.Distinct to TestList. It will reduce the number of elements to search through.

Also in the List.Contains... Did you mean to search for [ID]? List.Contains(TestList, [Item]) = false filters IDs with no "Y" result, is this what you intend to do?

 

Added the code similar to yours. This returns all [ID] which at least once have "Y" in the [Test] field. Depends on the size of your data I would probably also buffered the lookup list ( TestList = List.Buffer (List.Distinct(...)) )

TestList = List.Distinct( Table.SelectRows(Table, each ([Test] = "Y"))[ID]),
Output = Table.SelectRows(Table, each List.Contains(TestList, [ID]))

 

Kind regards,
JB

Thanks for your reply! Doing an inner join is how I eventually solved this myself, I was hoping to find a slightly... neater way to do it I suppose, though I hadn't considered that as you say this does involve a lot of computation.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors