Page MenuHomePhabricator

Affiliates output rank metrics
Closed, ResolvedPublic

Description

SQL / Python code to transform input data in to Output Rank Metrics

Input MetricsOutput Rank Metrics
Active affiliates (Count) (all affiliates operating in the coutnry), Affiliate Size (Max) (only affiliates primarily located in the country), Affiliate recognition tenure (Sum)Affiliate Presence
Count of growth points (sum count of affiliates demonstrating new recognition or development in governance or size for those primarily located in the country), % of annual grants going to affiliates, % of historical grants going to affiliatesAffiliate Growth
Historical Grants to Affiliates 2010 to current USD ($), Affiliates Active in Country, Affiliate Size (Largest), Affiliate Tenure, Highest Governance TypeAffiliate Leadership

Event Timeline

initial roll-up at

kcv.affiliate_output_rank_metrics

pending:

  • 3-letter to 2-letter country codes
  • not having all countries causes issues with percent rank

re-opening for QA review.

KCVelaga_WMF renamed this task from Affiliate Metrics Transformation to Affiliates output rank metrics.Nov 14 2022, 10:29 AM

@KCVelaga_WMF I have no idea what's up with the two tiny discrepancies for subcon Australia & New Zealand and continent Africa, any ideas, could the SQL be dropping a comparison point somehow?

Given they are minimal and only two data points, I am inclined to sign-off

It is being caused due to rounding off error. When I limit the number of decimal places to 3, the values match with the outputs from the sheets. If not, they cause slight differences which cause some minor differences in the final ranks. The final query is

WITH
    affiliates_input_metrics_hstack AS (
        SELECT
            geography_level,
            iso2_country_code AS country_code,
            CAST(COALESCE(affiliate_total_active, 0) AS FLOAT) AS affiliate_total_active,
            CAST(COALESCE(affiliate_max_size, 0) AS FLOAT) AS affiliate_max_size,
            CAST(COALESCE(affiliate_count_growth_points, 0) AS FLOAT) AS affiliate_count_growth_points,
            CAST(COALESCE(affiliate_tenure_max, 0) AS FLOAT) AS affiliate_tenure_max,
            CAST(COALESCE(affiliate_tenure_avg, 0) AS FLOAT) AS affiliate_tenure_avg,
            CAST(COALESCE(affiliate_governance_type, 0) AS FLOAT) AS affiliate_governance_type,
            CAST(COALESCE(affiliate_percent_annual_grants, 0) AS FLOAT) AS affiliate_percent_annual_grants,
            CAST(COALESCE(affiliate_percent_historical_grants, 0) AS FLOAT) AS affiliate_percent_historical_grants,
            CAST(COALESCE(historical_grants_weighted_affiliates, 0) AS FLOAT) AS historical_grants_weighted_affiliates
        FROM
            input_metrics),
    
    affiliates_input_metrics_vstack AS (
        SELECT
            geography_level,
            country_code,
            STACK(9, 
                affiliate_total_active, 'affiliate_total_active',
                affiliate_max_size, 'affiliate_max_size',
                affiliate_count_growth_points, 'affiliate_count_growth_points',
                affiliate_tenure_max, 'affiliate_tenure_max',
                affiliate_tenure_avg, 'affiliate_tenure_avg',
                affiliate_percent_annual_grants, 'affiliate_percent_annual_grants',
                affiliate_percent_historical_grants, 'affiliate_percent_historical_grants',
                historical_grants_weighted_affiliates, 'historical_grants_weighted_affiliates',
                affiliate_governance_type, 'affiliate_governance_type') AS (metric_value, metric)
        FROM 
            affiliates_input_metrics_hstack),
    
    affiliate_input_metrics_ranks AS (
        SELECT
            geography_level,
            country_code,
            metric,
            ROUND(
                PERCENT_RANK() OVER (
                    PARTITION BY geography_level, metric
                    ORDER BY metric_value), 3) AS p_rank
        FROM 
            affiliates_input_metrics_vstack),
    
    presence AS (
        SELECT
            geography_level,
            country_code,
            ROUND(
                PERCENT_RANK() OVER (
                    PARTITION BY geography_level
                    ORDER BY AVG(p_rank))*10, 2) AS metric_value,
            'affiliate_presence' AS metric
        FROM 
            affiliate_input_metrics_ranks
        WHERE
            metric IN ('affiliate_total_active', 'affiliate_max_size', 'affiliate_tenure_max', 'affiliate_tenure_avg')
        GROUP BY geography_level, country_code),
    
    growth AS (
        SELECT
            geography_level,
            country_code,
            ROUND(
                PERCENT_RANK() OVER (
                    PARTITION BY geography_level
                    ORDER BY AVG(p_rank))*10, 2) AS metric_value,
            'affiliate_growth' AS metric
        FROM 
            affiliate_input_metrics_ranks
        WHERE
            metric IN ('affiliate_percent_annual_grants', 'affiliate_percent_historical_grants', 'affiliate_count_growth_points')
        GROUP BY geography_level, country_code),
    
    leadership AS (
        SELECT
            geography_level,
            country_code,
            ROUND(
                PERCENT_RANK() OVER (
                    PARTITION BY geography_level
                    ORDER BY AVG(p_rank))*10, 2) AS metric_value,
            'affiliates_leadership' AS metric
        FROM 
            affiliate_input_metrics_ranks
        WHERE
            metric IN ('affiliate_total_active', 'affiliate_max_size', 'affiliate_tenure_max',
                       'affiliate_governance_type', 'historical_grants_weighted_affiliates')
        GROUP BY geography_level, country_code)
    
    
SELECT * FROM presence
UNION ALL
SELECT * FROM growth
UNION ALL
SELECT * FROM leadership