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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
kmj97024
New Member

DAX with For/While Loop

Hi All,

 

I created some DAX code which I want it to work as a For/While loop. I used this page as a reference. (https://community.powerbi.com/t5/Community-Blog/For-and-While-Loops-in-DAX/ba-p/636314)

kmj97024_3-1658195307330.png

LOOP - HR Analysis = 
VAR IndexNum = 1
VAR TotalValue = 0
VAR LoopTable = GENERATESERIES(1, 1)
VAR LoopTable1 = ADDCOLUMNS(LoopTable, "SUM", TotalValue + SUMX(FILTER(LoopTable, [Value]<=EARLIER([Value])), 
                                                            [LOOP - HR Analysis Value]))
VAR MaxInteraction = MAXX(LoopTable1, [Value])
RETURN MAXX(FILTER(LoopTable1, [Value]=MaxInteraction), [SUM])

But I want this code to work something similar to below, where I can give the iterator value as a parameter for my measure used in the expression area.

kmj97024_2-1658195281486.png

As you can see I get a syntax error in this example though. Does anyone know if I can achieve the same behaviour, without an error?

 

Thank you!

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @kmj97024 ,

You can try to use the following function:

LOOP - HR Analysis =
VAR IndexNum = 1
VAR TotalValue = 0
VAR LoopTable = GENERATESERIES(1, 1)
VAR LoopTable1 = ADDCOLUMNS(LoopTable, "SUM", TotalValue + SUMX(FILTER(LoopTable, [Value]<=EARLIER([Value])),
                                                          SUMX(FILTER(ALL('HR Analysis'),
                                                         'HR Analysis'[Index]=[Value]), [LOOP - HR Analysis Value])))
VAR MaxInteraction = MAXX(LoopTable1, [Value])
RETURN MAXX(FILTER(LoopTable1, [Value]=MaxInteraction), [SUM])

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @kmj97024 ,

You can try to use the following function:

LOOP - HR Analysis =
VAR IndexNum = 1
VAR TotalValue = 0
VAR LoopTable = GENERATESERIES(1, 1)
VAR LoopTable1 = ADDCOLUMNS(LoopTable, "SUM", TotalValue + SUMX(FILTER(LoopTable, [Value]<=EARLIER([Value])),
                                                          SUMX(FILTER(ALL('HR Analysis'),
                                                         'HR Analysis'[Index]=[Value]), [LOOP - HR Analysis Value])))
VAR MaxInteraction = MAXX(LoopTable1, [Value])
RETURN MAXX(FILTER(LoopTable1, [Value]=MaxInteraction), [SUM])

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

amitchandak
Super User
Super User

@kmj97024 , add a filter or you have to use maxx(looptable, [Value])

 

The information you have provided is not making the problem clear to me. Can you please explain with an example.

Appreciate your Kudos.

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.