Forum Discussion

jakeryan56's avatar
jakeryan56
Icon for Advocate II rankAdvocate II
5 years ago
Solved

"While loop" in PowerBI - Please help!

Hey guys, I am very desperate for a solution to this problem! Firstly you'll need to be familire with NPS scores - here is some background. I need to calculate for a given NPS score target, h...
  • Greg_Deckler's avatar
    Greg_Deckler
    5 years ago

    jakeryan56 Perhaps below. Basically, use GENERATESERIES to create a table from 1 to whatever in increments of 1. This is the basis for the WHILE loop. The ADDCOLUMNS emulates the loop. At each [Value] you are doing your NPS calculation again but adding the number of promoters specified by the Value column. Once you are done calculating all possible values for your loop, your MINX essentially emulates the "break" condition for your while loop. You are finding the minimum [Value] where your NPS score is equal to or greater than your goal. Once you have that, you just return the value.

    Promoters Required 2 = 
        VAR __CurrentNPS = [NPS_Score]
        VAR __GoalNPS = [Goal_NPS_Score]
        VAR __SurveyCount = [SurveyCount]
        VAR __PromoterSum = [Promoter_SUM]
        VAR __DetractorSum = [Detractor_SUM]
        VAR __Promoters = 
            ADDCOLUMNS(
                GENERATESERIES(1,10000,1),
                "__NPS", ((__PromoterSum+[Value])/(__SurveyCount+[Value]) - (__DetractorSum/(__SurveyCount+[Value])))*100
            )
        VAR __Required = MINX(FILTER(__Promoters,[__NPS]>=__GoalNPS),[Value])
    RETURN
        __Required