none
Is []? {}? syntax is any better than try .. otherwise? RRS feed

  • Question

  • From performance point of view, is []? {}? syntax any better than try .. otherwise?

    If you have a chain of [] {} calls, definitely try otherwise is more convenient, but what if this forces PQ to do more job (handling error, etc.)?



    Sunday, August 18, 2019 4:04 PM

Answers

  • If there is actually a missing field or index position, then "try" is going to be slower. If there is not, then "try" may be marginally faster. The disadvantages of try are that it's less readable (in my opinion) and that it catches all errors and not just the ones you think might happen.
    Friday, August 23, 2019 2:04 PM

All replies

  • not that I know of. From personal experience, I barely use it, but It's essentially the way that Curt likes writing his code most of the time that I've seen him share M code.

    try offers much more and I wouldn't really categorize this comparison as apples to apples. The explicit way using Record.FieldOrDefault or  List.SingleOrDefault would be the best way to compare these.

    Here's one situation where try (just as is) offers so much more value:

    https://www.poweredsolutions.co/2019/06/18/error-handling-iferror-in-power-bi-power-query/

    Sunday, August 18, 2019 5:37 PM
  • If there is actually a missing field or index position, then "try" is going to be slower. If there is not, then "try" may be marginally faster. The disadvantages of try are that it's less readable (in my opinion) and that it catches all errors and not just the ones you think might happen.
    Friday, August 23, 2019 2:04 PM
  • The last time I used []? it looks more native in such case:

    = Table.Group(
        table, 
        {"client"}, 
        {{"last sales for green", 
        each Table.Max(
            Table.SelectRows(_, each [color]="green"),
            "date")[sales]?,
        type number}})

    If there're no rows for "green", then you'll get an empty table on Table.SelectRows, and Table.Max will return null, so the direct field reference here isn't working.


    Maxim Zelensky Excel Inside

    Sunday, August 25, 2019 10:12 AM