A query can be "partially folded", in which a SQL statement is created resolving only part of an overall query
The results of this SQL statement would be returned to Power BI Desktop (or the on-premises data gateway) and the remaining logic would be computed using M's in-memory engine with local resources
M queries can be designed to maximize the use of the source system resources, by using standard expressions supported by query folding early in the query process
Minimizing the use of local or on-premises data gateway resources is a top consideration