I hope it helps.
It is worth mentioning that the total number of users varies over time, so what is being considered in the query is the cumulative number of users up to the month and year that the action was performed.
WITH tt_users_by_month AS (
SELECT
date_part('year', created_at) AS year,
date_part('month', created_at) AS month,
COUNT(*) AS "new_users_month"
FROM users
WHERE id > 0
GROUP BY date_part('year', created_at), date_part('month', created_at)
ORDER BY date_part('year', created_at) ASC, date_part('month', created_at)
),
total_users AS (
SELECT
year,
month,
SUM(new_users_month) over (ORDER BY year, month rows between unbounded preceding AND current row) AS total
FROM tt_users_by_month ORDER BY year, month
)
SELECT
date_part('year', ua.created_at) AS year,
date_part('month', ua.created_at) AS month,
TRUNC(COUNT(DISTINCT user_id)::decimal/tu.total*100,2) AS "%"
FROM
user_actions ua
INNER JOIN total_users tu ON (date_part('year', ua.created_at) = tu.year AND date_part('month', ua.created_at) = tu.month)
WHERE action_type IN (1,5)
GROUP BY date_part('year', created_at), date_part('month', created_at), total