I'm not sure exactly what this query should be doing, but if you question is how to use insert with row_number then just do this:
INSERT INTO [ReceiptRepository].[Lkup].[BillingGroups]
select M.RowNum, M.BillingGroupNumber
from
(
select BillingGroupNumber, (row_number() over (order by BillingGroupNumber) - 1) % @Counter + 1 as RowNum
from Sap.DetailsImport
) as M
I'm not sure what the other variables are used for or why this is in a while loop.