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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
senthil_venkat
Frequent Visitor

Split Text into multiple columns

Hi Guys,

I have an data where one specific column "Total Duration" which has the data combined with days, hours and minutes in one field, as shown below.    Is there any way to convert this data to use for calculating actual durations ? or Averages, Total Days, TOtal Hours etc..  or any calculation ? 


Say for ex.. If need to get average of 10 rows, currently with this "Text" type it is not possible for me. I looking for some help to convert this text to values which can used for calculations.  

 

6 Days 17 Hours 20 Minutes
6 Days 17 Hours 20 Minutes
2 Days 17 Hours 18 Minutes
2 Days 17 Hours 18 Minutes
8 Days 15 Hours 46 Minutes
8 Days 35 Minutes
6 Days 6 Hours 50 Minutes
6 Days 23 Hours 14 Minutes
15 Hours 19 Minutes
15 Hours 19 Minutes
15 Hours 15 Minutes
15 Hours 15 Minutes
2 Days 15 Hours 10 Minutes
2 Days 15 Hours 10 Minutes
1 Day 15 Hours 8 Minutes
1 Day 15 Hours 8 Minutes
15 Hours 8 Minutes
15 Hours 8 Minutes
1 Day 15 Hours 8 Minutes
1 Day 15 Hours 8 Minutes
2 ACCEPTED SOLUTIONS
mahoneypat
Microsoft Employee
Microsoft Employee

Please see the M code below for how to do this by extracting the numbers (from end of input since not all rows have Days value).  To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.  It results in columns for Days, Hours, and Minutes, along with a Total Duration column, and a total minutes column.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlNwSawsVjA0V/DILy0qVjAyUPDNzCstSS1WitUhKG2EJm1oQYq0BVTaFCptYoZN2tgUm4vMoHpMsbrXyBhmowmKNNwqQ0sSxU2JEjdC85Eh9uDCIW0IkkbIWpAgSZowefbEAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    #"Inserted Text Between Delimiters" = Table.AddColumn(#"Changed Type", "Minutes", each Text.BetweenDelimiters([Column1], " ", " ", {1, RelativePosition.FromEnd}, 0), type text),
    #"Inserted Text Between Delimiters1" = Table.AddColumn(#"Inserted Text Between Delimiters", "Hours", each Text.BetweenDelimiters([Column1], " ", " ", {3, RelativePosition.FromEnd}, 0), type text),
    #"Inserted Text Before Delimiter" = Table.AddColumn(#"Inserted Text Between Delimiters1", "Days", each Text.BeforeDelimiter([Column1], " ", {4, RelativePosition.FromEnd}), type text),
    #"Changed Type1" = Table.TransformColumnTypes(#"Inserted Text Before Delimiter",{{"Minutes", Int64.Type}, {"Hours", Int64.Type}, {"Days", Int64.Type}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type1",null,0,Replacer.ReplaceValue,{"Days"}),
    #"Added Custom" = Table.AddColumn(#"Replaced Value", "Duration", each #duration([Days]+0,[Hours],[Minutes],0)),
    #"Changed Type2" = Table.TransformColumnTypes(#"Added Custom",{{"Duration", type duration}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type2", "Total Minutes", each Duration.TotalMinutes([Duration])),
    #"Changed Type3" = Table.TransformColumnTypes(#"Added Custom1",{{"Total Minutes", Int64.Type}})
in
    #"Changed Type3"

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

CNENFRNL
Community Champion
Community Champion

My logic is intuitive, first substitute Days/Hours/Minutes with 1440/60/1 respectively; then sum it up.

Pls refer to the following M code,

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Replaced Value" = Table.ReplaceValue(Source,"Days","1440",Replacer.ReplaceText,{"Text"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","Hours","60",Replacer.ReplaceText,{"Text"}),
    #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","Minutes","1",Replacer.ReplaceText,{"Text"}),
    Custom1 = Table.TransformColumns(#"Replaced Value2", {"Text", each List.Transform(Text.Split(_, " "), Number.From)}),
    Custom2 = Table.TransformColumns(Custom1, {"Text", each
        let
            D = _{0}*_{1},
            h = _{2}?*_{3}?,
            H = if h=null then 0 else h,
            m = _{4}?*_{5}?,
            M = if m=null then 0 else m,
            span = D+H+M
        in
            span
        })
in
    Custom2

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

3 REPLIES 3
CNENFRNL
Community Champion
Community Champion

My logic is intuitive, first substitute Days/Hours/Minutes with 1440/60/1 respectively; then sum it up.

Pls refer to the following M code,

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Replaced Value" = Table.ReplaceValue(Source,"Days","1440",Replacer.ReplaceText,{"Text"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","Hours","60",Replacer.ReplaceText,{"Text"}),
    #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","Minutes","1",Replacer.ReplaceText,{"Text"}),
    Custom1 = Table.TransformColumns(#"Replaced Value2", {"Text", each List.Transform(Text.Split(_, " "), Number.From)}),
    Custom2 = Table.TransformColumns(Custom1, {"Text", each
        let
            D = _{0}*_{1},
            h = _{2}?*_{3}?,
            H = if h=null then 0 else h,
            m = _{4}?*_{5}?,
            M = if m=null then 0 else m,
            span = D+H+M
        in
            span
        })
in
    Custom2

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

mahoneypat
Microsoft Employee
Microsoft Employee

Please see the M code below for how to do this by extracting the numbers (from end of input since not all rows have Days value).  To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.  It results in columns for Days, Hours, and Minutes, along with a Total Duration column, and a total minutes column.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlNwSawsVjA0V/DILy0qVjAyUPDNzCstSS1WitUhKG2EJm1oQYq0BVTaFCptYoZN2tgUm4vMoHpMsbrXyBhmowmKNNwqQ0sSxU2JEjdC85Eh9uDCIW0IkkbIWpAgSZowefbEAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    #"Inserted Text Between Delimiters" = Table.AddColumn(#"Changed Type", "Minutes", each Text.BetweenDelimiters([Column1], " ", " ", {1, RelativePosition.FromEnd}, 0), type text),
    #"Inserted Text Between Delimiters1" = Table.AddColumn(#"Inserted Text Between Delimiters", "Hours", each Text.BetweenDelimiters([Column1], " ", " ", {3, RelativePosition.FromEnd}, 0), type text),
    #"Inserted Text Before Delimiter" = Table.AddColumn(#"Inserted Text Between Delimiters1", "Days", each Text.BeforeDelimiter([Column1], " ", {4, RelativePosition.FromEnd}), type text),
    #"Changed Type1" = Table.TransformColumnTypes(#"Inserted Text Before Delimiter",{{"Minutes", Int64.Type}, {"Hours", Int64.Type}, {"Days", Int64.Type}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type1",null,0,Replacer.ReplaceValue,{"Days"}),
    #"Added Custom" = Table.AddColumn(#"Replaced Value", "Duration", each #duration([Days]+0,[Hours],[Minutes],0)),
    #"Changed Type2" = Table.TransformColumnTypes(#"Added Custom",{{"Duration", type duration}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type2", "Total Minutes", each Duration.TotalMinutes([Duration])),
    #"Changed Type3" = Table.TransformColumnTypes(#"Added Custom1",{{"Total Minutes", Int64.Type}})
in
    #"Changed Type3"

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Hi Pat,

Wonderful. Thanks for the solution. This works for me. Can you guide me how to replace the example text with actual column from my table ? 

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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