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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

Max gap between 2 values using DAX

Hi all,

 

I am trying find the maximum gap between 2 values, but that excludes Saturday and Sunday.

 

Eg: 

 SunMonTueWedThurFriSat
1FTFFFFF
2FTTTFFF
3FTTTTTF

 

In the above table, I would like to calculte the max gap between 2 T's (Sat and Sun excluded)

So in the first row, the max gap will be 5

In the second row the max gap will be 3

In the third row the max gap will be 1

 

Can I please if it is possible to implement this in DAX?

If so, can i please understand on how to implement?

 

Any help is appreciated!

 

Thank you!

Megha

1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hi @Anonymous ,

 

In your sample, try to create a custom column in Power Query Editor like so:

= List.Count(List.Select(Record.ToList(Record.SelectFields(_,{"Monday","Tuesday","Wednesday","Thursday","Friday"})), each _ =  "F"))+1

Icey_1-1647325452800.png

 

Icey_0-1647325421723.png

 

In addition, here is the complete query and you can create a blank query and paste it.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXID4hAojYpjdaKVjJBUhGCoBKkwxqICoTI2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Sunday = _t, Monday = _t, Tuesday = _t, Wednesday = _t, Thursday = _t, Friday = _t, Saturday = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", Int64.Type}, {"Sunday", type text}, {"Monday", type text}, {"Tuesday", type text}, {"Wednesday", type text}, {"Thursday", type text}, {"Friday", type text}, {"Saturday", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Max Gap", each List.Count(List.Select(Record.ToList(Record.SelectFields(_,{"Monday","Tuesday","Wednesday","Thursday","Friday"})), each _ =  "F"))+1)
in
    #"Added Custom"

 

 

Best Regards,

Icey

 

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

View solution in original post

3 REPLIES 3
Jihwan_Kim
Super User
Super User

Hi,

I am not sure if I understood your question correctly, but please check the below picture and the attached pbix file.

 

Picture4.png

 

Max gap between two Ts: =
IF (
    HASONEVALUE ( Data[Row Index] ),
    CALCULATE (
        COUNTROWS ( 'Weekday' ),
        FILTER ( RELATEDTABLE ( Data ), Data[Value] <> "T" )
    ) - 1
)

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.
Anonymous
Not applicable

Thank you! @Jihwan_Kim 

 

The table is in this format and there are multiple values. 

Megha3012_0-1646994237949.png

Is there a way I can get the maximum gap in the same way without changing the format?

 

Megha3012_1-1646994458087.png

 

Thank you!

Megha

Icey
Community Support
Community Support

Hi @Anonymous ,

 

In your sample, try to create a custom column in Power Query Editor like so:

= List.Count(List.Select(Record.ToList(Record.SelectFields(_,{"Monday","Tuesday","Wednesday","Thursday","Friday"})), each _ =  "F"))+1

Icey_1-1647325452800.png

 

Icey_0-1647325421723.png

 

In addition, here is the complete query and you can create a blank query and paste it.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXID4hAojYpjdaKVjJBUhGCoBKkwxqICoTI2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Sunday = _t, Monday = _t, Tuesday = _t, Wednesday = _t, Thursday = _t, Friday = _t, Saturday = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", Int64.Type}, {"Sunday", type text}, {"Monday", type text}, {"Tuesday", type text}, {"Wednesday", type text}, {"Thursday", type text}, {"Friday", type text}, {"Saturday", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Max Gap", each List.Count(List.Select(Record.ToList(Record.SelectFields(_,{"Monday","Tuesday","Wednesday","Thursday","Friday"})), each _ =  "F"))+1)
in
    #"Added Custom"

 

 

Best Regards,

Icey

 

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

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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