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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

DAX query to add numbers until a condition is met

I have two columns named 'HA' and 'PA'. I want to add values in column 'HA' once it is greater than or equal to the value in 'PA'.

HA       PA

1.58     3.24

3.24     3.24

3.23     3.24

the values in column 'PA' will be the same. I want to add values in column 'HA' once the sum is greater than or equal to the value in column 'PA' For eg: it should add the first two rows (1.58 + 3.24 = 4.82) in column 'HA', as its sum will be greater than or equal to the value in columns 'PA'. It should not add the 3rd value in column 'HA'.

1 ACCEPTED SOLUTION
v-yuta-msft
Community Support
Community Support

@Anonymous ,

 

Firstly, click query editor-> add columns-> add index column, then create a calculate column using DAX below:

Running Total = CALCULATE(SUM('Table'[HA]), FILTER('Table', 'Table'[Index] <= EARLIER('Table'[Index])))

1.PNG 

Then, create a measure as below:

Result = 
VAR Index = CALCULATE(MAX('Table'[Index]), FILTER('Table', 'Table'[Running Total] = MIN('Table'[Running Total]) && 'Table'[Running Total] >= 'Table'[PA])) + 1
RETURN
CALCULATE(SUM('Table'[HA]), FILTER('Table', 'Table'[Index] <= Index))

2.PNG 

 

Community Support Team _ Jimmy Tao

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

3 REPLIES 3
v-yuta-msft
Community Support
Community Support

@Anonymous ,

 

Firstly, click query editor-> add columns-> add index column, then create a calculate column using DAX below:

Running Total = CALCULATE(SUM('Table'[HA]), FILTER('Table', 'Table'[Index] <= EARLIER('Table'[Index])))

1.PNG 

Then, create a measure as below:

Result = 
VAR Index = CALCULATE(MAX('Table'[Index]), FILTER('Table', 'Table'[Running Total] = MIN('Table'[Running Total]) && 'Table'[Running Total] >= 'Table'[PA])) + 1
RETURN
CALCULATE(SUM('Table'[HA]), FILTER('Table', 'Table'[Index] <= Index))

2.PNG 

 

Community Support Team _ Jimmy Tao

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

Anonymous
Not applicable

@v-yuta-msft , Thank you for your help. 
Is there a way to create a Calculated column in place of this measure 'Result'?


@v-yuta-msft wrote:

@Anonymous ,

 

Firstly, click query editor-> add columns-> add index column, then create a calculate column using DAX below:

Running Total = CALCULATE(SUM('Table'[HA]), FILTER('Table', 'Table'[Index] <= EARLIER('Table'[Index])))

1.PNG 

Then, create a measure as below:

Result = 
VAR Index = CALCULATE(MAX('Table'[Index]), FILTER('Table', 'Table'[Running Total] = MIN('Table'[Running Total]) && 'Table'[Running Total] >= 'Table'[PA])) + 1
RETURN
CALCULATE(SUM('Table'[HA]), FILTER('Table', 'Table'[Index] <= Index))

2.PNG 

 

Community Support Team _ Jimmy Tao

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


 

Anonymous
Not applicable

Hi

 

are those columns of a table or measures?

Also, how do you choose values to sum? Are they sorted in some ways? 

I guess finally you want to have a MEASURE with the summed amount rather than a column?

 

In general, DAX cannot really work "iteratively" (i.e. do this until a condition is met) but you can achieve the same result in different ways. For example you could create a calculated column with the sum of the previous values (you'll need to RANK them in some ways, because DAX does not have the concept of "previous row") and then choose with a FILTER the row where the new value is higher than the value in PA.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.