I had an interesting problem recently when the JSON output from an API started to return a null value instead of empty list. This broke my PowerQuery so I wanted to share a fix.

Originally, I used the built-in “Extract values (from a list)” feature, which concatenated them to make comma separated values:

= Table.TransformColumns(#"Removed Columns", {"List_Field", each Text.Combine(List.Transform(_, Text.From), ","), type text})

And that worked fine. Until the content of that field was not a list. Evidently, this field had previously contained an empty JSON list if it contained no values. I’ve never looked at the JSON itself so I had no idea. So, when the API started returning null for that field instead List.Transform failed:

Expression.Error: We cannot convert the value null to type List.

In some of my other Queries it was also failing “silently” because this step was immediately followed by a RemoveRowsWithErrors step. Whoops. Having no idea that the API output had changed (or what it originally contained) was my big problem here. Took me a while to understand what had gone wrong and how.

The fix itself isn’t complex, simply check if the value is a list before concatenation:

= Table.TransformColumns(#"Removed Columns", {"List_Field", each if Value.Is(_, type list) then Text.Combine(List.Transform(_, Text.From), ",") else null, type text})

However, that DOES make me wonder why the built-in doesn’t automatically add code to check the type. Can’t be that hard…

Leave a Reply

Your email address will not be published. Required fields are marked *

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)