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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Super User
Super User

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

@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors