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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
LFrench
Frequent Visitor

If then statement with embedded calculation


I feel like I am close, and I can make each column work seperately, but now I need them to work together if possible. Where is my formula going wrong?

 

I have two columns, [Work Start Date] and [Work End Date]. I am trying to create a new column that displays the following whole number:

 

If [Work End Date] has a date, then [Work End Date] - [Work Start Date] + 1
If [Work End Date] is "null" then Today's Date - [Work Start Date] + 1

Here is what I have so far, but I keep getting an error message. Thanks for any help you can provide.

= Table.AddColumn("Days in Position", each if [Work End Date] <> null then Duration.Days([Work End Date] - [Work Start Date]) +1, Int64.Type)) else Duration.Days(Date.From(DateTime.LocalNow())) - [Work Start Date]) +1, Int64.Type))

 

1 ACCEPTED SOLUTION
SundarRaj
Super User
Super User

Hi @LFrench , another solution that you could look at. I'll attach the images and the M code used.

SundarRaj_0-1745405476327.png

SundarRaj_1-1745405499484.png

Here's the code:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"StartDate", type datetime}, {"EndDate", type datetime}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if Date.From([StartDate]) is date then Duration.Days([StartDate] - [EndDate]) + 1
else if [EndDate] = null then Duration.Days(Date.From(DateTime.FixedLocalNow()) - [StartDate]) + 1 else null)
in
#"Added Custom"

Sundar Rajagopalan

View solution in original post

6 REPLIES 6
SundarRaj
Super User
Super User

Hi @LFrench , another solution that you could look at. I'll attach the images and the M code used.

SundarRaj_0-1745405476327.png

SundarRaj_1-1745405499484.png

Here's the code:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"StartDate", type datetime}, {"EndDate", type datetime}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if Date.From([StartDate]) is date then Duration.Days([StartDate] - [EndDate]) + 1
else if [EndDate] = null then Duration.Days(Date.From(DateTime.FixedLocalNow()) - [StartDate]) + 1 else null)
in
#"Added Custom"

Sundar Rajagopalan
v-dineshya
Community Support
Community Support

Hi @LFrench ,

Thank you for reaching out to the Microsoft Community Forum.

 

Can you please try this Power Query M code.

= Table.AddColumn(Source, "Days in Position", each
if [Work End Date] <> null then
Duration.Days([Work End Date] - [Work Start Date]) + 1
else
Duration.Days(Date.From(DateTime.LocalNow()) - [Work Start Date]) + 1,
Int64.Type)

 

If my response has resolved your query, please mark it as the Accepted Solution to assist others. Additionally, a 'Kudos' would be appreciated if you found my response helpful.

Thank you

 

 

Hi @LFrench ,

If you find this post helpful, please mark it as an "Accept as Solution" and consider giving a KUDOS. Feel free to reach out if you need further assistance.
Thanks and Regards

Hi @LFrench ,

If you find this post helpful, please mark it as an "Accept as Solution" and consider giving a KUDOS. Feel free to reach out if you need further assistance.
Thanks and Regards

Hi @LFrench ,

If you find this post helpful, please mark it as an "Accept as Solution" and consider giving a KUDOS. Feel free to reach out if you need further assistance.
Thanks and Regards

lbendlin
Super User
Super User

= Table.AddColumn("Days in Position", 
each Duration.Days(([Work End Date]??Date.From(DateTime.LocalNow())) - [Work Start Date]) +1,
 Int64.Type)

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon & SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.