Forum Discussion

zh1988's avatar
zh1988
Regular Visitor
1 year ago
Solved

Help with measure for rolling NPS

Hi there,   I'm trying to build a DAX measure in Power BI to calculate a rolling Net Promoter Score (NPS) based on selected months. I'm having trouble getting the cumulative calculation to work cor...
  • MohamedFowzan1's avatar
    1 year ago

    Hi zh1988 

    The Basic measures you need:

    Promoters =
    CALCULATE(
        COUNTROWS('fact_table'),
        'fact_table'[nps score] >= 9
    )
    
    Detractors =
    CALCULATE(
        COUNTROWS('fact_table'),
        'fact_table'[nps score] <= 6
    )
    
    TotalResponses =
    COUNTROWS('fact_table')
    
    NPS =
    DIVIDE(
        [Promoters] - [Detractors],
        [TotalResponses],
        0
    ) * 100

     
    The Rolling Sums:

    RollingPromoters =
    VAR CurrentMonthOrder = MAX('DateTable'[MonthNumber])
    VAR SelectedMonths =
        CALCULATETABLE(
            VALUES('DateTable'[MonthNumber]),
            ALLSELECTED('DateTable')
        )
    VAR MinSelectedMonth = MINX(SelectedMonths, [MonthNumber])
    RETURN
    CALCULATE(
        [Promoters],
        FILTER(
            ALL('DateTable'),
            'DateTable'[MonthNumber] >= MinSelectedMonth &&
            'DateTable'[MonthNumber] <= CurrentMonthOrder
        )
    )
    
    RollingDetractors =
    VAR CurrentMonthOrder = MAX('DateTable'[MonthNumber])
    VAR SelectedMonths =
        CALCULATETABLE(
            VALUES('DateTable'[MonthNumber]),
            ALLSELECTED('DateTable')
        )
    VAR MinSelectedMonth = MINX(SelectedMonths, [MonthNumber])
    RETURN
    CALCULATE(
        [Detractors],
        FILTER(
            ALL('DateTable'),
            'DateTable'[MonthNumber] >= MinSelectedMonth &&
            'DateTable'[MonthNumber] <= CurrentMonthOrder
        )
    )
    
    RollingTotalResponses =
    VAR CurrentMonthOrder = MAX('DateTable'[MonthNumber])
    VAR SelectedMonths =
        CALCULATETABLE(
            VALUES('DateTable'[MonthNumber]),
            ALLSELECTED('DateTable')
        )
    VAR MinSelectedMonth = MINX(SelectedMonths, [MonthNumber])
    RETURN
    CALCULATE(
        [TotalResponses],
        FILTER(
            ALL('DateTable'),
            'DateTable'[MonthNumber] >= MinSelectedMonth &&
            'DateTable'[MonthNumber] <= CurrentMonthOrder
        )
    )

     
    Rolling perc & NPS:

    RollingPromotersPct =
    DIVIDE(
        [RollingPromoters],
        [RollingTotalResponses],
        0
    )
    
    RollingDetractorsPct =
    DIVIDE(
        [RollingDetractors],
        [RollingTotalResponses],
        0
    )
    
    RollingNPS =
    DIVIDE(
        [RollingPromoters] - [RollingDetractors],
        [RollingTotalResponses],
        0
    ) * 100


    Please do mention the kind of issue faced or what discrepancy occurs if this doesn't help

  • zh1988's avatar
    zh1988
    1 year ago

    Hi there, thank you very much!! When first applying the measures it did not take the year slicer into account (I also did not mention that explicitly) so the totals were based on the complete data set, but by adding a month key into allmeasures it now shows correct data. 

    RollingPromoters_monthkey =
    VAR CurrentMonthKey = MAX('dim_date'[Year]) * 100 + MAX('dim_date'[Month])
    VAR SelectedMonthKeys =
        SELECTCOLUMNS(
            FILTER(
                ALLSELECTED('dim_date'),
                NOT(ISBLANK('dim_date'[Month])) &&
                NOT(ISBLANK('dim_date'[Year]))
            ),
            "MonthKey", 'dim_date'[Year] * 100 + 'dim_date'[Month]
        )
    VAR MinSelectedMonthKey = MINX(SelectedMonthKeys, [MonthKey])

    RETURN
    CALCULATE(
        [Promoters],
        FILTER(
            ALL('dim_date'),
            ('dim_date'[Year] * 100 + 'dim_date'[Month]) >= MinSelectedMonthKey &&
            ('dim_date'[Year] * 100 + 'dim_date'[Month]) <= CurrentMonthKey &&
            ('dim_date'[Year] * 100 + 'dim_date'[Month]) IN SelectedMonthKeys
        )
    )

    😃😃