If I was you I'd avoid doing that. Instead I would say in Column N, First generate a List of Distinct Values from column M and then apply the formula I shared above on Column N.
In case you don't know (apologies if you already did) how to create a list of distinct values then follow these steps:
1. Select the Entire range M1:M50 and give it a Name Reference say List.
2. Then Select only M1 and give it a named reference say List_Start
3. Then in Column N, starting from N2 enter the below formula and instead of Enter , hit CTRL + SHIFT + ENTER. This will create an array (you should be able to see Curly brackets {} at the start and end of the formula. Then drag this formula down
=IF(COUNT(IF(MATCH(List,List,0)=(ROW(List)-ROW(List_start)+1),INDEX(List,MATCH(List,List,0)),""))>=(ROW(List)-ROW(List_start)+1),INDEX(List,SMALL(IF(MATCH(List,List,0)=(ROW(List)-ROW(List_start)+1),MATCH(List,List,0),""),ROW(List)-ROW(List_start)+1)),"")
4. Once you have created this Distinct List of Entities modify the formula I shared earlier to get the result.
CAUTION: This could potentially slow down your worksheet. So don't drag the Distinct formula all the way to 50 rows. Keep it till the point you can see all the distinct values getting generated.