Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Create a matrix in hh:mm format

Hi,
I have a table with 3 columns: Year Month, Specialty, Duration (in seconds)

YearMonthSpecialtyDuration
11/1/2023 0:00AA2820
11/1/2023 0:00AA3960
11/1/2023 0:00AA4200
11/1/2023 0:00AA4620
12/1/2023 0:00AA1680
12/1/2023 0:00AA3420
12/1/2023 0:00AA3600
12/1/2023 0:00AA5460
11/1/2023 0:00BB480
11/1/2023 0:00BB660
11/1/2023 0:00BB720
11/1/2023 0:00BB780
11/1/2023 0:00BB960
11/1/2023 0:00CC1020
11/1/2023 0:00CC1080
11/1/2023 0:00CC1140
11/1/2023 0:00CC1200
11/1/2023 0:00CC1260
11/1/2023 0:00CC1320
11/1/2023 0:00CC1500
11/1/2023 0:00CC1680
11/1/2023 0:00CC1860

 

 

I am trying to to create a matrix like that with values in hh:mm format

SpecialtyNov 2023Dec 2023Avg durationMonth  
vs Avg Var
Total
AA156001416014880-72026760
BB360003600-36003600
CC120601000011030-103022060

 

Thank you in advance!

 

  • Here is an example of a duration measure.

    The first measure is just the sum of seconds.

    Duration Seconds = SUM ( YourTable[Duration] )

     

    The second measure uses the first to calcuation the time and format it.

    Formatted Duration = 
    VAR _Seconds = [Duration Seconds]
    VAR _Minutes = INT ( DIVIDE ( _Seconds, 60 ) )
    VAR _RemainingSeconds = MOD ( _Seconds, 60 )
    VAR _Hours = INT ( DIVIDE ( _Minutes, 60 ) )
    VAR _RemainingMinutes = MOD ( _Minutes, 60 )
    
    RETURN
        IF (
            NOT ISBLANK ( _Seconds ),
                FORMAT ( _Hours, "00" ) & ":" & 
                FORMAT ( _RemainingMinutes, "00" ) & ":" & 
                FORMAT ( _RemainingSeconds, "00" )
        )

     

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi  Anonymous ,

     

    Here are the steps you can follow:

    1. Create calculated column.

    Month_Year =
    FORMAT('Table'[YearMonth],"mmm")&" "&YEAR('Table'[YearMonth])

    2. Create calculated table.

    Test =
    SUMMARIZE('Table','Table'[Specialty],'Table'[Month_Year],
    "Duration",SUMX(FILTER(ALL('Table'),'Table'[Specialty]=EARLIER('Table'[Specialty])&&'Table'[Month_Year]=EARLIER('Table'[Month_Year])),[Duration]))

    Table 2 =
    var _table1=
    SUMMARIZE(
        'Test','Test'[Specialty],
        "Month_Year","Avg duration",
        "Duration",AVERAGEX(FILTER(ALL('Test'),'Test'[Specialty]=EARLIER('Test'[Specialty])),[Duration]))
    var _table2=
    SUMMARIZE(
        'Test','Test'[Specialty],
        "Month_Year","Month vs Avg Var",
        "Duration",
        SUMX(FILTER(ALL('Test'),'Test'[Specialty]=EARLIER('Test'[Specialty])&&
        'Test'[Month_Year]=
        MAXX(FILTER(ALL('Table'),'Table'[YearMonth]=MAX('Table'[YearMonth])),[Month_Year])),[Duration])
        -
        AVERAGEX(FILTER(ALL('Test'),'Test'[Specialty]=EARLIER('Test'[Specialty])),[Duration]))
    RETURN
    UNION(
        'Test',_table1,_table2
    )

    As far as I know, Power BI's default sorting is based on alphabetical order, so it's not the effect you're looking for, we need to go and create a new table to do the sorting

    Sort_Table =
    var _table1=
     DISTINCT('Table 2'[Month_Year])
    return
    ADDCOLUMNS(
        _table1,"Index",
        SWITCH(
        TRUE(),
        [Month_Year]="Nov 2023",1,
        [Month_Year]="Dec 2023",2,
        [Month_Year]="Avg duration",3,
        [Month_Year]="Month vs Avg Var",4))

    3. Select [Month_Year] – Column tools – Sort by column – [lndex].

    4. Connecting tables to each other.

     

    5. Result:

    Best Regards,

    Liu Yang

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

5 Replies

  • Here is an example of a duration measure.

    The first measure is just the sum of seconds.

    Duration Seconds = SUM ( YourTable[Duration] )

     

    The second measure uses the first to calcuation the time and format it.

    Formatted Duration = 
    VAR _Seconds = [Duration Seconds]
    VAR _Minutes = INT ( DIVIDE ( _Seconds, 60 ) )
    VAR _RemainingSeconds = MOD ( _Seconds, 60 )
    VAR _Hours = INT ( DIVIDE ( _Minutes, 60 ) )
    VAR _RemainingMinutes = MOD ( _Minutes, 60 )
    
    RETURN
        IF (
            NOT ISBLANK ( _Seconds ),
                FORMAT ( _Hours, "00" ) & ":" & 
                FORMAT ( _RemainingMinutes, "00" ) & ":" & 
                FORMAT ( _RemainingSeconds, "00" )
        )

     

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi  Anonymous ,

     

    Here are the steps you can follow:

    1. Create calculated column.

    Month_Year =
    FORMAT('Table'[YearMonth],"mmm")&" "&YEAR('Table'[YearMonth])

    2. Create calculated table.

    Test =
    SUMMARIZE('Table','Table'[Specialty],'Table'[Month_Year],
    "Duration",SUMX(FILTER(ALL('Table'),'Table'[Specialty]=EARLIER('Table'[Specialty])&&'Table'[Month_Year]=EARLIER('Table'[Month_Year])),[Duration]))

    Table 2 =
    var _table1=
    SUMMARIZE(
        'Test','Test'[Specialty],
        "Month_Year","Avg duration",
        "Duration",AVERAGEX(FILTER(ALL('Test'),'Test'[Specialty]=EARLIER('Test'[Specialty])),[Duration]))
    var _table2=
    SUMMARIZE(
        'Test','Test'[Specialty],
        "Month_Year","Month vs Avg Var",
        "Duration",
        SUMX(FILTER(ALL('Test'),'Test'[Specialty]=EARLIER('Test'[Specialty])&&
        'Test'[Month_Year]=
        MAXX(FILTER(ALL('Table'),'Table'[YearMonth]=MAX('Table'[YearMonth])),[Month_Year])),[Duration])
        -
        AVERAGEX(FILTER(ALL('Test'),'Test'[Specialty]=EARLIER('Test'[Specialty])),[Duration]))
    RETURN
    UNION(
        'Test',_table1,_table2
    )

    As far as I know, Power BI's default sorting is based on alphabetical order, so it's not the effect you're looking for, we need to go and create a new table to do the sorting

    Sort_Table =
    var _table1=
     DISTINCT('Table 2'[Month_Year])
    return
    ADDCOLUMNS(
        _table1,"Index",
        SWITCH(
        TRUE(),
        [Month_Year]="Nov 2023",1,
        [Month_Year]="Dec 2023",2,
        [Month_Year]="Avg duration",3,
        [Month_Year]="Month vs Avg Var",4))

    3. Select [Month_Year] – Column tools – Sort by column – [lndex].

    4. Connecting tables to each other.

     

    5. Result:

    Best Regards,

    Liu Yang

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

    • Anonymous's avatar
      Anonymous
      Not applicable

      Anonymous I can't thank you enough! I was breaking my head how to do it.