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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Help to calculate the subtraction of two dates from the same column and with conditions

Hello people,

I am new to Power BI and I have been looking for several solutions for what I am looking for and I have not been able to find the solution.

I have a database, that, in summary, has several columns as seen in the image:

DBB_0-1674134259855.png

I want to generate a column that counts the minutes between two dates, as long as they share the same place "PLACE" and "PLATAFORMA". And the dates that want to subtract are the date after the current one. So not all rows will have a value.

DBB_1-1674134313713.png

The idea is to generate this new column in DAX.

1 ACCEPTED SOLUTION
TomasAndersson
Solution Sage
Solution Sage

Hi!

You can use this calculated column:

 

Minutes = 
VAR __PLACE = 'Table'[PLACE]  //store variables for this row's place, platform and time
VAR __PLATAFORMA = 'Table'[PLATAFORMA]
VAR __DATEANDTIME = 'Table'[DATE AND TIME]
VAR __NextDate =
CALCULATE(
    min('Table'[DATE AND TIME]),  //find the earliest time
    FILTER(ALL('Table'),  //of rows with same place and platform and a time after this row's time
        'Table'[PLACE] = __PLACE &&
        'Table'[PLATAFORMA] = __PLATAFORMA &&
        DATEDIFF( __DATEANDTIME, 'Table'[DATE AND TIME], MINUTE) > 0
    )
)
return
if(  //if there is a next date, return the time difference in minutes, else do nothing
    NOT ISBLANK(__NextDate),
    DATEDIFF( __DATEANDTIME, __NextDate, MINUTE)
)

 

TomasAndersson_0-1674138588503.png

 


Good luck!

View solution in original post

7 REPLIES 7
FreemanZ
Super User
Super User

hi @Anonymous 

you may add a calculated column like this:

DIFF = 
VAR _datetimenext =
MINX(
    FILTER(
        TableName,
        TableName[PLACE] = EARLIER(TableName[PLACE])
            &&TableName[PLATAFORMA] = EARLIER(TableName[PLATAFORMA])
            &&TableName[DATE AND TIME]>EARLIER(TableName[DATE AND TIME])
    ),
    TableName[DATE AND TIME]
)
VAR _diff = DATEDIFF([DATE AND TIME], _datetimenext, MINUTE)
RETURN
IF(_datetimenext<>BLANK(), _diff)

FreemanZ_0-1674142319104.png

Anonymous
Not applicable

It's no working for me.

I'm using other names for columns' titles. 

My actual columns are these:

DBB_0-1674141866500.png

I only need the results for "PUNTA ALVEAR", so this value could be constant.

The code I wrote, changing the one you told me is:

DBB_1-1674141954422.png

And appears that mistake

 

Can you help me?

The DATEDIFF at the end has "minutes" as its third argument. It should be MINUTE.

Anonymous
Not applicable

I changed that part and it's going to appear the same mistake 😭

DBB_0-1674142692220.png

 

EDIT: I found de mistake, line 10, there was to brackets

Ah, saw that now as well. Hope it works now!

Anonymous
Not applicable

Yes, it's working perfectly! Thank you so much!!!!

TomasAndersson
Solution Sage
Solution Sage

Hi!

You can use this calculated column:

 

Minutes = 
VAR __PLACE = 'Table'[PLACE]  //store variables for this row's place, platform and time
VAR __PLATAFORMA = 'Table'[PLATAFORMA]
VAR __DATEANDTIME = 'Table'[DATE AND TIME]
VAR __NextDate =
CALCULATE(
    min('Table'[DATE AND TIME]),  //find the earliest time
    FILTER(ALL('Table'),  //of rows with same place and platform and a time after this row's time
        'Table'[PLACE] = __PLACE &&
        'Table'[PLATAFORMA] = __PLATAFORMA &&
        DATEDIFF( __DATEANDTIME, 'Table'[DATE AND TIME], MINUTE) > 0
    )
)
return
if(  //if there is a next date, return the time difference in minutes, else do nothing
    NOT ISBLANK(__NextDate),
    DATEDIFF( __DATEANDTIME, __NextDate, MINUTE)
)

 

TomasAndersson_0-1674138588503.png

 


Good luck!

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.