Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find year in a string?

I try to write a custom function which is going to find a current or next year in a given string. Let’s say I have a string in a Name column: A2022_new_accruals.xlsx. I need to return 1 if it’s true, null if it’s not. I wrote this function but it gives me error:

= if Text.Contains([Name], Date.ToText(Date.Year(DateTime.LocalNow()),"yyyy")) or Text.Contains([Name], Date.ToText(Date.AddYears(Date.Year(DateTime.LocalNow()),1),"yyyy")) then 1 else null

Error:

Expression.Error: Cannot convert value of Function type to Date type

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

try

let Source = #table({"Name"},{{"A2021_new_accruals.xlsx"},{"A2022_new_accruals.xlsx"},{"A2023_new_accruals.xlsx"},{"A2024_new_accruals.xlsx"}}),
ThisYear=Date.Year(DateTime.LocalNow()),
#"Added Custom" = Table.AddColumn(Source, "Custom", each if Text.Contains([Name],Text.From(ThisYear)) or Text.Contains([Name],Text.From(ThisYear+1)) then 1 else null)
in #"Added Custom"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading