Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Time line with data?

Hi!   I need to make a timeline, and count how many rows that are "active" for each hour..    Today i have a table with   RowID, Startdatetime, Enddatetime   I also have a table with dates, a...
  • Anonymous's avatar
    Anonymous
    6 years ago

    I have found a great solution with DAX in Power BI. Will try to walk you through it: 

     

    1. I have a table with data, that has a "startdatetime" and "enddatetime". And i want to make a historical view of these, and count how many is "active" each hour. 

    2. I create a table for date and time structure like this (as an example i found on this forum):

    Dates = SELECTCOLUMNS(
        CROSSJOIN(
            
            //************************************************************
            // Date range below. This will generate a table with a [Date] column
            //************************************************************
            CALENDAR(min(LivedataAkuten[KONTAKTSTARTDATUM]);max(LivedataAkuten[KONTAKTSTARTDATUM]));
    
            //************************************************************
            // Time granduality below. This will generate a table with a [Time] column
            //************************************************************
            SELECTCOLUMNS(
                CROSSJOIN(
                    SELECTCOLUMNS(GENERATESERIES(0;0;1);"Second";[Value]);
                    SELECTCOLUMNS(GENERATESERIES(0;0;1);"Minute";[Value]);
                    SELECTCOLUMNS(GENERATESERIES(0;23;1);"Hour";[Value])
                );
                "Time"; TIME([Hour];[Minute];[Second])
            )
        );
        //************************************************************
        // Custom table columns below. 
        // This is extracted from the Date and Time columns generated above
        //************************************************************
        "Date"; FORMAT([Date]; "YYYY-MM-DD");
        "Time"; FORMAT([Time]; "hh:mm:ss");
        "DateTime"; [Date]+[Time]
    )

    3. I create a new table with dates, and groupinginformation like this

    AktivaSamtidigt = 
    CROSSJOIN(Dates;DISTINCT(Table[GroupOfAccounts]))

    4. In that table i add a column with this script

    Antal = 
    VAR Antalet = COUNTX(FILTER(Table;Table[STARTDATE]<=AktivaSamtidigt[DateTime]&&Table[ENDDATE]>AktivaSamtidigt[DateTime]+TIME(1;0;0)&&Table[GroupOfAccounts]=AktivaSamtidigt[GroupOfAccounts]);Table[ContactID])
    RETURN
    IF(ISBLANK(Antalet); 0; Antalet)

     

    From this I can then create a visual presentation like this:

    This is working way faster than scripting with the crossjoin as I did before!

     

    So! That's a good friyay evening!