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
miguelsus2000
Helper III
Helper III

Help! Converting decimal to date/time format string doesnt always work

Hi,

 

I've been pulling my hairs (what's new with Powerbi 🙂 ) trying to figure why some of my conversions from decimal to D H M format work for some decimals and not for others.  Notice how some minutes go beyond 60 and don't carry over the the hours and some hours go over 24 not carrying over to the days.

 

miguelsus2000_0-1631847753444.png

I feel I'm using very straightforward conversions:

To extract Days, Hours, and Mins I use the following formulate:
  • AccumulatedTimeInStep-Days = rounddown([AccTimeInStep],0)
  • AccumulatedTimeInStep-Hrs = rounddown((([AccTimeInStep] - rounddown([AccTimeInStep],0))*24),0)
  • AccumulatedTimeInStep-Min = if(
    mod([AccTimeInStep]*1440-trunc([AccTimeInStep],0)*1440,60) < 60,
    mod([AccTimeInStep]*1440-trunc([AccTimeInStep],0)*1440,60),
    (mod([AccTimeInStep]*1440-trunc([AccTimeInStep],0)*1440,60)/60) - trunc(mod([AccTimeInStep]*1440-trunc([AccTimeInStep],0)*1440,60),0)
    )
     
    AccTimeInStep is the decimal displayed in the table in days.
     
    Has anyone found a more straightforward easier way?

Thank you.

M

 

 

1 ACCEPTED SOLUTION
AllisonKennedy
Super User
Super User

@miguelsus2000  I would do this in Power Query if possible, looks like your Day is wrong, which is causing the problems with the other calculations.  I've followed the same logic as you though. I'm curious why that Day value is wrong though - can you provide a sample file or more context?

 

Paste this code into a new blank Query in Power Query editor and see if it makes sense/does the trick: 

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LcxBCgAgCETRu7iOwTStziLe/xqBuXswn4mgCVbKESTgW2AYFwx+CgqRnrpRbG/o/vGE2z8S3EWZDw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [AccTimeInStep = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"AccTimeInStep", type number}}),
#"Inserted Days" = Table.AddColumn(#"Changed Type", "Days", each Number.RoundDown([AccTimeInStep]), Int64.Type),
#"Inserted Remaining Hours" = Table.AddColumn(#"Inserted Days", "Remaining Hours", each ([AccTimeInStep] - [Days])*24, type number),
#"Inserted Hours" = Table.AddColumn(#"Inserted Remaining Hours", "Hours", each Number.RoundDown([Remaining Hours]), Int64.Type),
#"Inserted Remaining Minutes" = Table.AddColumn(#"Inserted Hours", "Remaining Minutes", each ([Remaining Hours] - [Hours])*60, type number),
#"Inserted Minutes" = Table.AddColumn(#"Inserted Remaining Minutes", "Minutes", each Number.RoundDown([Remaining Minutes]), Int64.Type),
#"Inserted Remaining Seconds" = Table.AddColumn(#"Inserted Minutes", "Remaining Seconds", each ([Remaining Minutes] - [Minutes])*60, type number),
#"Inserted Seconds" = Table.AddColumn(#"Inserted Remaining Seconds", "Seconds", each Number.RoundDown([Remaining Seconds]), Int64.Type),
#"Inserted TimeKey" = Table.AddColumn(#"Inserted Seconds", "TimeKey", each ([Days] * 1000000) + ([Hours] * 10000) + ([Minutes] * 100) + [Seconds], type number),
#"Split Column by Position" = Table.SplitColumn(Table.TransformColumnTypes(#"Inserted TimeKey", {{"TimeKey", type text}}, "en-NZ"), "TimeKey", Splitter.SplitTextByPositions({0,2,4,6}, true), {"D", "HH", "MM", "SS"}),
#"Replaced Value" = Table.ReplaceValue(#"Split Column by Position","","0",Replacer.ReplaceValue,{"D"}),
#"Merged Columns" = Table.CombineColumns(#"Replaced Value",{"D", "HH", "MM", "SS"},Combiner.CombineTextByDelimiter(":", QuoteStyle.None),"D:HH:MM:SS")
in
#"Merged Columns"


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

View solution in original post

7 REPLIES 7
AllisonKennedy
Super User
Super User

@miguelsus2000  I would do this in Power Query if possible, looks like your Day is wrong, which is causing the problems with the other calculations.  I've followed the same logic as you though. I'm curious why that Day value is wrong though - can you provide a sample file or more context?

 

Paste this code into a new blank Query in Power Query editor and see if it makes sense/does the trick: 

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LcxBCgAgCETRu7iOwTStziLe/xqBuXswn4mgCVbKESTgW2AYFwx+CgqRnrpRbG/o/vGE2z8S3EWZDw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [AccTimeInStep = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"AccTimeInStep", type number}}),
#"Inserted Days" = Table.AddColumn(#"Changed Type", "Days", each Number.RoundDown([AccTimeInStep]), Int64.Type),
#"Inserted Remaining Hours" = Table.AddColumn(#"Inserted Days", "Remaining Hours", each ([AccTimeInStep] - [Days])*24, type number),
#"Inserted Hours" = Table.AddColumn(#"Inserted Remaining Hours", "Hours", each Number.RoundDown([Remaining Hours]), Int64.Type),
#"Inserted Remaining Minutes" = Table.AddColumn(#"Inserted Hours", "Remaining Minutes", each ([Remaining Hours] - [Hours])*60, type number),
#"Inserted Minutes" = Table.AddColumn(#"Inserted Remaining Minutes", "Minutes", each Number.RoundDown([Remaining Minutes]), Int64.Type),
#"Inserted Remaining Seconds" = Table.AddColumn(#"Inserted Minutes", "Remaining Seconds", each ([Remaining Minutes] - [Minutes])*60, type number),
#"Inserted Seconds" = Table.AddColumn(#"Inserted Remaining Seconds", "Seconds", each Number.RoundDown([Remaining Seconds]), Int64.Type),
#"Inserted TimeKey" = Table.AddColumn(#"Inserted Seconds", "TimeKey", each ([Days] * 1000000) + ([Hours] * 10000) + ([Minutes] * 100) + [Seconds], type number),
#"Split Column by Position" = Table.SplitColumn(Table.TransformColumnTypes(#"Inserted TimeKey", {{"TimeKey", type text}}, "en-NZ"), "TimeKey", Splitter.SplitTextByPositions({0,2,4,6}, true), {"D", "HH", "MM", "SS"}),
#"Replaced Value" = Table.ReplaceValue(#"Split Column by Position","","0",Replacer.ReplaceValue,{"D"}),
#"Merged Columns" = Table.CombineColumns(#"Replaced Value",{"D", "HH", "MM", "SS"},Combiner.CombineTextByDelimiter(":", QuoteStyle.None),"D:HH:MM:SS")
in
#"Merged Columns"


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Greg_Deckler
Super User
Super User

@miguelsus2000 There are better ways to do this but it's late:

D = INT([Column1])
H = INT(([Column1] - INT([Column1])) / (1/24))
M = INT((([Column1] - INT([Column1])) - [H]/24) / (1/24/60))

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thank you Greg.  I might have tried something similar... it looks better sitll have a couple discrepancies.  I would have thought TRUNC is better as INT rounds to the closest integer; TRUNC strips the decimal.  See below.  This should be very straightforward, but wonder if there is anythign in my config not allowing the truncating of some of the values.

miguelsus2000_0-1631850958189.png

 

@miguelsus2000 Are you creating measures or columns? From your formula it looks like [AccTimeInStep] is a MEASURE, which means the other values should only be MEASURES as well, or you'll get funny results. 

 

As I mentioned previously, it's more than just the values you highlighted that are incorrect: 

AllisonKennedy_0-1631939618486.png

The problem stems from your INT/TRUNC/ROUNDDOWN step but it will be related to the CONTEXT of the [AccTimeInStep] measure.

 

@Greg_Deckler formulas should work like a charm in correct context, so we need to help you with context errors. 

 

@miguelsus2000 Can you please provide:

* screenshot of your fields in the matrix (ie what fields are in columns, rows and values)

* Paste the DAX for the [AccTimeInStep] measure directly into your post

* screenshot of your model view showing relationships between all the tables and tell us which tables the fields come from that are used in the matrix

Hopefully that makes sense?

 

Once we have that info we can troubleshoot the problem easily. 😀


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

@miguelsus2000 I don't see the same discrepencies when using my formulas. Check out Table 11 in attached PBIX below signature. TRUNC and INT are only different when using negative numbers.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thank you Greg... gonna try out and get back. 

@miguelsus2000 Not sure, but this might make things clearer:

Conversion = 
    VAR __Value = [Column1]
    VAR __Days = INT(__Value)
    VAR __Hours = INT ( (__Value - __Days) * 24 )
    VAR __Minutes = INT ( (__Value - __Days - __Hours / 24) * 24*60 )
    VAR __Seconds = INT ( (__Value - __Days - __Hours / 24 - __Minutes/24/60) * 24*60*60 )
RETURN
    __Days&":"&__Hours&":"&__Minutes&":"&__Seconds

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.

Top Solution Authors