Forum Discussion
Anonymous
1 year agoNot applicable
Summarizing inconsistent text format in column
I have a data source that provides "time spent" in various statuses, however it comes with delimited for all times it changes status, meaning one row can look like this "1M 0w 1d 20h 0m,1w 1d 20h 14...
- 1 year ago
Simple enough,
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("RVI5EsQgDPuKJzUFPjjyiH3BznYp0qTO91dgk3S2JYFk+H43zpy3tMlNehDzScbX9ksD0AKAbyoHiZ4kdiXzktviWAfHPmQ3ZWACrC+sy6vPOHi/kjynlXoldUXJV6q4fD9Jc4jZ6hB/CHp2mpaFVR6OP5RvksDqwpo8OnsMJZDFyegfbh/n6HTOHQYlAMltAJE1hmrqccBGmoJtSDCWMa3yULh5MJjnipJR3hQxTR04aS1Le3EvHIyYmw4rEgbX2qvNqbz6unPw2rxsTfWdWnCLxmqzrxZh8jQXDdvq4E8Fjz4DaeTxdGj2mcFNhCB7DVk4rXOTcVobF81x9z93EELJ/Acj3nz83x8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Key = _t, Status = _t]), Replacement = {{"M", "*30*1440"}, {"w", "*7*1440"}, {"d", "*1440"}, {"h", "*60"}, {"m", ""}, {" ", "+"}, {",", "+"}}, #"Transformed minutes" = Table.TransformColumns(Source, {"Status", each Expression.Evaluate(List.Accumulate(Replacement, _, (s,c) => Text.Replace(s, c{0}, c{1})))}) in #"Transformed minutes"
Anonymous
1 year agoNot applicable
You could use a Python script for it:
import pandas as pd
import numpy as np
def convert_to_minutes(row):
# Original string
time_string = row['Status 1']
# Predefined order of suffixes and their conversion factors to minutes
time_units = {
"Y": 365 * 24 * 60, # Years to minutes
"M": 30 * 24 * 60, # Months to minutes (approximate, assuming 30 days per month)
"w": 7 * 24 * 60, # Weeks to minutes
"d": 24 * 60, # Days to minutes
"h": 60, # Hours to minutes
"m": 1 # Minutes
}
# Create list for all the converted entries
converted_times = []
# Split by commas to handle multiple time components (e.g., "2w, 3d, 11h, 41m")
for times in time_string.split(","):
time_in_minutes = 0 # Initialize total time for this entry
# Parse the string and calculate the total minutes for each part
for part in times.split():
suffix = part[-1] # Get the suffix (last character)
# Check if the suffix is valid and exists in the time_units dictionary
if suffix in time_units:
value = int(part[:-1]) # Extract the numeric value
time_in_minutes += value * time_units[suffix] # Convert and accumulate the total
# Append the calculated time for this part to the list
converted_times.append(time_in_minutes)
# Sum the total times for all parts in the row
total_time = np.sum(np.asarray(converted_times))
return total_time
# Apply the function to create a new column for total time
dataset['total time in minuts'] = dataset.apply(convert_to_minutes, axis=1).astype(int)
Resultat in:
Did I solve your question? Mark my post as a solution! Kudos are appreciated as well as LinkedIn endorsements.
- ThxAlot1 year agoSuper User
Good to see someone incorporating py script.
The script is also simple enough, cheers!
import re repl = { "Y": '* 365 * 24 * 60', # Years to minutes "M": '* 30 * 24 * 60', # Months to minutes (approximate, assuming 30 days per month) "w": '* 7 * 24 * 60', # Weeks to minutes "d": '* 24 * 60', # Days to minutes "h": '* 60', # Hours to minutes "m": '', # Minutes ",": '+', " ": '+' } ptn = re.compile('[a-zA-Z, ]') dataset['Minutes'] = dataset.apply(lambda r: eval(ptn.sub(lambda m: repl[m.group()], r['Status'])), axis=1)