# What is the precedence or order of operation for multiple functions applied to a dataset?

This is from the Wolfram Language book section 45 on the planets dataset.

This works:

moonsMass = planets[All, "Moons", Total, "Mass"] // Normal

<|"Mercury" -> 0, "Venus" -> 0,
"Earth" -> Quantity[7.3459*10^22, "Kilograms"],
"Mars" -> Quantity[1.22*10^16, "Kilograms"],
"Jupiter" -> Quantity[3.9301*10^23, "Kilograms"],
"Saturn" -> Quantity[1.4051*10^23, "Kilograms"],
"Uranus" -> Quantity[9.14*10^21, "Kilograms"],
"Neptune" -> Quantity[2.1487*10^22, "Kilograms"]|>


However, if I change to the more logical order, where I get the Mass from the Moons, I get KeyAbsent in the results:

planets[All, "Moons", "Mass", Total] // Normal

<|"Mercury" -> Missing["KeyAbsent", "Mass"],
"Venus" -> Missing["KeyAbsent", "Mass"],
"Earth" -> Missing["KeyAbsent", "Mass"],
"Mars" -> Missing["KeyAbsent", "Mass"],
"Jupiter" -> Missing["KeyAbsent", "Mass"],
"Saturn" -> Missing["KeyAbsent", "Mass"],
"Uranus" -> Missing["KeyAbsent", "Mass"],
"Neptune" -> Missing["KeyAbsent", "Mass"]|>


I haven't found anything in the docs or various books on this. What rules does Mathematice follow when applying nested operations like this on a Dataset?

The documentation on dataset queries is quite detailed in my opinion (open the "Details & Options" section to see it). That being said, the different arguments of dataset queries do not correspond to different steps of the query. Rather they correspond to queries on the different levels of the dataset. The precise ordering is then decided by the categorization of the specofic part of the query (this is defined in great detail in the documentation, see the part on ascending and descending operators) So in your case the first example does the following:

1. Query All planets
2. Query "Moons" for each
3. Implicitly operate on All moons (Total is an ascending operator)
4. Query "Mass" for each
5. On the "way back up", compute the Total of the query "mass of each moon"

The second version corresponds to the following:

1. Query All planets
2. Query "Moons" for each
3. Query "Mass" for the list of moons. This fails, because the list as a whole does not have a member called "Mass"