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

DATEDIFF as DAX Measure

Hello all,

 

I am trying to create a measure that calculates the difference between 2 time columns (hh:mm:ss format) and gives a result as a decimal. DATEDIFF works as a calculated column, but I cannot figure out how to translate to a DAX Measure. My report is saying there is too much space being taken up, so I want to change these columns to measures.

 

Please help!

1 ACCEPTED SOLUTION

You could try either of these measure expressions

 

TimeDiff = SUMX(Table, Table[TimeColumn1] - Table[TimeColumn2]

 

or


TimeDiff = SUMX(Table, DATEDIFF(Table[TimeColumn1], Table[TimeColumn2], MINUTE))

 

If you make a table visual with both time columns, you should see the difference when you add the measure.  Or you could use the aggregate, maybe switching to AVERAGEX, MAXX, etc. depending on what you are looking for.

 

Also, there are probably more impactful ways to reduce the file size of your model.

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

7 REPLIES 7
amitchandak
Super User
Super User

@Anonymous ,

[Date1] -[Date2] will give in that format.

 

If you get in in second, minute or hour refer this for the duration of your choice

https://radacad.com/calculate-duration-in-days-hours-minutes-and-seconds-dynamically-in-power-bi-using-dax

https://www.pbiusergroup.com/communities/community-home/digestviewer/viewthread?GroupId=547&MessageKey=814a2cb4-3cca-4cd1-a620-c467adeaaaf6&CommunityKey=b35c8468-2fd8-4e1a-8429-322c39fe7110&tab=digestviewer

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here
Anonymous
Not applicable

The DAX measure does not give me the option to call the entire column, so I cannot use [Date 1] - [Date 2]. In this case I would need a function that allows me to subtract 2 columns that are in the date format.

@Anonymous , in Measure you need to

 

measure = max([Date1]) -min([Date2])

Take care row context while using. refer this blog how summarize and valuea has been used

 

https://community.powerbi.com/t5/Community-Blog/Decoding-Direct-Query-in-Power-BI-Part-2-Date-Difference-Across/ba-p/934397#M451 

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here
Anonymous
Not applicable

Hello, hoping you can read my last reply and provide some assistance please. I have a hard deadline for this project and I'm really trying to deliver it. Thank you!

You could try either of these measure expressions

 

TimeDiff = SUMX(Table, Table[TimeColumn1] - Table[TimeColumn2]

 

or


TimeDiff = SUMX(Table, DATEDIFF(Table[TimeColumn1], Table[TimeColumn2], MINUTE))

 

If you make a table visual with both time columns, you should see the difference when you add the measure.  Or you could use the aggregate, maybe switching to AVERAGEX, MAXX, etc. depending on what you are looking for.

 

Also, there are probably more impactful ways to reduce the file size of your model.

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Second measure worked using MAXX. Thank you very much!!!

 

Additionally, could you tell me what are the best ways to substantially reduce file size?

Anonymous
Not applicable

I tried that measure and when I put it in a table it only gives one value. I am trying to replicate the following column as a DAX measure:

duran-lm_1-1595341873660.png

 

 This was calculated using the following calculation:

 

Start Time Variance =
DATEDIFF('bv AFKO: Order Header Data PP Orders'[GSUZS_SIMP_TM: (GC) Scheduled Start (Time)] , 'bv AFKO: Order Header Data PP Orders'[GSUZI_SIMP_TM: (GC) Actual start time] , MINUTE) / 60
 
The columns in the calculation are the following:
 
 
 
sch.PNG
 
 
act.PNG

 

 

 

 

 

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!

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.