Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
jakeryan56
Advocate II
Advocate II

"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, how many Promoters would be required to achieve that.

The only solution I could come up with at the moment is creating multiple variables and then adding one Promoter (and one extra survey count) to each variable and then running an if statement to see which variable returns true.

This is extremely impractical because I need this "loop" to run at least 500 times.  

I've been trying to adapt this post on an alternative "while-loop" proxy but just can't make it work.  Please help 🙂

 

bmfzmtoi68j71.png

1 ACCEPTED SOLUTION

@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

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

4 REPLIES 4
Greg_Deckler
Community Champion
Community Champion

@jakeryan56 Would need sample data. In general, to use a "while loop" in DAX you need to use GENERATESERIES or a table to iterate over. You need to make a calculation at each row and then you filter the table for the first value that meets the goal criteria.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler  You were the exact person I was hoping would reply!

I tried followed your guides but for the life of me couldn't make it work.  
I have put together some sample data here.  Would you be able to take a quick look?

@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

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler  You are the best that worked perfectly!! 
THANK YOU SO MUCH.  You have no idea the hours I have wasted on this and turns out I was very close.

Virtaul beer for you 🍺

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors