Forum Discussion

SebastianAlmer's avatar
SebastianAlmer
Frequent Visitor
5 years ago
Solved

identify overlapping entries

Dear all,   this might not be the best task for PowerBI but we have made so much great reports in our organization and would prevent to have this report in another tool. We want to identify in o...
  • PaulDBrown's avatar
    PaulDBrown
    5 years ago

    SebastianAlmer 

     

    If you wish to use Direct Query, you can (NB, I've added another overlapping row to date 2/03/2021 to check if all overlapping rows are returned) :

    1) Create the DateTime columns in the table using calculated columns with:

     

     

    StartTime = TableDQ[date] & " " & TableDQ[start_hour] & ":" & TableDQ[start_minute]

     

     

    And the equivalent for the EndTime column.  Set the Data type to Date/time:

    2) Create a measure to establish the row order by date/employee/task:

     

     

    Sum StartTime = SUM(TableDQ[StartTime])
    Order by date =
    RANKX (
        FILTER (
            ALLEXCEPT ( TableDQ, TableDQ[employee], TableDQ[date], TableDQ[Task] ),
            NOT ( ISBLANK ( [Sum StartTime] ) )
        ),
        [Sum StartTime],
        ,
        ASC
    )

     

    This gives you the following table visual:

    3) Next create measures to establish the StartDateTime and EndDateTime for the comparison:

     

    StartDateTime =
    VAR starttime =
        MAX ( TableDQ[StartTime] )
    RETURN
        CALCULATE (
            starttime,
            ALLEXCEPT ( 'TableDQ', 'TableDQ'[date], 'TableDQ'[employee], 'TableDQ'[Task] )
        )
    EndDateTime =
    VAR PrevPeriod = [Order by date] - 1
    RETURN
        CALCULATE (
            MAX ( TableDQ[EndTime] ),
            FILTER (
                ALLEXCEPT ( TableDQ, 'TableDQ'[date], 'TableDQ'[employee], TableDQ[Task] ),
                [Order by date] = PrevPeriod
            )
        )
    

     

    4) and finally the measure to identify the rows which overlap:

     

    Indentify overlapping DQ =
    VAR SelPeriod =
        CALCULATE (
            [Order by date],
            FILTER ( TableDQ, NOT ( ISBLANK ( [EndDateTime] ) ) )
        )
    VAR _table =
        CALCULATETABLE (
            VALUES ( TableDQ[date] ),
            FILTER (
                TableDQ,
                [Order by date] >= SelPeriod - 1
                    && [Order by date] <= SelPeriod
            )
        )
    VAR _date =
        CALCULATETABLE (
            VALUES ( 'TableDQ'[date] ),
            FILTER ( ALL ( TableDQ ), [StartDateTime] < [EndDateTime] )
        )
    RETURN
        COUNTROWS ( INTERSECT ( _table, _date ) )

     

    And you will get this: