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
nleuck
Post Patron
Post Patron

Custom Column in Power Query Editor

Hello All,

 

So I haven't found much on how to deal with nulls in Custom Column formula and could use some help. I have two Date/Time columns: CreatedDateTime and ResolvedDateTime. I'm trying to find the duration between these columns, but I have null values when there isn't a ResolvedDateTime. I could easily replace those null with the current date/time, but I don't want to show to ResolvedDateTime if it hasn't actually been resolved.

 

ResolvedTime Custom Column formula:

 

=Duration.ToText(if [ResolvedDateTime] = null then

Date.Time.LocalNow - [CreatedDateTime] else

[ResolvedDateTime] - [CreatedDateTime])

 

The issue is I'm getting errors for the rows where ResolvedDateTime is null. I thought the formula would take care of that.

 

Any ideas?

 

Thanks!

1 ACCEPTED SOLUTION

Hi @nleuck ,

 

It's not the same formula if you check, you are not opening and closing brackets after the DateTime.LocalNow

 

Add the brackets and check if the error is gone, if not please check the error code on the bottom and tell me the result.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

11 REPLIES 11
Mariusz
Community Champion
Community Champion

Hi @nleuck 

 

Try this, if you don't want to calculate if null

try Duration.ToText( [ResolvedDateTime] - [CreatedDateTime] ) otherwise null

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

@Mariusz 

 

I don't want them to be null. If there isn't a ResolvedDateTime that means the issue hasn't been resolved yet. I want to get the duration still. For example, if an issue was created 4/23/2020 4:00pm and hasn't been resolved, I want to know the duration between created date and current data/time.

Hi  @nleuck ,

 

try the following custom column:

if [ResolvedDateTime] = null then DateTime.LocalNow() - [CreatedDateTime] else [ResolvedDateTime]-[CreatedDateTime]

Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



@MFelix 

 

That is the formula I'm using except mine is inside Duration.ToText().

Hi @nleuck ,

 

It's not the same formula if you check, you are not opening and closing brackets after the DateTime.LocalNow

 

Add the brackets and check if the error is gone, if not please check the error code on the bottom and tell me the result.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



@MFelix 

 

I see that now. I didn't realize the () made a difference. Is there a way to Round the seconds up though? I'm getting some weird results. Example: 3.12:40:18.1204227

 

Thanks again for your help!

Hi  @nleuck ,

 

try the following code:

Text.BeforeDelimiter(

Duration.ToText(if [ResolvedDateTime] = null then DateTime.LocalNow() - [CreatedDateTime] else [ResolvedDateTime]-[CreatedDateTime]), ".",1)

 

Then convert column to duration format


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



@MFelix 

 

Still gives me decmial for seconds. It only does it for those that the ResolvedDateTime is null and I'm using DateTime.LocalNow() - [CreatedDateTime]

Hi @nleuck ,

 

On the test I made it took out decimals from the ones that were nulls. Can you share the complete formula? Not doubting you but want to check the syntax.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



@MFelix 

 

Here is the formula:

 

Text.BeforeDelimiter(Duration.ToText(if [ResolvedDateTime] = null then
DateTime.LocalNow() - [CreatedDateTime] else
[ResolvedDateTime] - [CreatedDateTime]), ".", 1)

 

There are 4 rows where it's still giving me decimals.

 

resolved time format.png

Hi @nleuck ,

 

Believe that the issue is related with the number below one day you need to redo your column to:

 

if Duration.Days( if [ResolvedDateTime] = null then
DateTime.LocalNow() - [CreatedDateTime] else
[ResolvedDateTime] - [CreatedDateTime] ) <1
then
Text.BeforeDelimiter(Duration.ToText(if [ResolvedDateTime] = null then
DateTime.LocalNow() - [CreatedDateTime] else
[ResolvedDateTime] - [CreatedDateTime]), ".", 0) 
else
Text.BeforeDelimiter(Duration.ToText(if [ResolvedDateTime] = null then
DateTime.LocalNow() - [CreatedDateTime] else
[ResolvedDateTime] - [CreatedDateTime]), ".", 1)

Should Work as expected.

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



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.