Showing results for 
Search instead for 
Did you mean: 

For Loop

Super User
Super User
Super User

For Loop

This is a demonstration on how to emulate a traditional For loop in DAX. For further explanation, see this article:



For Loop = 
// Provide some starting values
VAR __n = 5
VAR __sum = 10
// 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",SUMX(FILTER(__loopTable,[Value]<=EARLIER([Value])),[Value]))
// Determine our MAX interation, the maximum value for "i"
VAR __max = MAXX(__loopTable1,[Value])
// Return the value associated with the maximum value of "i" which is the last iteration in our "loop"



Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!