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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
mail2vjj
Helper III
Helper III

Date Difference between two random dates

Hi everyone,

 

I am trying to calculate the difference between two random dates.

 

I have a table with 2 columns 'buying dates' and 'selling dates' and another table with the Calendar on it.

 

I am trying to calculate the difference between two dates selected on the slicer, either using Buying Date and Selling Date.

 

The following is my Buying and Selling Date Table (You can create a calendar based on this table, using CALENDARAUTO).

 

BUYING DATESELLING DATE

18-Nov-154-May-17
23-Dec-1510-May-17
23-Dec-154-May-17
23-Dec-1514-Jun-17
23-Dec-154-May-17
23-Dec-154-May-17
4-Jan-164-May-17
4-Jan-164-May-17
13-Jan-164-May-17
9-Sep-164-May-17
9-Sep-164-May-17
17-Oct-1624-Apr-17
19-Oct-161-Nov-17
21-Oct-1610-Apr-17
21-Oct-1628-Aug-17
21-Oct-1611-Apr-17
24-Oct-1613-Oct-17
11-Nov-1624-Apr-17
9-Feb-171-Nov-17
21-Mar-1710-May-17
22-Mar-172-May-17
30-Mar-172-May-17
5-Apr-171-Jun-17
24-Apr-1715-May-17
2-May-179-May-17
2-May-1716-May-17
3-May-1712-Jul-17
8-May-1710-May-17
9-May-1721-Nov-17
10-May-1715-May-17
12-May-1726-May-17
12-May-171-Jun-17
15-May-1713-Jun-17
1-Jun-179-Jun-17
2-Jun-178-Jun-17
8-Jun-178-Jun-17
9-Jun-179-Nov-17
9-Jun-171-Nov-17
9-Jun-1713-Jun-17
13-Jun-1718-Jul-17
13-Jun-1718-Jul-17
14-Jun-1728-Jul-17
12-Jul-1725-Jul-17
19-Jul-1720-Jul-17
19-Jul-1725-Jul-17
31-Jul-173-Aug-17
28-Aug-1731-Aug-17
21-Sep-1713-Oct-17
21-Sep-1719-Oct-17
26-Oct-1727-Oct-17
26-Oct-1727-Oct-17
27-Oct-173-Nov-17
27-Oct-173-Nov-17
30-Oct-176-Dec-17
31-Oct-1727-Dec-17
1-Nov-171-Nov-17
1-Nov-172-Nov-17
3-Nov-179-Nov-17
6-Nov-1717-Nov-17
10-Nov-1713-Nov-17
21-Nov-1729-Nov-17
22-Nov-1730-Nov-17
23-Nov-1724-Nov-17
23-Nov-1719-Dec-17
23-Nov-1724-Nov-17
24-Nov-1728-Nov-17
24-Nov-1721-Dec-17
30-Nov-1726-Dec-17
5-Dec-1719-Dec-17
5-Dec-175-Dec-17
6-Dec-1719-Dec-17
13-Dec-1727-Dec-17
27-Dec-1728-Dec-17

 

These are what my 2 slicers look like:

The Buying Date is set to 'After' and the Selling Date is set to 'Before'

Screenshot (6).png

So what I want is depending upon the Buying and Selling dates selected in the slicer, I want to calculate the difference of days between those dates.

 

I have tried to use DATEDIFF but the formula asks for a specific date, so I tried to use the MIN and Max functions along with it but it did not really work.

 

So if you can come up with anything, please let me know.

 

Thank you,

 

Vishesh Jain

6 REPLIES 6
v-jianhe-msft
Resolver II
Resolver II

Hi,

 

May I know how is your issue going currently?

 

BR,

Henry 

 

Hi Henry,

 

Sorry I didn't get a change to look at your solution.


I am still working on it and will let you know if your solution works, or if I find one of my own.

 

Thank you,

 

Vishesh Jain

Hi,

 

Thank you for the notification.

 

BR,

Henry 

 

Hey Henry,

 

I tried your method but it doesn't seem to be working.

 

From whatever I know about PowerBI and DAX, your solution should have worked.

 

Here is the screen shot of my result.

 

Screenshot (9).png

 

Also, I am looking for a scalar value, not really planning to use this measure as a column in any of the tables.

 

I am still working on it and will let you know if I come up with a solution.

 

Anyways, thanks for your help and my apologies for not being able to reply sooner.

 

Thank you,

 

Vishesh Jain

Hi,

 

Thank you for your notification. 

 

v-jianhe-msft
Resolver II
Resolver II

Hi,

 

So you hope to calculate the date difference between two dates in slicer, right? To achieve it, we need to create two new tables (Buying date table and Selling date table), which contains all the dates from 18-Nov-15 to 27-Dec-17, 4-May-17 to 28-Dec-17, accordingly. Otherwise, the date can only be the one which exists in your original dataset.

 

Below are my steps:

  1. Create Buying Date table using DAX: (“Table1 “ is your original table )

Tablebuying = CALENDAR(FIRSTDATE(Table1[Buying Date]),LASTDATE(Table1[Selling Date]))

 

Similarly, create Selling Date table using

Tableselling = CALENDAR(FIRSTDATE(Table1[Selling Date]),LASTDATE(Table1[Selling Date]))

 

2. Make them as slicer, after/ before,etc.

1.PNG

3. Add a measure to calculate the date difference:

Measure 3 = DATEDIFF(FIRSTDATE(Tablebuying[Date]),LASTDATE(Tableselling[Date]),day)

 

4. When previewing, you could get result of date difference:

2.PNG

Best Regards,

Henry 

 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

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! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.