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

count days between two dates

How can I count the days between two dates which are defined as whole number i tabular and not as date in a dax measure?

For example:

firts date 20201201

last date 20210201

 

firstdate-lastdate= 63 days

 

 

I can't change the data type in tabular , there are other measures using this column.

1 ACCEPTED SOLUTION
selimovd
Super User
Super User

Hello @Anonymous ,

 

you can transform the integer into a date and then calculate the difference with the DATEDIFF function:

Date Diff =
VAR vFistDate = DATE( LEFT( myTable[first date], 4 ), MID( myTable[first date], 5, 2 ), RIGHT( myTable[first date], 2 ) )
VAR vLastDate = DATE( LEFT( myTable[last date], 4 ), MID( myTable[last date], 5, 2 ), RIGHT( myTable[last date], 2 ) )
VAR vDiff = DATEDIFF( vFistDate, vLastDate, DAY )
RETURN
    vDiff

 

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
 
Best regards
Denis
 

 

View solution in original post

3 REPLIES 3
selimovd
Super User
Super User

Hello @Anonymous ,

 

you can transform the integer into a date and then calculate the difference with the DATEDIFF function:

Date Diff =
VAR vFistDate = DATE( LEFT( myTable[first date], 4 ), MID( myTable[first date], 5, 2 ), RIGHT( myTable[first date], 2 ) )
VAR vLastDate = DATE( LEFT( myTable[last date], 4 ), MID( myTable[last date], 5, 2 ), RIGHT( myTable[last date], 2 ) )
VAR vDiff = DATEDIFF( vFistDate, vLastDate, DAY )
RETURN
    vDiff

 

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
 
Best regards
Denis
 

 

Anonymous
Not applicable

Thank you @selimovd , I tested the measure and it works but not when the first and last day is the same day.

for example :

2019-05-14    2019-05-14    vDiff=0   (but it should be counted as 1 day, vDiff=1)

 

 

Hey @Anonymous ,

 

mathematically there is not a single day difference between today and today.

But if you want to count that as one day, then you should add +1 for all values to be consistent.

 

Then try:

Date Diff =
VAR vFistDate = DATE( LEFT( myTable[first date], 4 ), MID( myTable[first date], 5, 2 ), RIGHT( myTable[first date], 2 ) )
VAR vLastDate = DATE( LEFT( myTable[last date], 4 ), MID( myTable[last date], 5, 2 ), RIGHT( myTable[last date], 2 ) )
VAR vDiff = DATEDIFF( vFistDate, vLastDate, DAY )
RETURN
    vDiff + 1

 

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
 
Best regards
Denis
 

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.