Forum Discussion

DontLikeMondays's avatar
DontLikeMondays
Regular Visitor
1 year ago
Solved

Solution required for finding 'data gap' in table and adding a new row showing the correct status

Hi all,

I am in need of some help please.

We have a table where we log PC 1 and PC 2 operational status.

We have Columns: PC Name (PC01, PC02), status (Available, Unavailable or Working), Start Time (Date/Time) and End Time (Date/Time).

I need to find the 'data gaps' where the Start Time of a new status does not equal the End Time of the last status of that PC,

then add a new row to the table showing the 'data gap' period as status 'Unavailable'.

 

 

The above is the table we currently have and highlighted in red is the 'data gap'.

 

 

The above is what i need to achieve, an added row with the 'data gap' showing in status 'Unavailable' as highlighted in green.

 

Any help is very much appreciated ๐Ÿ™‚

Thanks ๐Ÿ™‚

 

  • Hi DontLikeMondays 

    Thanks for Posting in Microsoft forum Community. 

     

    As per the Query you mentioned for Solution required for finding 'data gap' in table and adding a new row showing the correct status. The provided DAX query is designed to find gaps in a table's data where the StartTime of a new row does not immediately follow the EndTime of the previous row for the same PC Name. It then creates new rows to fill those gaps, assigning them the status "Ideal". Here's a breakdown of how the code works: 

     

    Step1: First it Creates a new intermediate table (GapRows) that includes additional calculated columns: 
    VAR GapRows = 

        ADDCOLUMNS( 

            'Data',  // Table name is 'Data' 

            "NextStartTime", 

            CALCULATE( 

                MIN('Data'[StartTime]),  // Get the next StartTime for the same PC Name 

                FILTER( 

                    'Data',  // Filtering to get the next row 

                    'Data'[PC Name] = EARLIER('Data'[PC Name]) && 

                    'Data'[StartTime] > EARLIER('Data'[StartTime])  // Ensure we only look ahead 

                ) 

            ), 

            "CurrentEndTime", 'Data'[EndTime]  // Rename existing EndTime to avoid conflict 

        ) 

     
    Step2: Identifies rows where there is a gap between the CurrentEndTime of a row and the NextStartTime of the next row for the same PC Name: 

    VAR IdealRows = 

        SELECTCOLUMNS( 

            FILTER( 

                GapRows, 

                NOT ISBLANK([NextStartTime]) && [CurrentEndTime] < [NextStartTime]  // Detect gaps 

            ), 

            "PC Name", [PC Name], 

            "Status", "Ideal",  // Add status as "Ideal" for the gap rows 

            "StartTime", [CurrentEndTime],  // Start time of the "Ideal" row is the current EndTime 

            "EndTime", [NextStartTime]  // End time of the "Ideal" row is the next StartTime 

        ) 

     

     
    Step3: Combine Original and Gap Rows: 

                        RETURN 

                               UNION( 

                                'Data',  // Combine original data 

                                    IdealRows  // Combine with the newly generated Ideal rows 

                                         ) 

    Using the above code, I obtained the expected output. I opted to use "Ideal" instead of "Unavailable" to enhance clarity and understanding. 

     

    By utilizing conditional formatting, I was able to highlight the desired results. 

     

     Using the Background Color feature, I set a rule to get the desired results. 

    If this post helps, then please give us Kudos and consider Accept it as a solution to help the other members find it more quickly. 

    Thank you.

     

10 Replies

  • v-saisrao-msft's avatar
    v-saisrao-msft
    Community Support

    Hi DontLikeMondays 

    Thanks for Posting in Microsoft forum Community. 

     

    As per the Query you mentioned for Solution required for finding 'data gap' in table and adding a new row showing the correct status. The provided DAX query is designed to find gaps in a table's data where the StartTime of a new row does not immediately follow the EndTime of the previous row for the same PC Name. It then creates new rows to fill those gaps, assigning them the status "Ideal". Here's a breakdown of how the code works: 

     

    Step1: First it Creates a new intermediate table (GapRows) that includes additional calculated columns: 
    VAR GapRows = 

        ADDCOLUMNS( 

            'Data',  // Table name is 'Data' 

            "NextStartTime", 

            CALCULATE( 

                MIN('Data'[StartTime]),  // Get the next StartTime for the same PC Name 

                FILTER( 

                    'Data',  // Filtering to get the next row 

                    'Data'[PC Name] = EARLIER('Data'[PC Name]) && 

                    'Data'[StartTime] > EARLIER('Data'[StartTime])  // Ensure we only look ahead 

                ) 

            ), 

            "CurrentEndTime", 'Data'[EndTime]  // Rename existing EndTime to avoid conflict 

        ) 

     
    Step2: Identifies rows where there is a gap between the CurrentEndTime of a row and the NextStartTime of the next row for the same PC Name: 

    VAR IdealRows = 

        SELECTCOLUMNS( 

            FILTER( 

                GapRows, 

                NOT ISBLANK([NextStartTime]) && [CurrentEndTime] < [NextStartTime]  // Detect gaps 

            ), 

            "PC Name", [PC Name], 

            "Status", "Ideal",  // Add status as "Ideal" for the gap rows 

            "StartTime", [CurrentEndTime],  // Start time of the "Ideal" row is the current EndTime 

            "EndTime", [NextStartTime]  // End time of the "Ideal" row is the next StartTime 

        ) 

     

     
    Step3: Combine Original and Gap Rows: 

                        RETURN 

                               UNION( 

                                'Data',  // Combine original data 

                                    IdealRows  // Combine with the newly generated Ideal rows 

                                         ) 

    Using the above code, I obtained the expected output. I opted to use "Ideal" instead of "Unavailable" to enhance clarity and understanding. 

     

    By utilizing conditional formatting, I was able to highlight the desired results. 

     

     Using the Background Color feature, I set a rule to get the desired results. 

    If this post helps, then please give us Kudos and consider Accept it as a solution to help the other members find it more quickly. 

    Thank you.

     

  • Hi DontLikeMondays 

     

    Please post a workable sample data and not an image. A link to an Excel file or a sanitized copy of your pbix will do.

  • v-saisrao-msft's avatar
    v-saisrao-msft
    Community Support

    hi DontLikeMondays 

    Just following up as we havenโ€™t received a response to our previous message. We hope your issue has been resolved.

    If the solution we provided has worked for you, kindly mark it as the accepted solution.

    Thank you for being a valued member of the Microsoft Fabric Community Forum!