Generalize insert of new rows

I have following snippet where I want to remove duplicate code as both blocks do basically the same. Do you have any idea how to create a single function which can be called instead of those two blocks? As use imports are not yet parameterisable yet I am having a hard time improving that code. Here is the snippet:

You can either create a function with quite a lot of generic parameters or create a macro that expands to the corresponding code block.