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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
Jthathaisa
Regular Visitor

Adding a loop in Power Bi

Hi Team,

 

How does one add this loop below in power BI 

 

let start = 100;
const rows = [];

for (let i = 0; i < rows.length; i++) {
start = start * (rows[i].priceChange + 1);
}

 

The aim of this loop is to calculate, my price change using this formula

[Initial value, starting at 100]100 * (price change +1 )

but while my price change is zero, the formula changes to

[The value derived from the first formula] previous value * (price change +1)

 

 

 

Thank you

3 REPLIES 3
v-lili6-msft
Community Support
Community Support

hi, @Jthathaisa 

You could refer to this post about it.

For and While Loops in DAX

https://community.powerbi.com/t5/Community-Blog/For-and-While-Loops-in-DAX/ba-p/636314

For your case, just do some adjustment as below:

For Loop = 
// Provide some starting values
VAR __n = 5
VAR __sum = 100
// Generate a "loop table", this will emulate a for loop for i=1 to some number
VAR __loopTable = GENERATESERIES(1,__n)
// Add in our calculated sum, emulating calculations done as iterations over the loop
VAR __loopTable1 = ADDCOLUMNS(__loopTable,"__sum",__sum + PRODUCTX(FILTER(__loopTable,[Value]<=EARLIER([Value])),[Value]))
// Determine our MAX interation, the maximum value for "i"
VAR __max = MAXX(__loopTable1,[Value])
RETURN
// Return the value associated with the maximum value of "i" which is the last iteration in our "loop"
MAXX(FILTER(__loopTable1,[Value]=__max),[__sum])

By the way, you'd better learn PRODUCTX Function and GENERATESERIES Function.

 

Best Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Could you please post sample data and desired outcome?

Sample Data:

 

 

Price change

Formula

Desired outcome

0.00

100 * (Price Change +1)

100

-0.02

100 * (Price Change +1)

98

-0.05

100 * (Price Change +1)

95

0.00

95* (Price Change +1)

95

0.01

95* (Price Change +1)

95.95

0.00

95.95* (Price Change +1)

95.95

-0.02

95.95* (Price Change +1)

94.031

 

so my starting formula is 100 *(price change +1), and continues until my price change = 0.00, if my price change = 0.00 then take the pervious desired outcome  and substitue the 100 with the desired outcome i.e  95* (Price Change +1)

 

I believe my loop is as follows :

 

let start = 100;
let price=0;
const rows = [];

for (let i = 0; i < rows.length; i++) {
start = rows[i].priceChange === 0.00 ? desiredoutcome : start;
price = start * (rows[i].priceChange + 1);
}

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 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.