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
dianecrz
Frequent Visitor

How to calculate the difference of the sum over the same column when filtered by max and min dates

I need some help building a formula in DAX. I want to calculate the amount sold between the most recent date and the oldest one. The first part of my statement is working but I am having trouble calculating the difference between dates. The numbers I see as results are close to what was expected but still don’t match exactly. I think filters may be affecting my calculations or something similar. I tried removing the KEEPFILTERS function and using FILTER instead but it also didn’t work.

 

Delta_Qty = VAR MAXDATE = MAX(Table1[SNAPSHOT_DATE])

 

VAR MINDATE = MIN(Table1[SNAPSHOT_DATE])

 

RETURN  IF ( HASONEVALUE (Tabl1e[DATE]),SUM(Table1[QUANTITY]), (CALCULATE (SUM(Table1[QUANTITY]), KEEPFILTERS(Table1[DATE]= MAXDATE)) - (CALCULATE (SUM(Table1[QUANTITY]), KEEPFILTERS(Table1[DATE]= MINDATE)))))

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

Hi @dianecrz ,

 

I think you can try this code to create a measure.

Delta_Qty =
VAR MAXDATE =
    MAX ( Table1[SNAPSHOT_DATE] )
VAR MINDATE =
    MIN ( Table1[SNAPSHOT_DATE] )
RETURN
    IF (
        HASONEVALUE ( Table1[SNAPSHOT_DATE] ),
        SUM ( Table1[QUANTITY] ),
        (
            CALCULATE (
                SUM ( Table1[QUANTITY] ),
                FILTER ( Table1, Table1[SNAPSHOT_DATE] = MAXDATE )
            )
                - (
                    CALCULATE (
                        SUM ( Table1[QUANTITY] ),
                        FILTER ( Table1, Table1[SNAPSHOT_DATE] = MINDATE )
                    )
                )
        )
    )

 Result is as below.

RicoZhou_0-1675755771073.png

 

Best Regards,
Rico Zhou

 

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
dianecrz
Frequent Visitor

@Greg_Deckler @v-rzhou-msft @

 

First, thank you for your detailed answers. I apologize for posting this issue without properly documenting my problem, it was my first time publishing in the community. 

 

I tried the measure suggested but I am still facing one error. This is what I am using: 

 

Diff = 
IF (
    HASONEVALUE ( Table[Snapshot_Date] ),
    ( ( SUM(Table[Orders]) ) ),
    CALCULATE (
            ( SUM(Table[Orders])),
            FILTER (
                Table,
                Table[Snapshot_Date] = MIN ( ( Table[Snapshot_Date] ) )
            )
    ) 
        - CALCULATE (
        ( SUM(Table[Orders])),
        FILTER (
            Table,
            Table[Snapshot_Date] = MAX ( ( Table[Snapshot_Date] ) )
        )
    )
        ) ​

 

The screenshot below shows that my Delta works when calculating the difference for the selected dates and rows below.

 

DeltaWorking.PNG

However, there are cases where the measure is not working, specifically when we have 0 for the maximum date and a positive value > 0 for the minimum date. Example below:

Delta_Issue_PBI.PNG


For the highlighted row, the delta should be -0.1 instead. All other rows in this matrix are making sense. 

Can someone please help me out with this issue? 

 

Sincerely, 

Diane

 

 

v-rzhou-msft
Community Support
Community Support

Hi @dianecrz ,

 

I think you can try this code to create a measure.

Delta_Qty =
VAR MAXDATE =
    MAX ( Table1[SNAPSHOT_DATE] )
VAR MINDATE =
    MIN ( Table1[SNAPSHOT_DATE] )
RETURN
    IF (
        HASONEVALUE ( Table1[SNAPSHOT_DATE] ),
        SUM ( Table1[QUANTITY] ),
        (
            CALCULATE (
                SUM ( Table1[QUANTITY] ),
                FILTER ( Table1, Table1[SNAPSHOT_DATE] = MAXDATE )
            )
                - (
                    CALCULATE (
                        SUM ( Table1[QUANTITY] ),
                        FILTER ( Table1, Table1[SNAPSHOT_DATE] = MINDATE )
                    )
                )
        )
    )

 Result is as below.

RicoZhou_0-1675755771073.png

 

Best Regards,
Rico Zhou

 

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

Greg_Deckler
Super User
Super User

@dianecrz Sorry, having trouble following, can you post sample data as text and expected output?
Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.


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