Forum Discussion
Table.FirstN function doesn't seem to work properly for my case...
Hi,
Thanks for the workaround, it is indeed working with SelectRows. The logic has to be like this though in order to be like what was expected with FirstN/LastN :
#"Added JobStarted" = Table.AddColumn(Custom1, "JobStarted", each Table.Last(Table.SelectRows([Custom], each [Isuserpartofteama] = true))[Creationdate], type datetimezone),
#"Added JobEnded" = Table.AddColumn(#"Added JobStarted", "JobEnded", each Table.First(Table.SelectRows([Custom], each [Isuserpartofteama] = false))[Creationdate], type datetimezone),But it actually doesn't answer why it is not working with FirstN/LastN ? Indeed, why for instance "each Table.FirstN([Custom], each [Isuserpartofteama] = false)" returns empty tables for the 1st and 4th line ? I keep reading the documentation and it should work, what is wrong with it please ?
Regards,
- lbendlin3 years agoSuper User
Welcome to the wonderful world of contexts. Yes, they exist in Power Query too. You are nesting one "each" inside another "each" and Power Query gets confused as to which context to use.
Mitigate by using inline functions or variables ("let ... in ..." ) much like you would do in DAX.
- Anonymous3 years agoNot applicable
But I have as many "each" involved/nested in my original code mixing Last/FirstN that I have in the Last/SelectRows version, so why is the context "clearer" in the second case ? Do parenthesis would help regarding this context issue ? Is this a known issue that Microsoft is going to address soon ? This seems crazy.. I mean my code is not yet very complicated and I can't already be sure that it is behaving as expected ? How can I check/debug what context is being use ?
- lbendlin3 years agoSuper User
Break your query into smaller steps, and use inline function to check/debug. You can also use the Query diagnostics to validate the results of each step.
If you like to learn M I can highly recommend the primer here
Power Query M Primer (Part 21): Identifier Scope & Sections | Ben Gribaudo