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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
petermb72
Helper IV
Helper IV

Power Query Editor Nested if statements to put dates in a column

I am trying to put dates in a column based on the contents of another column.  Here is an example of the data:

 

AttributeHoursDate
Column614 
Column1010 

Column11

11 

Column13

9 
Column1417 
Column172 
Column194 

What I want is to have todays date put in the date column for Column19, yesterdays date put in the column for column17, daybefore date put in column14.

 

I thought this would work for this proceess but I am getting an error after the first date gets inserted:

 

if [Attribute] = "Column19" then DateTime.LocalNow()
else if [Attribute] = "Column17" then DateTime.AddDays(DateTime.LocalNow(), -1)
else if [Attribute] = "Column14" then DateTime.AddDays(DateTime.LocalNow(), -2)
else if [Attribute] = "Column13" then DateTime.AddDays(DateTime.LocalNow(), -3)
else if [Attribute] = "Column11" then DateTime.AddDays(DateTime.LocalNow(), -4)
else if [Attribute] = "Column10" then DateTime.AddDays(DateTime.LocalNow(), -5)
else if [Attribute] = "Column6" then DateTime.AddDays(DateTime.LocalNow(), -6)
else null

 

I get an error that DateTime.AddDays wasn't recognized.  I am also not sure if this is the right formula to use.  Any help would be appreciated.

1 ACCEPTED SOLUTION

Hi @petermb72 ,

Sorry about that, i've made a mistake in my Code, but please, use the bellow M code:

if [Attribute] = "Column19" then Date.From(DateTime.LocalNow())  // Today's date
else if [Attribute] = "Column17" then Date.AddDays(Date.From(DateTime.LocalNow()), -1)  // Yesterday's date
else if [Attribute] = "Column14" then Date.AddDays(Date.From(DateTime.LocalNow()), -2)  // Day before yesterday
else if [Attribute] = "Column13" then Date.AddDays(Date.From(DateTime.LocalNow()), -3)
else if [Attribute] = "Column11" then Date.AddDays(Date.From(DateTime.LocalNow()), -4)
else if [Attribute] = "Column10" then Date.AddDays(Date.From(DateTime.LocalNow()), -5)
else if [Attribute] = "Column6" then Date.AddDays(Date.From(DateTime.LocalNow()), -6)
else null

 

Your output will look like this:

Bibiano_Geraldo_0-1734015225175.png

 

View solution in original post

6 REPLIES 6
Bibiano_Geraldo
Super User
Super User

Hi @petermb72 ,

Please try this updated M code:

if [Attribute] = "Column19" then Date.From(DateTime.LocalNow())  // Today's date
else if [Attribute] = "Column17" then Date.From(DateTime.AddDays(DateTime.LocalNow(), -1))  // Yesterday's date
else if [Attribute] = "Column14" then Date.From(DateTime.AddDays(DateTime.LocalNow(), -2))  // Day before yesterday
else if [Attribute] = "Column13" then Date.From(DateTime.AddDays(DateTime.LocalNow(), -3))
else if [Attribute] = "Column11" then Date.From(DateTime.AddDays(DateTime.LocalNow(), -4))
else if [Attribute] = "Column10" then Date.From(DateTime.AddDays(DateTime.LocalNow(), -5))
else if [Attribute] = "Column6" then Date.From(DateTime.AddDays(DateTime.LocalNow(), -6))
else null

I am getting the following error when I put your formula in:

 

Expression.Error: THe name 'DateTime.AddDays' wasn't recognized. Make sure it's spelled correctly.

Hi @petermb72 ,

Sorry about that, i've made a mistake in my Code, but please, use the bellow M code:

if [Attribute] = "Column19" then Date.From(DateTime.LocalNow())  // Today's date
else if [Attribute] = "Column17" then Date.AddDays(Date.From(DateTime.LocalNow()), -1)  // Yesterday's date
else if [Attribute] = "Column14" then Date.AddDays(Date.From(DateTime.LocalNow()), -2)  // Day before yesterday
else if [Attribute] = "Column13" then Date.AddDays(Date.From(DateTime.LocalNow()), -3)
else if [Attribute] = "Column11" then Date.AddDays(Date.From(DateTime.LocalNow()), -4)
else if [Attribute] = "Column10" then Date.AddDays(Date.From(DateTime.LocalNow()), -5)
else if [Attribute] = "Column6" then Date.AddDays(Date.From(DateTime.LocalNow()), -6)
else null

 

Your output will look like this:

Bibiano_Geraldo_0-1734015225175.png

 

Awesome!  That was the trick! Thank You so much!

VijayP
Super User
Super User

@petermb72 

It is most important to know why you wanted to do instead of what you wanted to do , it helps us to give right answer




Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
MY Blog || My YouTube Channel || Connect with me on Linkedin || My Latest Data Story - Ageing Analysis

Proud to be a Super User!


I want to have the date for each of the items in the table going back for the week.  I bring in the data everyday and it is a rolling 7 day period so the date would change for each line every day (forward one day).

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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