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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
sandip
Helper III
Helper III

How can i get only days,hours, minutes and seconds value from a list

Hi,

How can i get only days,hours, minutes and seconds value from the below list:

numberbusiness_duration
CTAP000126039 Seconds
CTAP00034051 Day 10 Minutes
CTAP00035982 Hours 38 Minutes
CTAP00027184 Days 17 Hours 16 Minutes
CTAP000234911 Days 7 Hours 21 Minutes
CTAP00027741 Hour 43 Minutes
CTAP00027602 Hours 38 Minutes
CTAP00044561 Day 1 Hour 11 Minutes
CTAP000311016 Hours 23 Minutes
CTAP00031191 Hour 43 Minutes
CTAP00032121 Hour 43 Minutes
CTAP00045891 Hour 43 Minutes
CTAP000371516 Hours 23 Minutes
CTAP00037633 Days 7 Hours
CTAP00042211 Hour 43 Minutes

 

so from the above table cell, i want to convert each row to seconds value from like : 3 Days 7 Hours or 16 Hours 23 Minutes

so for 3 Days 7 Hours it should be 3*24*60*60 + 7*60*60....

and also I want to create a calculated for  this calculation and seconds value can be fitted into that new column.

Any idea how to achieve that?

 

 

1 ACCEPTED SOLUTION
v-easonf-msft
Community Support
Community Support

Hi , @sandip 

Please check if the following  sample  helps.

Pbix attached .

 

Try following steps:

1.split column by deliliter "" 

41.png

 

2.select  the columns and replace value 
1)   change "Seconds","Minutes","" Hours ","Days"  as "Second","Minute","Hour","Day"

2)do the similar steps to change "Second","Minute","" Hour","Day"  as "1","60","3600","96400"

 

42.png

 

3.chage column types as "whole number" and create a custome column as below

43.png

44.png

 

Best Regards,
Community Support Team _ Eason
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-easonf-msft
Community Support
Community Support

Hi , @sandip 

Please check if the following  sample  helps.

Pbix attached .

 

Try following steps:

1.split column by deliliter "" 

41.png

 

2.select  the columns and replace value 
1)   change "Seconds","Minutes","" Hours ","Days"  as "Second","Minute","Hour","Day"

2)do the similar steps to change "Second","Minute","" Hour","Day"  as "1","60","3600","96400"

 

42.png

 

3.chage column types as "whole number" and create a custome column as below

43.png

44.png

 

Best Regards,
Community Support Team _ Eason
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi,

Can you help me to learn Poqwer query or do you have any PDF to learn from it?

 

Thanks,

Sandip

ryan_mayu
Super User
Super User

@sandip 

 

You can try to create a column.Please note that the second logic is very easy because of the data you shared. I can only see seconds without minutes ,hours and days. If you have the combination of day with seconds, hour, minute with seconds, minute with seconds. The calculation for find the seconds value should be the most complicated.

 

Column2 = 
VAR daypostion=FIND("D",Sheet8[duration],1,0)
VAR hourpostion=find("H",Sheet8[duration],1,0)
VAR minutepostion=FIND("M",Sheet8[duration],1,0)
VAR secondpostion=FIND("Se",Sheet8[duration],1,0)
VAR dayvalue=if(daypostion=0,blank(),trim(left(Sheet8[duration],daypostion-1)))
VAR hoursvalue=if(hourpostion=0,blank(),if(daypostion=0,trim(left(Sheet8[duration],hourpostion-1)),trim(mid(Sheet8[duration],daypostion+4,hourpostion-(daypostion+4)))))
VAR minvalue=if(minutepostion=0,blank(),if(daypostion=0&&hourpostion=0,left(Sheet8[duration],minutepostion-1),if(hourpostion=0,trim(mid(Sheet8[duration],daypostion+4,minutepostion-(daypostion+4))),trim(mid(Sheet8[duration],hourpostion+5,minutepostion-(hourpostion+5))))))
VAR secondvalue=if(secondpostion=0,BLANK(),trim(LEFT(Sheet8[duration],secondpostion-1)))
return dayvalue*24*60*60+hoursvalue*60*60+minvalue*60+secondvalue

. 1.PNG





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




edhans
Super User
Super User

Try this. It returns this table in Power Query. This is very tedious and not bullet proof. Any extra spaces will cause this to not work. You'd need a lot more if/then/else handeling to account for that.

NOTE: I had to make a slight tweak to the code. It seems to work fine. The image below is returning the wrong amounts on a few occassions, but the code works. (I think). I didn't bother redoing the screen cap.

2020-06-04 09_15_05-Untitled - Power Query Editor.png

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZE9C8IwEIb/ypHZIfeRpBlFBxdB0K10EO3g0oK1g//epK1K4bDO9/C+z92Vpdmc1gdrLZK3ZmU4wrG+tM21M9XqM2SxLg0RtucnoIX9rekf9RxxsUgIwa7t7x1woUEUMEOSczrAMMHoVZgl5lIc6TdMqCcHGQwzBMI6M6z4W1HE+e+qYxyqlYyY45L8JKaWJiouiTEhLTHiiuWcgO4Po+A5f3p21lkVEepV1Qs=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [number = _t, business_duration = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"number", type text}, {"business_duration", type text}}),
    #"Added Days" = 
        Table.AddColumn(
            #"Changed Type", 
            "Days", 
            each
                if Text.PositionOf([business_duration], "Day") > 1
                then Number.From(Text.Middle([business_duration],0,Text.PositionOf([business_duration], "Day"))) * 86400
                else 0
            ),
    #"Added Hours" =
        Table.AddColumn(
            #"Added Days", 
            "Hours", 
            each
                if Text.PositionOf([business_duration], "Hour") > 1 then 
                    if Text.PositionOf([business_duration], "Hour") > 8
                    then Number.From(Text.Middle([business_duration],Text.PositionOf([business_duration], "Hour") - 3,2)) * 3600
                    else Number.From(Text.Start([business_duration],2)) * 3600
                else 0
            ),
    #"Added Minutes" =
        Table.AddColumn(
            #"Added Hours", 
            "Minute String", 
            each
                if Text.PositionOf([business_duration], "Minute") > 1 then
                    if Text.PositionOf([business_duration], "Minute") > 8
                    then Number.From((Text.Middle([business_duration],Text.PositionOf([business_duration], "Minute") - 3,2))) * 60
                    else Number.From((Text.Start([business_duration],2))) * 60
                else 0
            ),
    #"Added Seconds" =
        Table.AddColumn(
            #"Added Minutes", 
            "Seconds String", 
            each
                if Text.PositionOf([business_duration], "Seconds") > 1 then
                if Text.PositionOf([business_duration], "Seconds") > 8
                    then Number.From(Text.Middle([business_duration],Text.PositionOf([business_duration], "Seconds") - 3,2))
                    else Number.From(Text.Start([business_duration],2))
                else 0
            ),
    #"Inserted Sum" = Table.AddColumn(#"Added Seconds", "Total Seconds", each List.Sum({[Days], [Hours], [Minute String], [Seconds String]}), Int64.Type),
    #"Removed Other Columns" = Table.SelectColumns(#"Inserted Sum",{"number", "business_duration", "Total Seconds"})

in
    #"Removed Other Columns"

 

 

1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done

 

I didn't create this in DAX because generally calculated columns perform worse for larger data sets.

In general, try to avoid calculated columns. There are times to use them, but it is rare. Getting data out of the source system, creating columns in Power Query, or DAX Measures are usually preferred to calculated columns. See these references:
Calculated Columns vs Measures in DAX
Calculated Columns and Measures in DAX
Storage differences between calculated columns and calculated tables
Creating a Dynamic Date Table in Power Query

and in my defense, if this were my project internally, I'd do a lot of cleanup of the code and use some variables, but that is a bit tedious in Power Query so went for the brute force approach. 😁



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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