"holdem_hand_player_detail.val_p_raise_aggressor_pos" holds the position of the last player
other than the active player to make an aggressive action preflop. For the flop, change _p_ to _f_, for turn use _t_, and for river _r_.
"Other than the active player" means that if you're looking at the action from the position of the last player to raise, then the raise aggressor position will not be their position.
An example:
PF: Cutoff raises, Button 3-bets, blinds fold, cutoff calls.
The last player to be agressive was the button, position 0.
If you're looking at the hand from the cutoff's point of view then the raise aggressor position is 0.
If you're looking at the hand from the button's point of view then the raise aggressor position is 1 (the cutoff was the last player to raise other than the button, which is the active player in this case).
To find out if the active player was the aggressor you can check for their last action being "R", like this:
lookup_actions_p.action LIKE '%R'
Tutorial: Custom Reports and StatisticsYou're right about the donk flop stat - if you want to build a version which includes 3- and 4-bet pots you could duplicate it and remove the checks for not 3- or 4-betting.
See the tutorial for how to make a new version of a stat.