Start with your initial base query result which is grouped by "destination":
index=palo | stats count by direction, dest_port | stats values(dest_port) as dest_port, list(count) as src_count, sum(count) as total by direction | sort direction, - src_count
Note: I'm assuming your query above provides you the answer of all info. Without source data, I only fixed syntax errors with comma usage.
Here, you are rolling up all dest_ports using "values", but might be needing "list" instead to keep all values and "dedup" them as needed. Assuming the query above provides the total set of records...
To query only for the first 10 records of every "direction" for the 10 greatest number of times ports were used for that "direction", you need to use "streamstats":
| streamstats count as eventCount by direction, - src_count | where eventCount < 11 | fields - eventCount
This results in the top 10 (or less) records for each "direction" grouped value.
... View more