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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
askpbiuser
Helper I
Helper I

DAX Previous value from measure

Hi All,

I have two fact tables connected with a dimension table. The fact tables have a many-to-one relationship with Dimension.
I have the below fields displayed in a table in Power BI visual.

askpbiuser_1-1714500131706.png

I have category coming from Dim table
Date,Weeks, A (measure), Cumulative sum of A are fields and measures created using Fact Table 1.
Fact table 2 have values summed to make Measure B (Excel cell G above)
Cumulative sum of A - B gives difference of measure [Cumulative sum of A] - [B]. (Cell H)

All these measures are returning right values and fine so far.

nHowever,I am facing a challenge in calculating Calculation field (I) as its formula is MAX(Cumulative sum of A] - [B] - Previous result).
For example for row 2, result would be 0.
For row 3 result would be 0-0.
For row 4 result would be MAX(1147,83 -(0+0),0)= 1147,83
For row 5 result would be MAX(1066,57 -(1147,83+0+0),0)= 0

So its basically sum of previous rows result subtracted from current row. I tried creating in same way as I have done Cumulative sum of A measure, but since B is coming from Fact table 2 which does not have index, same concept doesn't apply there.

 I am a bit lost and could you please help with the DAX measure. 

Kindly let me know if something is unclear.

CategoryDateIndexWeeksACumulative sum of ABCumulative sum of A-BOutput
40212-10-2024 00:00124154,01154,01597,6000,00
4026-11-2024 00:00227501,22655,23669,5800,00
40221-11-2024 00:003291.2141.870721,801147,831147,83
40212-12-2024 00:004320,551.870803,201066,570


I have edited the post to include the sample data,
Output is the field which I am looking for.
Its formula as mentioned above is always MAX([Cumulative sum of A-B] - (previous row result).
So for example - considering row 3, MAX(1147,83 -(0+0),0)=1147,83, same is for all the rows as well. Row 1 just doesn't have any previous rows.

1 ACCEPTED SOLUTION

Thanks @lbendlin 

Hi, @askpbiuser 

Let's assume that your A, B, Cumulative sum of A, and Cumulative sum of A-B are all metrics. If not, you should adjust my DAX expression below appropriately.

I used the following DAX expression:

Output1 =
VAR _currIndex =
    SELECTEDVALUE ( 'Table'[Index] )
VAR _previousvalue =
    CALCULATE (
        [Measure of Cumulative sum of A-B],
        FILTER ( ALL ( 'Table' ), 'Table'[Index] < _currIndex )
    )
VAR _result = [Measure of Cumulative sum of A-B] - _previousvalue
RETURN
    SWITCH ( TRUE (), _result > 0, _result, 0 )

Here are the results:

vjianpengmsft_0-1715326155583.png

I've uploaded the PBIX file I used this time below.

 

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

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

It is impossible to do this in DAX because DAX does not support conditional accumulation.  The only way to do this is in Power Query via List.Accumulate.

 

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.

If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...

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

Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Thanks.
I have edited my post now to provide the Sample table and the Output I am looking for.

Thanks @lbendlin 

Hi, @askpbiuser 

Let's assume that your A, B, Cumulative sum of A, and Cumulative sum of A-B are all metrics. If not, you should adjust my DAX expression below appropriately.

I used the following DAX expression:

Output1 =
VAR _currIndex =
    SELECTEDVALUE ( 'Table'[Index] )
VAR _previousvalue =
    CALCULATE (
        [Measure of Cumulative sum of A-B],
        FILTER ( ALL ( 'Table' ), 'Table'[Index] < _currIndex )
    )
VAR _result = [Measure of Cumulative sum of A-B] - _previousvalue
RETURN
    SWITCH ( TRUE (), _result > 0, _result, 0 )

Here are the results:

vjianpengmsft_0-1715326155583.png

I've uploaded the PBIX file I used this time below.

 

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

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

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.