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
elderfd
Frequent Visitor

List.Select does not work as expected when List contains Errors

When a List contains Errors, List.Select does not work as expected. First we generate a list with an Error,

List = {"Hello", 2, #date("not a date")}

The third element of this list is an Error.

 

If we apply List.Select with a condition that no elements can fulfil,

SelectedWithFalse = List.Select(List, each false)

we would expect to get an empty list, but instead get a List containing only the final Error value.

 

If we apply List.Select with a condition that all elements fulfil,

SelectedWithTrue = List.Select(List, each true)

we would expect to get the original list again, but instead get a one element list which contains the first value ("Hello") and which is now marked as an Error. However there is no message attached to this Error, if selected it just shows the original value ("Hello").

1 ACCEPTED SOLUTION
Ehren
Employee
Employee

The reason for this behavior is that lists in M are lazy. The previews (in the Query Editor) of both the "each false" and "each true" queries actually produce the same result. In both cases, the Query Editor attempts to show all the values, which causes List.Select to traverse its entire source list. In the process, it encounters the error value, which interrupts its enumeration of the source list, resulting in a list that contains an error.

 

However, when you navigate into the first item in the resulting list, both the "each false" and "each true" queries attempt to get only the first item resulting from the List.Select. Since the "each true" query's List.Select condition will "match" any input, the “Hello” value is selected and returned, and the error value is never encountered. However, in the "each false" query, the first two inputs do not "match", and thus it moves on to the error value, which interrupts its enumeration and causes the error to bubble up.

 

Hope this helps clarify things.

Ehren

View solution in original post

5 REPLIES 5
Ehren
Employee
Employee

The reason for this behavior is that lists in M are lazy. The previews (in the Query Editor) of both the "each false" and "each true" queries actually produce the same result. In both cases, the Query Editor attempts to show all the values, which causes List.Select to traverse its entire source list. In the process, it encounters the error value, which interrupts its enumeration of the source list, resulting in a list that contains an error.

 

However, when you navigate into the first item in the resulting list, both the "each false" and "each true" queries attempt to get only the first item resulting from the List.Select. Since the "each true" query's List.Select condition will "match" any input, the “Hello” value is selected and returned, and the error value is never encountered. However, in the "each false" query, the first two inputs do not "match", and thus it moves on to the error value, which interrupts its enumeration and causes the error to bubble up.

 

Hope this helps clarify things.

Ehren

elderfd
Frequent Visitor

Thanks @Ehren for the explanation.

 

However, I am still slightly confused. If the Error ceases the traversal of the List, would we not expect the "each true" query to return the first two non-Error elements of the list rather than only the first?

Good question. When you navigate into the first element in the "each true" query, try replacing the generated list index of {0} with {1}, and you should see the second value in the list.

 

The error doesn't just cause the enumeration of the list to stop...it actually changes the result of enumeration to an error.

Ehren

elderfd
Frequent Visitor

@Ehren Thanks a lot for the help, makes sense.

v-qiuyu-msft
Community Support
Community Support

Hi @elderfd,

 

I have sent a email to consult this issue internally, will update here once I get any information. 

 

Best Regards,
Qiuyun Yu 

Community Support Team _ Qiuyun Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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.