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
e_vera
Helper I
Helper I

calculate sum by dates

Hi,

I am new to DAX and have absolute minimal idea about formulas. Could someone please help me construct a measure that computes for the difference (amount) between the earliest of status 1 and the latest for status 2 filtered by ticket? In the below screenshot, for example, it should be:

Ticket 0001 > -120 (earliest of status FINE_OFFSET) + 50 (latest of status FINE) = 70 (correct answer for Ticket 0001)

Ticket 0002 > 0 + 150 = 150

attach.png

Thank you in advance!

1 ACCEPTED SOLUTION

Hi @e_vera ,

 

We can create a measure to meet your requirement:

 

Measure =
VAR FirstTimeForStatus1 =
    CALCULATE ( MIN ( 'Table'[Date/Time] ), 'Table'[Status] = "FINE_OFFSET" )
VAR LastTimeForStatus2 =
    CALCULATE ( MAX ( 'Table'[Date/Time] ), 'Table'[Status] = "FINE" )
RETURN
    CALCULATE (
        SUM ( 'Table'[Amount] ),
        'Table'[Status] = "FINE_OFFSET",
        'Table'[Date/Time] = FirstTimeForStatus1
    )
        + CALCULATE (
            SUM ( 'Table'[Amount] ),
            'Table'[Status] = "FINE",
            'Table'[Date/Time] = LastTimeForStatus2
        )

 

5.jpg

 


Best regards,

 

Community Support Team _ Dong 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

5 REPLIES 5
parry2k
Super User
Super User

@e_vera try this measure

 

Measure 1 = 
VAR __fineOffset = 
    CALCULATE ( 
        MAX ( Fine[Amount] ), 
        TOPN( 1, FILTER( ALL ( Fine ), Fine[Ticket] = MAX ( Fine[Ticket] ) &&    Fine[Flag] = "Fine_Offset" ), Fine[Date/Time], ASC ) 
    )
VAR __fine = 
    CALCULATE ( 
        MAX ( Fine[Amount] ), 
        TOPN( 1, FILTER(   ALL ( Fine ), Fine[Ticket] = MAX ( Fine[Ticket] ) &&    Fine[Flag] = "Fine" ), Fine[Date/Time], DESC ) 
   )

RETURN 
__fineOffset + __fine


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

hi parry2k,

Thank you so much for your suggestion. Unfortunately, the formula is not returning any value. I've tried removing the second var as well (keeping only fineOffset) but it's still blank.

Hi @e_vera ,

 

We can create a measure to meet your requirement:

 

Measure =
VAR FirstTimeForStatus1 =
    CALCULATE ( MIN ( 'Table'[Date/Time] ), 'Table'[Status] = "FINE_OFFSET" )
VAR LastTimeForStatus2 =
    CALCULATE ( MAX ( 'Table'[Date/Time] ), 'Table'[Status] = "FINE" )
RETURN
    CALCULATE (
        SUM ( 'Table'[Amount] ),
        'Table'[Status] = "FINE_OFFSET",
        'Table'[Date/Time] = FirstTimeForStatus1
    )
        + CALCULATE (
            SUM ( 'Table'[Amount] ),
            'Table'[Status] = "FINE",
            'Table'[Date/Time] = LastTimeForStatus2
        )

 

5.jpg

 


Best regards,

 

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you so much for this, it works now!

@e_vera how you are using it? Did you put Ticket column in the table visual and this measure? Share the screen shot. Do you have any filter or relationship that has impact. Provide more details.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

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.