Power Apps: Convert Time

by | Date & Time | 0 comments


The other day, I saw a good question over on Reddit about “Converting Decimal to Minutes with Seconds”. So if you had a numeric value of 353.10, convert that number over to a hh:mm:ss, like 05:53:06. I ended up making a YouTube video about this, but we’ll step through this here in this post as well, step-by-step.

If by chance, you don’t want to learn and thoroughly understand how we convert these values, and you just want to get at the formula you need, look no further, because here it is (just replace “YOUR_TIME_VARIABLE” with your variable or numeric control value):

With( 
    {DecimalValue: YOUR_TIME_VARIABLE}, 
    Text( Trunc(DecimalValue / 60), "00" ) & ":" & 
    Text( Trunc(Mod(DecimalValue, 60)), "00" ) & ":" & 
    Text( Trunc(Value( 
        With(
            {SecondsDecimal: 
                If(IsBlank(Find(".", 
                    Text(DecimalValue))), 
                    0, 
                    Left(Last(Split(Text(DecimalValue), ".")).Result, 2) ) 
            }, 
            If(Len(SecondsDecimal) = 1, 
                SecondsDecimal & "0", SecondsDecimal 
            ) 
        ) 
    ) * 60 / 100), 
    "00") 
)

The first step to break things down and understand how we got at the end result formula, is to separate values and steps down with controls.

  1. Add a slider control to increase/decrease the decimal numeric value with represents how many minutes have passed in a time span.
    • Set the Max to 86,400, which is how many seconds there are in a day. If we do this, we’ll know that if we slide to the middle, the end result should be somewhere around 12 hours (more or less a little), and if we slide it to all the way to the right, we’ll end up with 24 hours.
    • Next, change the OnChange to update a variable. This could be a global variable or a context variable. Because we are only using this variable on this screen, why not use UpdateContext function to make it a context variable.
  2. Add three labels below the slider for hours, minutes, seconds:
    • One for Hours – Text: Trunc(DecimalTime / 60)
      • Trunc will remove the decimal portion we don’t need
    • One for Minutes – Text: Trunc(Mod(DecimalTime, 60))
      • Trunc will remove the decimal portion we don’t need
    • One for Seconds – Text: Value(Last(Split(Text(DecimalTime), “.”)).Result) / 10
      • this one will need to be broken down much more, since it is more complicated (See bulleted list below).
  3. Add in a label at the bottom of all these which will pull it all together which will hold the end result.

Power Apps Screen Shot

You’ll notice that the seconds portion isn’t quite right. There are a few more steps we need to knock out before we get to the value we need:

  1. We need to know if the value is zero. If the numberic value is reduced to a zero with no decimal point, there’s no need to try to do the next steps.
  2. If there is a decimal value, we need to grab only the first two numbers after the decimal. We’re just interested in whole seconds, not fractions of seconds in this use case.
  3. If we only get one number after the decimal, then we need to add a zero to the end. For example, 0.1 minutes is a tenth (10) and not a hundredth (1), which would give us very different results.
  4. In order to convert a decimal variable/100 to a time, unknown/60, we know from High School Algebra class, we should multiply these fractions and solve for x (the unknown value). We do this by multiplying our left over decimal portion of our number, multiplying it by 60, then dividing by 100.
    • NOTE: I go over all this in much more depth in the video at mark 5:30, which you might find very interesting!

At last, this give us our needed value for seconds! Be sure to grab the fully finished expression at the top of this post.

And here is the end result we achieved in the video. We just accomplished it with extra steps over 38 minutes:

Download the file here: https://github.com/PowerAppsDarren/TimeConvertFromDecimal

Power Apps Screen Shot

0 Comments

Submit a Comment

Current Articles

Understanding Data for the Power Platform

Unlock the power of data in your Power Apps projects with this comprehensive guide to relational databases! In this live stream tutorial, we dive deep into the fundamentals of data, relational databases, and how they seamlessly interact with the Microsoft Power...

Power Apps Beginner Topics

Welcome to the Power Apps Beginner Topics livestream, hosted by Darren Neese – your Power Apps Accelerator. This in-depth nearly 3-hour session is tailor-made for absolute beginners who are eager to delve into the world of Microsoft Power Apps.

Wrapping Your Mind Around Power BI

The business world is constantly evolving, and the need for effective data visualization and analytics tools has never been greater. If you're looking to harness the power of data in your business, then this blog post is for you. We are thrilled to share an exclusive...

Power App Host Object

https://youtu.be/8of3VF4aOy8 Dive deep into the world of Microsoft Power Apps with Darren Neese, your expert guide on the platform. This insightful video demystifies the latest feature in Power Apps - the Host Object. The Host Object, integral to every app, equips you...

Microsoft Power Platform in the Enterprise

Harnessing the Power of Microsoft Power Platform in the Enterprise The Microsoft Power Platform offers a suite of tools that can be used to develop customized business applications, automate workflows, create visual data analytics, and build chatbots without extensive...

Future of Power Apps

As technology continues to evolve at a rapid pace, so does the potential for Power Apps. Microsoft is constantly updating and improving the platform, adding new features and capabilities to help businesses and organizations streamline their processes and improve their...

Power Apps Advanced Example

https://youtu.be/3ISiX4eImVI Looking for an advanced Power Apps application to streamline your timesheets and invoices? Look no further than this demonstration video showcasing an example application built for one of our clients! In this video, we take you through all...

Delete Records in Power Apps by Using Remove

https://www.youtube.com/watch?v=WYBfigNtpkA Any Power Apps application you write will at some point need to delete records from your data source. Sometimes you're on an edit screen, and you'll want to have a delete button for an individual record, but sometimes you...

How to Create Database Tables in Microsoft Dataverse

https://www.youtube.com/watch?v=v4A51a494yw In this video tutorial, you'll learn how to create custom database tables in Microsoft Dataverse by using an Entity Relationship Diagram (ERD) as a guide. We'll walk you through the entire process of building your ERD...

Can ChatGPT Create a Whole Power App?

https://www.youtube.com/watch?v=Ohgy3k6oW4o In this exciting live stream, I'll be demonstrating how you can build a complete Power App from start to finish using Dataverse, with the help of ChatGPT. This is an exciting opportunity to see the power of AI and how it can...