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
arutsjak90
Helper I
Helper I

While Loops in the visual table to add values and compare

Good morning All, I hope you are all safe in current situation!

 

What I am trying to do is the loop which should check when the sum of the rows in the Plus column is higher than the value in the Difference column, when it finds this sum, it should show it in the first row where the difference is less than zero.

 

Table which I do have right now:

 
YearWeekDifferencePlusMinus
202013-51600-5160
202014-52390-79
202015-841579-3255
202016-682715880
202017158684130
202018634847620
20202462690-79
2020251076945000
202028106900-79
20202985794800-6911
20203077850-794
202033780-7707
202045-10-79
202052449945000

 

What would be perfect if possible:

RowYearWeekDifferencePlusMinusSuggestionNew Difference
1202013-51600-5160100804920
2202014-52390-7904841
3202015-841579-325501586
4202016-68271588001586
520201715868413001586
620201863484762047626348
720202462690-7906269
82020251076945000450010769
9202028106900-79010690
1020202985794800-69114800

8579

1120203077850-79407785
12202033780-7707078
13202045-10-7945004499
1420205244994500004499

 

Suggestion Row1 = if Difference Row1 (-5160)<0,

Difference Row1 *(-1) < Plus Row1 -> No

Difference Row1 *(-1) < Plus Row1 + Plus Row2 -> No

Difference Row1 *(-1) < Plus Row1 + Plus Row2 + Plus Row3-> No

Difference Row1 *(-1) < Plus Row1 + Plus Row2 + Plus Row3 + Plus Row4-> No

Difference Row1 *(-1) < Plus Row1 + Plus Row2 + Plus Row3 + Plus Row4 + Plus Row5-> Yes 

(0 + 0 + 79 + 1588 + 8413) = 10080 > 5160

 

If yes then move calculated (10080) to the first row where Difference is less than 0.

*Values ​​added earlier cannot be used for the next lines

 

New Difference is showing how we will go if we will use Suggestion column.

 

1 REPLY 1
Greg_Deckler
Community Champion
Community Champion

https://gregdeckler.com/2019/03/04/for-and-while-loops-in-dax/


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
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.

June 2025 community update carousel

Fabric Community Update - June 2025

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