cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
DDM
Helper I
Helper I

Date Formatting Data

Hi All, 

 

I have a strange issue that started to affect my flow recently. I get an attachment (csv) which I move to a SharePoint Document library. I then parse the csv and create a new SharePoint list item based on the information in the csv. 

 

Expressions are:

 

Year - substring(string(item()?['Last Working Date']),6,4)
Month - substring(string(item()?['Last Working Date']),3,2)
Day - substring(string(item()?['Last Working Date']),0,2)

 

Date - Format.png

 

 

This was working fine until the other day when i started to get an error.

 

Unable to process template language expressions in action 'Year' inputs at line '0' and column '0': 'The template language function 'substring' parameter is out of range: 'start index' must be non-negative integer and should be less than the length of the string.

 

Data in the CSV comes in the following format:

 

"Unique Id","First Name","Surname","Last Working Date","Email"
"2285638","John","Smth","31/12/2022","john.smith@xyz.com"

 

If I do not format the date I get an ISO 8601 Error.

 

I really would appreciate some help.

 

Thanks

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
ekarim2020
Super User
Super User

Hi @DDM 

 

[
  {
    "\"Unique Id\"": "\"2285638\"",
    "\"First Name\"": "\"John\"",
    "\"Surname\"": "\"Smith\"",
    "\"Last Working Date\"": "\"31/12/2999\"",
    "\"Email\"\r": "\"john.smith@xyz.com\"\r"
  }
]

 

Using the example above: The property name (key) "\"Email\"\r" actually has two characters at the end of the name: (1) a quotation mark \" and (2) \r a carriage return character.

 

The property value (value) "\"john.smith@xyz.com\"\r" also has two characters at the end: (1) a quotation mark \" and (2) \r a carriage return character.

The simplest way to slice the email address would be to put the Email property into a compose action and then use that compose variable in the slice action.

Snag_12c7b95.png

The slice expression uses -2 as the endIndex  because we want to remove two characters from the end of the email address: (1) the quotation mark \" and (2) the \r carriage return character:

slice(outputs('Compose_Email'), 1, -2)

 

Snag_13be562.png

You will also need to check that last array item's Email property value to see if it ends in a  \r carriage return character. If it does not end in a \r carriage return character the slice action will remove an extra character from the email address and return john.smith@xyz.co 

 

UPDATE: A better option would be to replace all the escape characters:

 

json(replace(replace(string(outputs('Compose_Email')), '\"',''),'\r',''))

 

 

or work with a "cleaned up" version of the JSON object as mentioned previously.
Ellis
____________________________________
If I have answered your question, please mark the post as Solved.
If you like my response, please give it a Thumbs Up.

View solution in original post

7 REPLIES 7
ekarim2020
Super User
Super User

Hi @DDM ,

 

You can use the parseDateTime function which will return a date that complies with ISO 8601:

 

parseDateTime('31/12/2022', 'en-GB') 

 

This will return:

 

2022-12-31T00:00:00.0000000 

 

We can combine parseDateTime with formatDateTime:


formatDateTime(parseDateTime('31/12/2022', 'en-GB'), 'yyyy-MM-dd')

 

This will return:

 

2022-12-31 

 


Ellis
____________________________________
If I have answered your question, please mark the post as Solved.
If you like my response, please give it a Thumbs Up.

Thanks for your response Ellis, but I am back to the ISO 8601 error.

 

Unable to process template language expressions in action 'Create_item' inputs at line '0' and column '0': 'In function 'parseDateTime', the value provided for date time string '' was not valid. The datetime string must match ISO 8601 format.'.

 

Here is the expression i used : 

formatDateTime(parseDateTime(string(item()?['Last Working Date']), 'en-GB'), 'yyyy-MM-dd')
 
This is what appears to be my problem....  date time string '' was not valid. 
 
Its empty, so seems it isn't the date time formatting. This is the output of my parsed JSON
 
  {
    "\"Unique Id\"""\"2285638\"",
    "\"First Name\"""\"John\"",
    "\"Surname\"""\"Smith\"",
    "\"Last Working Date\"""\"31/12/2999\"",
    "\"Email\"\r""\"john.smith@xyz.com\"\r"
  },
ekarim2020
Super User
Super User

OK, I think I now see the problem.

 

The data from the CSV file contains quotation marks as part of the column names and data:

 

"Unique Id","First Name","Surname","Last Working Date","Email"
"2285638","John","Smth","31/12/2022","john.smith@xyz.com"

 

Your JSON object is:

 

{
  "\"Unique Id\"": "\"2285638\"",
  "\"First Name\"": "\"John\"",
  "\"Surname\"": "\"Smith\"",
  "\"Last Working Date\"": "\"31/12/2999\"",
  "\"Email\"\r": "\"john.smith@xyz.com\"\r"
}

 

Because the property names and values have use double quotes the back slash \ symbol used.   The back slash \ is an escape character and the quotation character that comes after it is understood to be part of the value of the string, property name or value.

 

The Last Working Date value includes the quotation marks as part of its name and value.  The value of Last Working Date is:

 

"31/12/2999"

 

Power Automate needs a date without the quotation marks in order to process it, such as:

 

31/12/2999

 

What you could do is remove the quotation marks using the slice function:

Snag_253d2e2.png

 

slice(items('Apply_to_each')['"Last Working Date"'],1,-1)

 

Then use that compose variable in the date expression:

Snag_2577afa.png

 


formatDateTime(parseDateTime(outputs('Compose_LastWorkingDay'), 'en-GB'), 'yyyy-MM-dd')

 


Ellis
____________________________________
If I have answered your question, please mark the post as Solved.
If you like my response, please give it a Thumbs Up.

A further note.

 

Your CSV File CSV File with no quotation marks in the column names or data

"Unique Id","First Name","Surname","Last Working Date","Email"
"2285638","John","Smth","31/12/2022","john.smith@xyz.com"

Unique Id,First Name,Surname,Last Working Date,Email
2285638,John,Smth,31/12/2022,john.smith@xyz.com

 

Normally we would want to work a clean JSON object so as to avoid the above problems:

Your current JSON object A "sanitized" JSON object
{
"\"Unique Id\"": "\"2285638\"",
"\"First Name\"": "\"John\"",
"\"Surname\"": "\"Smith\"",
"\"Last Working Date\"": "\"31/12/2999\"",
"\"Email\"\r": "\"john.smith@xyz.com\"\r"
}
{
"Unique Id": "2285638",
"First Name": "John",
"Surname": "Smith",
"Last Working Date": "31/12/2999",
"Email": "john.smith@xyz.com"
}

 

We could convert the original JSON array to a string and replace the quotation characters to produce a cleaner JSON object:

Snag_25b4215.png

 

 

json(replace(replace(string(outputs('Compose_JSON')), '\"',''),'\r',''))

 

 

 

The resulting JSON array, with no escaped quotation characters in the property names or data:

Snag_25c07a7.png

 

I said that we could convert the original JSON array to cleaner JSON object - but there will be instances where you may want to keep the data as is, without altering it.

 

Ellis

v-liwei-msft
Community Support
Community Support

Hi @DDM 

 

Flow doesn't recognize "31-12-2022" and dates in formats such as "2022-31-12" (dd-MM-yyyy and yyyy-dd-MM). 

Flow recognizes 31 as a month instead of a day. 

Please adjust your date format to avoid this error.

 

Best Regards,

Levi

Hi Ellis, almost perfect.

 

Your suggestion regarding the date was spot on. Rather than clean the JSON I used the same expression for email I am presented with the strangest error.....

 

'slice(items('Apply_to_each')['"Email"'],1,-1)' cannot be evaluated because property '"Email"' doesn't exist, available properties are '"Unique Id", "First Name", "Surname", "Last Working Date", "Email"

 

What am i missing ? 

 

Thanks for your help and patience so far.

ekarim2020
Super User
Super User

Hi @DDM 

 

[
  {
    "\"Unique Id\"": "\"2285638\"",
    "\"First Name\"": "\"John\"",
    "\"Surname\"": "\"Smith\"",
    "\"Last Working Date\"": "\"31/12/2999\"",
    "\"Email\"\r": "\"john.smith@xyz.com\"\r"
  }
]

 

Using the example above: The property name (key) "\"Email\"\r" actually has two characters at the end of the name: (1) a quotation mark \" and (2) \r a carriage return character.

 

The property value (value) "\"john.smith@xyz.com\"\r" also has two characters at the end: (1) a quotation mark \" and (2) \r a carriage return character.

The simplest way to slice the email address would be to put the Email property into a compose action and then use that compose variable in the slice action.

Snag_12c7b95.png

The slice expression uses -2 as the endIndex  because we want to remove two characters from the end of the email address: (1) the quotation mark \" and (2) the \r carriage return character:

slice(outputs('Compose_Email'), 1, -2)

 

Snag_13be562.png

You will also need to check that last array item's Email property value to see if it ends in a  \r carriage return character. If it does not end in a \r carriage return character the slice action will remove an extra character from the email address and return john.smith@xyz.co 

 

UPDATE: A better option would be to replace all the escape characters:

 

json(replace(replace(string(outputs('Compose_Email')), '\"',''),'\r',''))

 

 

or work with a "cleaned up" version of the JSON object as mentioned previously.
Ellis
____________________________________
If I have answered your question, please mark the post as Solved.
If you like my response, please give it a Thumbs Up.

Helpful resources

Announcements
Power Automate News & Announcements

Power Automate News & Announcements

Keep up to date with current events and community announcements in the Power Automate community.

Power Automate Community Blog

Power Automate Community Blog

Check out the latest Community Blog from the community!

Users online (5,037)