Forum Discussion
Jthathaisa
7 years agoRegular 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 o...
v-lili6-msft
7 years agoCommunity 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