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

Can't use DATEDIFF in ADDCOLUMNS

Hello! I have a table of retailers including a column with a date of creation [CreatedDate]. My goal is to find the difference between each creation date and a variable 'firstDay' which was calculated in advance. I created a column containing only firstDay in each row...

 

VAR tableWithFD = ADDCOLUMNS( ALL('DB Retailers'), "firstDay", firstDay)
 
... then I implemented this:
 
VAR tableWithDelta = ADDCOLUMNS( tableWithFD, "DiffCD", DATEDIFF( [CreatedDate], [firstDay], DAY))
 
And I got the wrong answer. I know it because I got the first CreatedDate with FIRSTNONBLANK and SELECTCOLUMNS and it was 2015/5/6, firstDay = 2020/11/1. But the first value in [DiffCD], result, equals -87. I don't know why CreatedDate changed. There are no missing values [CreatedDate]. 
 
I tried to calculate DATEDIFF from [CreatedDate] to TODAY() in order to subtract DATEDIFF([firstDAY], TODAY()) :
 
VAR tableWithDelta = ADDCOLUMNS( tableWithFD, "DiffFT", DATEDIFF( [fstD], TODAY(), DAY), "DiffCT", DATEDIFF( [CreatedDate], TODAY(), DAY))
 
 The first non-blank value in [DiffCT] was 15. CreatedDate equals 2015/5/6 in the first row! The difference should be about 2000 days.  I have no idea why it doesn't work.  Any ideas? 
 
1 ACCEPTED SOLUTION
Anonymous
Not applicable

PROBLEM IS SOLVED. 

 

The only reason why I couldn't get the right answer was that the result columns I got with SELECTCOLUMNS was sorted in the opposite way - the result of DATEDIFF for the first values in column (FIRSNONBLANK) I can get only with LASTNONBLANK. 

 

I think this is a topic for hot discussion (perhaps): "Why SELECTCOLUMNS sorts columns?"

 

Anyway, thank you for your help @PhilipTreacy .

View solution in original post

3 REPLIES 3
PhilipTreacy
Super User
Super User

Hi @Anonymous 

Can you please post your data so we can see what it is you are working with.

Regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Anonymous
Not applicable

// Create a dummy column
VAR tableWithFD = ADDCOLUMNS( ALL('DB Retailers'), "fstD", firstDateThisMonth)
 
// Just get first values 
VAR fstDate = FIRSTNONBLANK(SELECTCOLUMNS(tableWithFD, "FD", [fstD]), 1)
VAR fstDateShow = FORMAT(fstDate, "YYYY MM DD")

VAR CDate = FIRSTNONBLANK(SELECTCOLUMNS( tableWithFD, "CDproof", [NewCD]), 1)
VAR CDateShow = FORMAT(CDate, "YYYY MM DD")
 
// Create a column with differences
VAR tableWithDelta = ADDCOLUMNS( tableWithFD, "DaysToFirst", DATEDIFF( [CreatedDate], [fstD], DAY))
 
2021-02-11_02-42-43.png

 
Anonymous
Not applicable

PROBLEM IS SOLVED. 

 

The only reason why I couldn't get the right answer was that the result columns I got with SELECTCOLUMNS was sorted in the opposite way - the result of DATEDIFF for the first values in column (FIRSNONBLANK) I can get only with LASTNONBLANK. 

 

I think this is a topic for hot discussion (perhaps): "Why SELECTCOLUMNS sorts columns?"

 

Anyway, thank you for your help @PhilipTreacy .

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.

Top Solution Authors