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
Anonymous
Not applicable

DAX Measure and Column Difference between previous date

Hello,

 

I have the below Dataset. And I need both measure and calculated column which calculates the difference as in the below example. I am having problems with the cases when there is gap between dates. Please help.


Date;                ID            Amount            Difference(!)
02/01/01          1                100                     100

06/01/01          2                   5                        5

08/01/01          1                150                       50

09/01/01          2               1500                   1450

16/01/99        1                151                        1

 

Thank you

1 ACCEPTED SOLUTION
Anonymous
Not applicable

I was able to find a solution.

Column =

var rowdate = Sheet[Date]
var prevdate =
CALCULATE(MAX(Sheet[Date]);FILTER(ALLEXCEPT(Sheet;Sheet[ID]);Sheet[Date] < rowdate))
return
IF(prevdate=BLANK();Sheet[amount];CALCULATE(SUM(Sheet[amount]);FILTER(ALLEXCEPT(Sheet;Sheet[ID]);Sheet[Date]=rowdate)))-CALCULATE(SUM(Sheet[amount]);FILTER(ALLEXCEPT(Sheet;Sheet[ID]);Sheet[Date]=prevdate))

Measure =
var rowdate = MAX(Sheet[Date])
var prevdate =
CALCULATE(MAX(Sheet[Date]);FILTER(ALLEXCEPT(Sheet;Sheet[ID]);Sheet[Date] < rowdate))
return
IF(prevdate=BLANK();SUM(Sheet[amount]);CALCULATE(SUM(Sheet[amount]);FILTER(ALLEXCEPT(Sheet;Sheet[ID]);Sheet[Date]=rowdate)))-CALCULATE(SUM(Sheet[amount]);FILTER(ALLEXCEPT(Sheet;Sheet[ID]);Sheet[Date]=prevdate))

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

I was able to find a solution.

Column =

var rowdate = Sheet[Date]
var prevdate =
CALCULATE(MAX(Sheet[Date]);FILTER(ALLEXCEPT(Sheet;Sheet[ID]);Sheet[Date] < rowdate))
return
IF(prevdate=BLANK();Sheet[amount];CALCULATE(SUM(Sheet[amount]);FILTER(ALLEXCEPT(Sheet;Sheet[ID]);Sheet[Date]=rowdate)))-CALCULATE(SUM(Sheet[amount]);FILTER(ALLEXCEPT(Sheet;Sheet[ID]);Sheet[Date]=prevdate))

Measure =
var rowdate = MAX(Sheet[Date])
var prevdate =
CALCULATE(MAX(Sheet[Date]);FILTER(ALLEXCEPT(Sheet;Sheet[ID]);Sheet[Date] < rowdate))
return
IF(prevdate=BLANK();SUM(Sheet[amount]);CALCULATE(SUM(Sheet[amount]);FILTER(ALLEXCEPT(Sheet;Sheet[ID]);Sheet[Date]=rowdate)))-CALCULATE(SUM(Sheet[amount]);FILTER(ALLEXCEPT(Sheet;Sheet[ID]);Sheet[Date]=prevdate))
v-frfei-msft
Community Support
Community Support

Hi @Anonymous ,

 

To create a measure as below.

difference =
VAR a =
    MAX ( 'Table'[date] )
VAR d =
    CALCULATE (
        MAX ( 'Table'[date] ),
        FILTER ( ALL ( 'Table' ), 'Table'[date] < a ),
        VALUES ( 'Table'[id] )
    )
VAR c =
    CALCULATE (
        SUM ( 'Table'[amount] ),
        FILTER ( ALL ( 'Table' ), 'Table'[date] = d ),
        VALUES ( 'Table'[id] )
    )
RETURN
    SUM ( 'Table'[amount] ) - c

Capture.PNG

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Anonymous
Not applicable

@Anonymous How is difference calculated?

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.