Forum Discussion

min-E's avatar
min-E
Helper I
2 years ago
Solved

Converting Minutes to DDHHMM format, negative not showing correctly

Hello,   I have not been able to find an answer to these questions.   I have duration in minutes from the system. PBI Datediff gives the same results. No issue.   _ Datediff PartOrder to Deliv...
  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi min-E ,

    The error in your original code is due to the presence of a negative number, which can cause an error when performing math calculations. You can add a judgment condition to your original code. For example:

    _PartOrder to PartDelivered DHM = 
    VAR Days =
    IF(
        [PartOrder to PartDelivered Minutes] >= 0,
        TRUNC([PartOrder to PartDelivered Minutes] / 1440),
        TRUNC((-[PartOrder to PartDelivered Minutes]) / 1440)
    )
    VAR Hours = 
    IF(
        [PartOrder to PartDelivered Minutes] >= 0,
        TRUNC(MOD([PartOrder to PartDelivered Minutes], 1440) / 60),
        TRUNC(MOD((-[PartOrder to PartDelivered Minutes]), 1440) / 60)
    )
    VAR Minutes = 
    IF(
        [PartOrder to PartDelivered Minutes] >= 0,
        MOD([PartOrder to PartDelivered Minutes], 60),
        MOD((-[PartOrder to PartDelivered Minutes]), 60)
    )
    RETURN
    IF(
        [PartOrder to PartDelivered Minutes] >= 0,
        CONCATENATE(
            Days & "D " & Hours & "H " & Minutes & "M",
            ""
        ),
        CONCATENATE(
            (0-Days) & "D " & (0-Hours) & "H " & (0-Minutes) & "M",
            ""
        )
    )

    If it is a negative number, it is first converted to a positive number for calculation.
    And the final output is as below:


    Regarding the sorting question, since your column is in your custom format, it is a text type column, and text types can't sort data from largest to smallest like the [MINUTE] column. Although you can do this by customizing the sorting, it requires you to define a sort order for each row in the table, which is quite cumbersome and I don't recommend it.

    Best Regards,
    Dino Tao
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.