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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
enieuwer
Frequent Visitor

Find the next nearest value with filtering

Hi All,

 

I have a table that consists of objects. These objects consist of several parts that are numbered (ascending). 
What I would like to do for each 'short' part is to find the nearest (ascending) 'long' part. 

I tried to visualize it by the image below, where 'Next long Object_Part' is the outcome I am trying to retrieve.

 

Because my datafile consist of hundreds of Objects and thousands of Parts it has to be robust. I already tried to do so by declaring a variable, but I keep getting 'time out' messages.

 

Thanks in advance!

Knipsel.PNG

1 REPLY 1
daxer-almighty
Solution Sage
Solution Sage

You're getting a timeout because such things should be calculated in Power Query, not in DAX.

 

 

// Objects
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIE4uKM/KISIO0Yb6gUqwMRN0IRN4KLGwNxTn5eOljYGC5sgqLcBC5uiqzcFCzshGarE9RWJzRbnaC2OkFtRYgbw8VNEMY7gWyNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Object = _t, Part = _t, Short_Long = _t, Object_Part = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Object", type text}, {"Part", Int64.Type}, {"Short_Long", type text}, {"Object_Part", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "NextLongPart", 
        each
            if _[Short_Long] = "long" then null else
            let
                CurrentObject = _[Object],
                NextLongPart = List.Min(
                    Table.SelectRows(
                        #"Changed Type",
                        (r) => 
                            r[Object] = _[Object]  // rows with same object
                            and 
                            r[Short_Long] = "long" // which are long
                            and
                            r[Part] > _[Part] // and whose Part is after the current row's Part
                    )[Part],
                    null
                ),
                ObjectPart = if NextLongPart <> null then
                    Table.SelectRows(
                        #"Changed Type",
                        each [Object] = CurrentObject and [Part] = NextLongPart
                    )[Object_Part]{0}
                else null
            in
                ObjectPart
    )
in
    #"Added Custom"

 

 

Put this in your Advanced Editor in PQ and see it work.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

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.