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

Data Days is here! Join us now for 60+ days of learning, challenges, and connection. Learn more

Reply
smpa01
Community Champion
Community Champion

DAX forLoop

@AlexisOlson  is it possible to replicate a forLoop in DAX. I need a forLoop that works in filter context. I am not able to find anything that can do the following in DAX.

 

let
   src=Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Jcu5DQAxDAPBXhg7kHR+axHcfxsn0gmBHYCZMDS4r9o1cFvCBbs2HoTg1E71xw7+urqreTsmGALd7MmU8BiU+wM=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [index = _t, row = _t, Value = _t]),
    ct = Table.TransformColumnTypes(src,{{"index", Int64.Type}, {"row", Int64.Type}, {"Value", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(ct, "Custom", each let
 curr = [index],
 filt = Table.SelectRows(ct, (_)=> (_[index] >= curr)),
 terminator = Table.RowCount(filt)-1,
 forLoop = List.Generate(
     ()=> [ i =0, x = filt[Value]{i}, y= Text.From(filt[row]{i}), combins = 0+x],       
        each [combins]<=100,
        each [i=[i]+1, x= filt[Value]{i}, y= [y]&"-"&Text.From(filt[row]{i}), combins = [combins]+x],
        each [y]
 ),
 len = List.Count(forLoop)-1
 in if (len>=0) then forLoop{len} else null)
in
    #"Added Custom"

 

Thank you in advance.


========================
Did I answer your question? Mark my post as a solution!
Proud to be a Super User
My Custom Visualization Projects
• Plotting Live Sound: Live Sound
• Beautiful News: Women in Parliament, Energy Mix, Shrinking Armies
• Visual Capitalist: Working Hrs
• Others: Easing Graph, Animated Calendar
MayViz Submissions
• Week 1: View
• Week 2: View
• Week 3: View
• Week 4: View
========================
1 ACCEPTED SOLUTION

Custom DAX =
VAR i = [index]
VAR b =
    ADDCOLUMNS (
        FILTER ( Query1, [index] >= i ),
        "g",
            VAR i2 = [index]
            RETURN
                SUMX ( FILTER ( Query1, [index] >= i && [index] <= i2 ), [Value] )
    )
RETURN
    CONCATENATEX ( FILTER ( b, [g] <= 100 ), [row], "-" )

 

lbendlin_0-1714854546064.png

 

View solution in original post

6 REPLIES 6
smpa01
Community Champion
Community Champion

Great, but how do I apply this to my scenario is the challenge. I need a for Loop to work in a way so that for each row in the filter context it considers a table where filter(all(table), table[index]>= currentIndexVisible) and then keep on conctaenating table[row], till runningTotal of values <=100 (termination clause).


========================
Did I answer your question? Mark my post as a solution!
Proud to be a Super User
My Custom Visualization Projects
• Plotting Live Sound: Live Sound
• Beautiful News: Women in Parliament, Energy Mix, Shrinking Armies
• Visual Capitalist: Working Hrs
• Others: Easing Graph, Animated Calendar
MayViz Submissions
• Week 1: View
• Week 2: View
• Week 3: View
• Week 4: View
========================

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information or anything not related to the issue or question.

Please show the expected outcome based on the sample data you provided.


smpa01
Community Champion
Community Champion

You can use the code from snippet to get the sample data.


========================
Did I answer your question? Mark my post as a solution!
Proud to be a Super User
My Custom Visualization Projects
• Plotting Live Sound: Live Sound
• Beautiful News: Women in Parliament, Energy Mix, Shrinking Armies
• Visual Capitalist: Working Hrs
• Others: Easing Graph, Animated Calendar
MayViz Submissions
• Week 1: View
• Week 2: View
• Week 3: View
• Week 4: View
========================

Custom DAX =
VAR i = [index]
VAR b =
    ADDCOLUMNS (
        FILTER ( Query1, [index] >= i ),
        "g",
            VAR i2 = [index]
            RETURN
                SUMX ( FILTER ( Query1, [index] >= i && [index] <= i2 ), [Value] )
    )
RETURN
    CONCATENATEX ( FILTER ( b, [g] <= 100 ), [row], "-" )

 

lbendlin_0-1714854546064.png

 

Here's a slightly optimized version

 

Custom DAX = 
VAR i = [index]
VAR f =
    FILTER ( Query1, [index] >= i )
VAR g =
    ADDCOLUMNS (
        f,
        "rt", VAR i2 = [index] RETURN SUMX ( FILTER ( f, [index] <= i2 ), [Value] )
    )
RETURN
    CONCATENATEX ( FILTER ( g, [rt] <= 100 ), [row], "-" )
lbendlin
Super User
Super User

All iterator functions are For Loops in DAX. SUMX, MAXX, PRODUCTX etc. One could argue that SUMMARIZE, SUMMARIZECOLUMNS and GENERATE are For Loops too.

Helpful resources

Announcements
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.