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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
MJEnnis
Resolver III
Resolver III

Replace date.time values in one column conditioned upon ID in another column

Due to a human data entry error that cannot be corrected at the source, I have to add one year to a date/time stamp for a about 10 events among 25,000. Fortunately, the events in question have a unique ID in the same table. When I try the following code, the date/times are not replaced in the new "replaced value" table. The [Date] column is only transformed from "date/time" data type to "any" (i.e., "ABC123") data type. 

 

= Table.ReplaceValue(#"Renamed Columns1", each [Date], each if [ID] = 36 then #datetime(2016, 02, 03, 0, 0, 0) else [Date], Replacer.ReplaceText,{"Date"})

 

I have also tried this method, also to no avail: 

 

= Table.ReplaceValue(#"Renamed Columns1", each [Date], each if [ID] = 36 then [Date] + #duration(365,0,0,0) else [Date], Replacer.ReplaceText,{"Date"})

 

Any suggestions for a quick fix?

 

1 ACCEPTED SOLUTION

Change your Replacer.ReplaceText with Replacer.ReplaceValue.

(I didn't test your duration addition)

= Table.ReplaceValue(#"Renamed Columns1", each [Date], each if [ID] = 36 then [Date] + #duration(365,0,0,0) else [Date], Replacer.ReplaceValue,{"Date"})

 

You will still run into issues with all of the types being reset to 'Any' with this method which is why I prefer the add column method.

 

See this post...

https://community.powerbi.com/t5/Desktop/Query-editor-replacing-values-based-on-another-column/td-p/...

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
chrome-9xf-Zagzel-B

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
KIfp67uy-Sr
Proud to be a Super User!PBI-Super-User-Rank-30x30-1x

View solution in original post

7 REPLIES 7
KNP
Super User
Super User

Sometimes it's easier and cleaner to do it with a new column and then remove the old. 

Try this...

let
  Source = Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText("i45WMjZT0lEyNtA3NNI3MjAyUIrVAYqZg8QMoWKGSrGxAA==", BinaryEncoding.Base64),
        Compression.Deflate
      )
    ),
    let
      _t = ((type nullable text) meta [Serialized.Text = true])
    in
      type table [id = _t, date = _t]
  ),
  #"Changed Type" = Table.TransformColumnTypes(Source, {{"id", Int64.Type}, {"date", type date}}),
  #"Added Custom" = Table.AddColumn(
    #"Changed Type",
    "Custom",
    each if [id] = 36 then Date.AddYears([date], 1) else [date]
  )
in
  #"Added Custom"

 

Just remove the original date column when you're done with it.

 

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
chrome-9xf-Zagzel-B

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
KIfp67uy-Sr
Proud to be a Super User!PBI-Super-User-Rank-30x30-1x

Thanks for the fast response! Quick question though: I have multiple relationships, calculations and reports dependent upon this column, so many that I cannot think of all of them off the top of my head. This is why I did not want to create a new custom column. If I delete the old column and use the same name for the new column, am I going to have to rebuild everything?

If you delete and rename you shouldn't have to redo anything.

I'll have a look at the syntax in your replace now.

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
chrome-9xf-Zagzel-B

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
KIfp67uy-Sr
Proud to be a Super User!PBI-Super-User-Rank-30x30-1x

Also, can you think of any reason why the code I tried isn't working?

Change your Replacer.ReplaceText with Replacer.ReplaceValue.

(I didn't test your duration addition)

= Table.ReplaceValue(#"Renamed Columns1", each [Date], each if [ID] = 36 then [Date] + #duration(365,0,0,0) else [Date], Replacer.ReplaceValue,{"Date"})

 

You will still run into issues with all of the types being reset to 'Any' with this method which is why I prefer the add column method.

 

See this post...

https://community.powerbi.com/t5/Desktop/Query-editor-replacing-values-based-on-another-column/td-p/...

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
chrome-9xf-Zagzel-B

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
KIfp67uy-Sr
Proud to be a Super User!PBI-Super-User-Rank-30x30-1x

Okay... now I feel pretty dumb. 🙂 Thanks a lot! 

Don't, we all do it from time to time.  Glad I could help. 

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
chrome-9xf-Zagzel-B

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
KIfp67uy-Sr
Proud to be a Super User!PBI-Super-User-Rank-30x30-1x

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors