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

Calculated Column: DATEDIFF with filter - not working + getting negative numbers

Hi folks,

I'm hoping someone can help me. I'm trying to create a calculated column to show the time elapsed, in days, between when a piece of documentation was created [Created Date], and TODAY() - but only if the [Status] of the documentation is "Draft". If the [Status]<> "Draft", the calculation should return a blank. My column formula and some sample data is below. I have two challenges: My calculation below is returning results regardless of the [Status], and I'm also getting some (not all) negative numbers which I don't understand, looking at the [Created Date] field:

 

 

 

 

DRAFT_TIME = IF
(
CONTAINS('COMBINED_Articles','COMBINED_Articles'[Status],"Draft"),
DATEDIFF
(
[Created Date],TODAY(), DAY) 
)

 

 

 

 

 

SC1.png

 

SC2.png

Any help would really be appreciated!

13 REPLIES 13
smpa01
Super User
Super User

sxaz.PNG

 

@Anonymous CONTAINS is not doing the job you thought it does. CONTAINS check if at least 1 row of the table is meeting the condition. CONTAINSTRING is your guy.

sxaz.PNG

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Greg_Deckler
Super User
Super User

I would just do this:

 

DRAFT_TIME = 
  IF
  (
    CONTAINS('COMBINED_Articles','COMBINED_Articles'[Status],"Draft"),
    ([Created Date] - TODAY()) * 1. ,
    BLANK()
  )

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Thanks so much for the quick feedback @Greg_Deckler  - after your reply I realied that what I'm actually trying to create is a calculated column, not a measure - I do apologise. I've editied the original post accordingly...

I think that calculation should work in a column.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Maybe I'm misunderstanding the issue, and simply doing something wrong?

 

Error1.png

 

Error2.png

It looks for all the world that you are creating a measure. You want to create a column right?


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Apologies again, I had gotten mixed up (that'll teach me for rushing) and was indeed trying to create a measure. I've sucessfully used your code to create a calculated column (thanks again), but 1) I'm getting negative numbers, and 2) it's not returning blank results for the non-'draft' articles:

 

Prob1.PNG

Right, easy to fix. I was using your code for the IF true/false statement, I would do it this way:

 

DRAFT_TIME = 
  IF
  (
    COMBINED_Articles'[Status] <> "Draft",
    (TODAY() - [Created Date]) * 1. ,
    BLANK()
  )

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

That works! Now the LAST problem is that some results are coming up in negatives - any idea why that might be happening?

 

Prob2.png

So, that's fixable. Couple different ways of doing it but one method is:

 

DRAFT_TIME = 
  VAR __Table = { [Created Date], TODAY() }
  VAR __DateMax = MAXX(__Table,[Value])
  VAR __DateMin = MINX(__Table,[Value])
RETURN
  IF
  (
    COMBINED_Articles'[Status] <> "Draft",
    (__DateMax - __DateMin) * 1. ,
    BLANK()
  )

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

While I certainly appreciate your response and your time, I'm not sure that this has changed too much 🙂

 

Prob3.png

Change your data type on your Created Date column to be Date Time column instead of text. If it was a Date or Date Time, it would be in italics in your screen shot.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Because on the negative value rows the created date > today.
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

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.