<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Find the next nearest value with filtering in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Find-the-next-nearest-value-with-filtering/m-p/1957082#M42718</link>
    <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a table that consists of objects. These objects consist of several parts that are numbered (ascending).&amp;nbsp;&lt;BR /&gt;What I would like to do&amp;nbsp;for each 'short' part is to find the nearest (ascending) 'long' part.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to visualize it by the image below, where 'Next long Object_Part' is the outcome I am trying to retrieve.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 14 Jul 2021 10:11:16 GMT</pubDate>
    <dc:creator>enieuwer</dc:creator>
    <dc:date>2021-07-14T10:11:16Z</dc:date>
    <item>
      <title>Find the next nearest value with filtering</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Find-the-next-nearest-value-with-filtering/m-p/1957082#M42718</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a table that consists of objects. These objects consist of several parts that are numbered (ascending).&amp;nbsp;&lt;BR /&gt;What I would like to do&amp;nbsp;for each 'short' part is to find the nearest (ascending) 'long' part.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to visualize it by the image below, where 'Next long Object_Part' is the outcome I am trying to retrieve.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jul 2021 10:11:16 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Find-the-next-nearest-value-with-filtering/m-p/1957082#M42718</guid>
      <dc:creator>enieuwer</dc:creator>
      <dc:date>2021-07-14T10:11:16Z</dc:date>
    </item>
    <item>
      <title>Re: Find the next nearest value with filtering</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Find-the-next-nearest-value-with-filtering/m-p/1957417#M42740</link>
      <description>&lt;P&gt;You're getting a timeout because such things should be calculated in Power Query, not in DAX.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;// 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) =&amp;gt; 
                            r[Object] = _[Object]  // rows with same object
                            and 
                            r[Short_Long] = "long" // which are long
                            and
                            r[Part] &amp;gt; _[Part] // and whose Part is after the current row's Part
                    )[Part],
                    null
                ),
                ObjectPart = if NextLongPart &amp;lt;&amp;gt; null then
                    Table.SelectRows(
                        #"Changed Type",
                        each [Object] = CurrentObject and [Part] = NextLongPart
                    )[Object_Part]{0}
                else null
            in
                ObjectPart
    )
in
    #"Added Custom"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Put this in your Advanced Editor in PQ and see it work.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jul 2021 13:35:17 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Find-the-next-nearest-value-with-filtering/m-p/1957417#M42740</guid>
      <dc:creator>daxer-almighty</dc:creator>
      <dc:date>2021-07-14T13:35:17Z</dc:date>
    </item>
  </channel>
</rss>

