Bamidbar31:2

נְקֹם נִקְמַת בְּנֵי יִשְׂרָאֵל מֵאֵת הַמִּדְיָנִים אַחַר תֵּאָסֵף אֶל־עַמֶּיךָ׃

"Avenge the children of Israel on the Midyanites. Afterward you shall be gathered to your people."

Bamidbar 31:2

נְקֹם נִקְמַת בְּנֵי

Bamidbar / Numbers 31:2

נְקֹם נִקְמַת בְּנֵי יִשְׂרָאֵל מֵאֵת הַמִּדְיָנִים אַחַר תֵּאָסֵף אֶל־עַמֶּיךָ׃

"Avenge the children of Israel on the Midyanites. Afterward you shall be gathered to your people."

Pshat פְּשָׁט

Moshe's last task

Rashi: vengeance is taken on the Midyanites, not the Moabites, since Moav acted out of fear while Midyan meddled in a quarrel not its own, and from Moav Rut will come. Ramban: Moshe will not enter the land, but on this side of the Yarden (Jordan) he fulfils all the commandments, and he is given the honour of seeing the vengeance before he dies. Pinchas (Phinehas) leads the war: he began the deed, and he should finish it.

How Rashi and Ramban explain it

Remez רֶמֶז

Whose vengeance is it

Kitzur Ba'al HaTurim: the command of vengeance stands right after the laws of vows: in distress people make vows, and one who brought another to sin answers for it all the more. Rabbeinu Bahya: the Eternal said "vengeance for the children of Israel", but Moshe told the people "the vengeance of the Eternal": Israel is hated for the Torah, so whoever rises against Israel rises against the Almighty.

How Kitzur Ba'al HaTurim and Rabbeinu Bahya explain it

Drash דְּרָשׁ

Hurrying to the last task

Midrash Tanchuma: Moshe (Moses) knew he would die after the vengeance on Midyan, and still he hurried to carry out the command. Yehoshua (Joshua), fearing the same fate, dragged out the war with the kings of Kenaan, and his years were cut by ten: he lived a hundred and ten, not a hundred and twenty like Moshe.

How Midrash Tanchuma explain it

Sod סוֹד

When imagination departs

Mei HaShiloach: Midyan is imagination, which confuses the heart. Moshe (Moses) was the wisdom of all Israel. When the vengeance on Midyan removes imagination from the people's heart, the people will hit upon the will of the Eternal even without Moshe's wisdom, and then Moshe can depart.

How Mei HaShiloach explain it