Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

How to find Date ranges NOT intersecting

Hello Everyone I am looking for a little help with this one if you don't mind. 

 

I have start and end datetimes for a server being online. I want to return a table for the start and end datetimes the server was not online (over a large datetime range). I have a single table with all date ranges and status in. 

 

Annotation 2020-09-07 211813.png

 

Here is the file https://1drv.ms/x/s!AhxwTE9UMcGrhHaTgy3_M-ZoXBXY?e=kxavAg

 

Any pointers would be really appreciated. 

 

Many thanks in advance


Will

 

1 ACCEPTED SOLUTION

Hi, @Anonymous , to my understanding, you'd like a table cleansed by PQ. Pls refer to this,

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZdNbhsxDIWvMvC2scE/aShdJciyAQoU6f135Ywsx+3QGjHwjsQXkuKT3ry/XxYCgivkK9GCVBNU0B+AFWC5vP0TJagsNeVbUbC/lmQ5f75+//r6efl4e4bJAlqZX8DSAlBTqWmdgln6Fq6QPVjuUbppPoflM5gVzRV5qrJ1QalMVdw2tbc5NzPdK8NKL2DElaZhZcFU7UCZPVhZiOZhDAuuWzqDA2tRgYrlHLZeAbfVSFIJDzCLUmBmLX2fGYsH4xiMO2z1YBJY2pbeYMc9s+iuAJmurCsA3ZnlWGV5WNkam9naYcmDRRTQ0geVlRisjGAIoZlt6a/bRIzBsAvd2zOkGIzuq5E8BVjUroF5GG9vgKC/Z1tU9ltjDibbjWX/G4sHS6HTxDQSOuaFrGi7ueUm5RzWFGCdurA1dgBrh7kz0xisKSD7lyPGFIBdAeIdAEEIRg8FuLCYAgjvM3P3jGIKoKYA8a9t4hiMR0Inic1MhrCYAiiN7jOKvQGUh5XF3gBah5XF3oC7C7LVcE+zxNosdxh4bXJMAQy9sqMLsijGYP0NcJyj7h6ptylwAmvprc2j2dOYpdInS3WcmcYslT5ZKhcWkZN2S2VtHg9A75ZqHjaQkz5ZqjlYf1DQPYDIg6LdUklFt7KHnGQK9rBURzlpzFJpt1SuAjRmqbRbKns3vTbNI9n3Zsr2/XLLLKcwHJ1myFJpt1QvlhZjCkDuM3NhMQVgU4B92B2NS4vaN9rsZ/V+x+w/h8Xf0Rv8dwN9fjbYx18=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [start = _t, end = _t, status = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"start", type datetimezone}, {"end", type datetimezone}, {"status", type text}}),

    //Sorting table by "start" for possible time overlap
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"start", Order.Ascending}}),
    tb = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1),
    #"Added Custom" = Table.AddColumn(tb, "Custom", each 
        [
            //in case of time overlap, Start of "offline" period is null
            Start = if tb[end]{[Index]} < tb[start]{[Index] + 1} then tb[end]{[Index]} else null,
            End = if Start is null then null else tb[start]{[Index]+1},
            Status = "Offline"
        ]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"start", "end", "status", "Index"}),
    #"Expanded Custom" = Table.ExpandRecordColumn(#"Removed Columns", "Custom", {"Start", "End", "Status"}, {"Start", "End", "Status"}),

    //filter out null and error at your own will
    #"Filtered Rows" = Table.SelectRows(#"Expanded Custom", each try [Start] <> null otherwise false)
in
    #"Expanded Custom"

As there are time overlaps in the dataset, time overlap examine should be conducted.

Overlap.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

5 REPLIES 5
Icey
Community Support
Community Support

Hi @Anonymous ,

 

Is this problem solved?

 

 

Best Regards,

Icey

Greg_Deckler
Super User
Super User

@Anonymous I haven't had a chance to post this to the Quick Measures Gallery yet but seems like you want this but in reverse, should be fairly easy to modify. This is for finding the total amount of time between overlapping appointments. 

Overlap

 

 

Overlap = 
    VAR __Start = MIN('Table'[Start])
    VAR __End = MAX('Table'[End])
RETURN
    IF(ISBLANK(MAX([Start])),BLANK(),
        VAR __Table = GENERATESERIES(__Start,__End,1/24/60)
        VAR __Table1 = ALL('Table')
        VAR __Table2 = GENERATE(__Table,__Table1)
        VAR __Table3 = ADDCOLUMNS(__Table2,"Include",IF([Value]>=[Start] && [Value] <= [End],1,0))
        VAR __Table4 = GROUPBY(__Table3,[Value],"Minute",MAXX(CURRENTGROUP(),[Include]))
    RETURN
        SUMX(__Table4,[Minute])/60)

 

 

I'm thinking:

Not Included:

 

 

Not Included = 
    VAR __Start = MIN('Table'[Start])
    VAR __End = MAX('Table'[End])
RETURN
    IF(ISBLANK(MAX([Start])),BLANK(),
        VAR __Table = GENERATESERIES(__Start,__End,1/24/60)
        VAR __Table1 = ALL('Table')
        VAR __Table2 = GENERATE(__Table,__Table1)
        VAR __Table3 = ADDCOLUMNS(__Table2,"Include",IF(COUNTROWS(FILTER(__Table1,[Value]>=[Start] && [Value] <= [End])>=1)),0,1))
        VAR __Table4 = GROUPBY(__Table3,[Value],"Minute",MAXX(CURRENTGROUP(),[Include]))
    RETURN
        SUMX(__Table4,[Minute])/60)

 

 

Let me know if that works and I'll include it when I post to the quick measures gallery. PBIX is attached below sig (has the Overlap version in it)

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Many thanks @Greg_Deckler, that is super useful but not exactly what i was looking for. I don't think i was as clear as i should have been. 

 

The output i want is a table, start, end datetimes with an 'offline/online' flag status. So creating an 'anti-table' and then appending to the initial 'online' set of start and end datetimes. 

 

Many thanks


Will

Hi, @Anonymous , to my understanding, you'd like a table cleansed by PQ. Pls refer to this,

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZdNbhsxDIWvMvC2scE/aShdJciyAQoU6f135Ywsx+3QGjHwjsQXkuKT3ry/XxYCgivkK9GCVBNU0B+AFWC5vP0TJagsNeVbUbC/lmQ5f75+//r6efl4e4bJAlqZX8DSAlBTqWmdgln6Fq6QPVjuUbppPoflM5gVzRV5qrJ1QalMVdw2tbc5NzPdK8NKL2DElaZhZcFU7UCZPVhZiOZhDAuuWzqDA2tRgYrlHLZeAbfVSFIJDzCLUmBmLX2fGYsH4xiMO2z1YBJY2pbeYMc9s+iuAJmurCsA3ZnlWGV5WNkam9naYcmDRRTQ0geVlRisjGAIoZlt6a/bRIzBsAvd2zOkGIzuq5E8BVjUroF5GG9vgKC/Z1tU9ltjDibbjWX/G4sHS6HTxDQSOuaFrGi7ueUm5RzWFGCdurA1dgBrh7kz0xisKSD7lyPGFIBdAeIdAEEIRg8FuLCYAgjvM3P3jGIKoKYA8a9t4hiMR0Inic1MhrCYAiiN7jOKvQGUh5XF3gBah5XF3oC7C7LVcE+zxNosdxh4bXJMAQy9sqMLsijGYP0NcJyj7h6ptylwAmvprc2j2dOYpdInS3WcmcYslT5ZKhcWkZN2S2VtHg9A75ZqHjaQkz5ZqjlYf1DQPYDIg6LdUklFt7KHnGQK9rBURzlpzFJpt1SuAjRmqbRbKns3vTbNI9n3Zsr2/XLLLKcwHJ1myFJpt1QvlhZjCkDuM3NhMQVgU4B92B2NS4vaN9rsZ/V+x+w/h8Xf0Rv8dwN9fjbYx18=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [start = _t, end = _t, status = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"start", type datetimezone}, {"end", type datetimezone}, {"status", type text}}),

    //Sorting table by "start" for possible time overlap
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"start", Order.Ascending}}),
    tb = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1),
    #"Added Custom" = Table.AddColumn(tb, "Custom", each 
        [
            //in case of time overlap, Start of "offline" period is null
            Start = if tb[end]{[Index]} < tb[start]{[Index] + 1} then tb[end]{[Index]} else null,
            End = if Start is null then null else tb[start]{[Index]+1},
            Status = "Offline"
        ]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"start", "end", "status", "Index"}),
    #"Expanded Custom" = Table.ExpandRecordColumn(#"Removed Columns", "Custom", {"Start", "End", "Status"}, {"Start", "End", "Status"}),

    //filter out null and error at your own will
    #"Filtered Rows" = Table.SelectRows(#"Expanded Custom", each try [Start] <> null otherwise false)
in
    #"Expanded Custom"

As there are time overlaps in the dataset, time overlap examine should be conducted.

Overlap.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Anonymous
Not applicable

link not working properly, try this. https://1drv.ms/u/s!AhxwTE9UMcGrhHjf-GwydzTcPw5L?e=A84SzD

 

Thanks Will

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors