Yep, the numbering gets more and more unpredictable the more nested rolls you throw in there. AFAIK no one has figured out any pattern, it's just trial and error.
There's plenty of ways to represent the spell rolls depending on preference, but as the rolls get more complicated those $[[0]] calls get increasingly difficult to wrangle. Separating out the damage into a second macro can make life easier - here's a Scorching Ray example that goes up to level 9 - it avoids using $[[0]] calls entirely due to the ungodly number of references you'd have to try to sort for rolling with 10 rays with advantage, and calculating damage. Has a target click to grab AC and colour the rolls blue for hit, green crit and red miss:
&{template:npcaction}{{rname=Scorching Ray}} {{name=Target: **@{target|t1|token_name}**}} {{description=Ray 1: ** ?{Advantage?|Normal,[[1d20cs>[[{0@{target|t1|npc_ac} - @{spell_attack_bonus},2}k1]]cf<19 + @{spell_attack_bonus}]]|
Advantage,[[2d20cs>[[{0@{target|t1|npc_ac} - @{spell_attack_bonus},2}k1]]cf<19kh1 + @{spell_attack_bonus}]]|
Disadvantage,[[2d20cs>[[{0@{target|t1|npc_ac} - @{spell_attack_bonus},2}k1]]cf<19kl1 + @{spell_attack_bonus}]]}
?{Cast at what level?|2,Ray 2: ?{Advantage?}
Ray 3: ?{Advantage?}
Cast at level **2** - |
3,Ray 2: ?{Advantage?} Ray 3: ?{Advantage?} Ray 4: ?{Advantage?}
Cast at level **3** - |
4,Ray 2: ?{Advantage?} Ray 3: ?{Advantage?} Ray 4: ?{Advantage?} Ray 5: ?{Advantage?}
Cast at level **4** - |
5,Ray 2: ?{Advantage?} Ray 3: ?{Advantage?} Ray 4: ?{Advantage?} Ray 5: ?{Advantage?} Ray 6: ?{Advantage?}
Cast at level **5** - |
6,Ray 2: ?{Advantage?} Ray 3: ?{Advantage?} Ray 4: ?{Advantage?} Ray 5: ?{Advantage?} Ray 6: ?{Advantage?} Ray 7: ?{Advantage?}
Cast at level **6** - |
7,Ray 2: ?{Advantage?} Ray 3: ?{Advantage?} Ray 4: ?{Advantage?} Ray 5: ?{Advantage?} Ray 6: ?{Advantage?} Ray 7: ?{Advantage?} Ray 8: ?{Advantage?}
Cast at level **7** - |
8,Ray 2: ?{Advantage?} Ray 3: ?{Advantage?} Ray 4: ?{Advantage?} Ray 5: ?{Advantage?} Ray 6: ?{Advantage?} Ray 7: ?{Advantage?} Ray 8: ?{Advantage?} Ray 9: ?{Advantage?}
Cast at level **8** - |
9,Ray 2: ?{Advantage?} Ray 3: ?{Advantage?} Ray 4: ?{Advantage?} Ray 5: ?{Advantage?} Ray 6: ?{Advantage?} Ray 7: ?{Advantage?} Ray 8: ?{Advantage?} Ray 9: ?{Advantage?} Ray 10: ?{Advantage?}
Cast at level **9** - }[Roll Damage](~bob|ScorchDmg)}}
And the damage macro, currently linked as (~bob|ScorchDmg):
&{template:npcaction}{{rname=Scorching Ray Damage roll}}{{description=**Scorching Ray** hits for a total of** [[ [[?{How many rays hit?|1}*2]]d6[BASE] + [[?{How many rays critically hit?|0}*2]]d6[CRIT] ]] fire damage**, ?{How many rays hit?} rays hit the target and ?{How many rays critically hit?} of those critically hit}}

It's all personal preference, really - there's plenty of ways to handle it. The one above for example, only asks for advantage once. I couldn't decide whether having a potential 9 Queries would be too much, so instead I put a "Level 0" option in the drop down to cast a single ray as required. But if you're generally casting 3 rays, the 3 Queries in one macro is probably a more convenient way to do it. I've never had to cast anything with so many attack rolls in a game, and have no idea which way I'd find easiest.