March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
I am trying to put dates in a column based on the contents of another column. Here is an example of the data:
Attribute | Hours | Date |
Column6 | 14 | |
Column10 | 10 | |
Column11 | 11 | |
Column13 | 9 | |
Column14 | 17 | |
Column17 | 2 | |
Column19 | 4 |
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.
Solved! Go to 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:
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:
Awesome! That was the trick! Thank You so much!
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
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).
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
132 | |
90 | |
88 | |
64 | |
58 |
User | Count |
---|---|
203 | |
141 | |
107 | |
73 | |
70 |