<?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 Re: Nested DAX iteration with multiple conditions in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Nested-DAX-iteration-with-multiple-conditions/m-p/3023088#M102940</link>
    <description>&lt;P&gt;Firstly split the customers and shops into separate tables, each linked in a one-to-many relationship with the orders table. This avoids any possible problems with auto exist. Then you can use the below code to generate a table&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;VAR FirstOrderEver = MIN( 'Orders'[Order Date] )
VAR YellowOrders =
	CALCULATETABLE(
		SUMMARIZE(
			'Orders',
			'Customers'[Customer],
			'Shops'[Office],
			'Orders'[Expiry Date]
		),
		TREATAS( { "Yellow" }, 'Orders'[Product] )
	)
VAR YellowOrdersWithAllDates =
	SELECTCOLUMNS(
		GENERATE(
			YellowOrders,
			DATESBETWEEN(
				'Orders'[Order Date],
				FirstOrderEver,
				'Orders'[Expiry Date]
			)
		),
		"Customer", 'Customers'[Customer],
		"Office", 'Shops'[Office],
		"Order Date", 'Orders'[Order Date]
	)
VAR RedOrders =
	CALCULATETABLE(
		VALUES( 'Orders'[Order ID] ),
		YellowOrdersWithAllDates,
		TREATAS( { "Red" }, 'Orders'[Product] )
	)
RETURN
	RedOrders&lt;/LI-CODE&gt;</description>
    <pubDate>Mon, 16 Jan 2023 11:46:59 GMT</pubDate>
    <dc:creator>johnt75</dc:creator>
    <dc:date>2023-01-16T11:46:59Z</dc:date>
    <item>
      <title>Nested DAX iteration with multiple conditions</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Nested-DAX-iteration-with-multiple-conditions/m-p/3023017#M102932</link>
      <description>&lt;P&gt;I am hopeful you will be able to help me or point me in the right direction to work out a DAX formula to return a calculated table.&lt;/P&gt;&lt;P&gt;I have spend hours trying to figure it out but but have hit a wall and cannot move further.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For the purpose of this illustration, I have a simple table which contains orders. Customer can purchase an order from two different shops. I am trying to work out which are the order numbers of red products which have been purchased before the max expiry date of a yellow product, if sold to the same customer at the same shop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My table is as follows:&lt;/P&gt;&lt;PRE&gt;Order ID    Office  Customer    Order Date  Expiry Date Product
       1    Shop1   Cust1       02/02/2022  27/08/2022  Red
       2    Shop1   Cust1       15/06/2021  04/02/2022  Red
       3    Shop1   Cust1       30/09/2022  29/04/2023  Blue
       4    Shop1   Cust1       07/05/2021  18/12/2021  Yellow
       5    Shop1   Cust2       30/05/2021  23/05/2022  Red
       6    Shop2   Cust2       08/02/2022  13/01/2023  Yellow
       7    Shop1   Cust2       03/09/2022  13/04/2023  Blue
       8    Shop1   Cust3       24/04/2021  11/07/2021  Yellow
       9    Shop1   Cust3       23/02/2022  21/01/2023  Yellow
      10    Shop1   Cust3       03/06/2022  24/11/2022  Blue
      11    Shop1   Cust3       04/09/2021  28/08/2022  Red
      12    Shop1   Cust3       05/09/2021  28/08/2022  Red&lt;/PRE&gt;&lt;P&gt;The desired output of the calculated table is as follows:&lt;/P&gt;&lt;PRE&gt;Order ID
       2
      11
      12&lt;/PRE&gt;&lt;P&gt;As explained, I need to retrieve all the order IDs for the red product purchased by the same customer who has also purchased a yellow product at the same shop as the red product and where the max expiry date of the yellow product is after the order date of the red product.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My table has the following Red products and here is the explanation why they should/shouldn't be included:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Order ID 1 - Don't flag this as max expiry date of Yellow product (order #4 - 18/12/21) is before the order date of Red product (order #1 - 02/02/22)&lt;/LI&gt;&lt;LI&gt;Order ID 2 - Flag this as max expiry date of the Yellow product (order #4 - 18/12/21) is after the order date of Red product (order #2 - 15/06/21)&lt;/LI&gt;&lt;LI&gt;Order ID 5 - Don't flag it as the Yellow product (order #6) was sold in a different Shop as Red product (there is no max expiry date of Yellow product in the same shop as the Red product order #5)&lt;/LI&gt;&lt;LI&gt;Order ID 11 - Flag this as the max expiry date of Yellow product (order #9) is after the order date of Red product (order #11)&lt;/LI&gt;&lt;LI&gt;Order ID 12 - Flag this as the max expiry date of Yellow product (order #9) is after the order date of Red product (order #12)&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Hope the above example is clear.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your advice how to achieve this will be greatly appreciated.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Jan 2023 11:07:27 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Nested-DAX-iteration-with-multiple-conditions/m-p/3023017#M102932</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-01-16T11:07:27Z</dc:date>
    </item>
    <item>
      <title>Re: Nested DAX iteration with multiple conditions</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Nested-DAX-iteration-with-multiple-conditions/m-p/3023088#M102940</link>
      <description>&lt;P&gt;Firstly split the customers and shops into separate tables, each linked in a one-to-many relationship with the orders table. This avoids any possible problems with auto exist. Then you can use the below code to generate a table&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;VAR FirstOrderEver = MIN( 'Orders'[Order Date] )
VAR YellowOrders =
	CALCULATETABLE(
		SUMMARIZE(
			'Orders',
			'Customers'[Customer],
			'Shops'[Office],
			'Orders'[Expiry Date]
		),
		TREATAS( { "Yellow" }, 'Orders'[Product] )
	)
VAR YellowOrdersWithAllDates =
	SELECTCOLUMNS(
		GENERATE(
			YellowOrders,
			DATESBETWEEN(
				'Orders'[Order Date],
				FirstOrderEver,
				'Orders'[Expiry Date]
			)
		),
		"Customer", 'Customers'[Customer],
		"Office", 'Shops'[Office],
		"Order Date", 'Orders'[Order Date]
	)
VAR RedOrders =
	CALCULATETABLE(
		VALUES( 'Orders'[Order ID] ),
		YellowOrdersWithAllDates,
		TREATAS( { "Red" }, 'Orders'[Product] )
	)
RETURN
	RedOrders&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 16 Jan 2023 11:46:59 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Nested-DAX-iteration-with-multiple-conditions/m-p/3023088#M102940</guid>
      <dc:creator>johnt75</dc:creator>
      <dc:date>2023-01-16T11:46:59Z</dc:date>
    </item>
    <item>
      <title>Re: Nested DAX iteration with multiple conditions</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Nested-DAX-iteration-with-multiple-conditions/m-p/3023090#M102941</link>
      <description>&lt;P&gt;Anonymous&lt;/LI-USER&gt;&amp;nbsp;Hi!&lt;/P&gt;
&lt;P&gt;Use the following code to calculate a new column:&lt;/P&gt;
&lt;DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;Acc Date = &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;VAR&lt;/SPAN&gt; &lt;SPAN&gt;MAXDATEYELLOW&lt;/SPAN&gt;&lt;SPAN&gt; = &lt;/SPAN&gt;&lt;SPAN&gt;CALCULATE&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;MAX&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;'Table'[Expiry Date]&lt;/SPAN&gt;&lt;SPAN&gt;), &lt;/SPAN&gt;&lt;SPAN&gt;ALLEXCEPT&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;'Table'&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;SPAN&gt;'Table'[Customer]&lt;/S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;IF&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;'Table'[Order Date]&lt;/SPAN&gt;&lt;SPAN&gt; &amp;lt; &lt;/SPAN&gt;&lt;SPAN&gt;MAXDATEYELLOW&lt;/SPAN&gt;&lt;SPAN&gt; &amp;amp;&amp;amp; &lt;/SPAN&gt;&lt;SPAN&gt;COUNTPROD&lt;/SPAN&gt;&lt;SPAN&gt; = &lt;/SPAN&gt;&lt;SPAN&gt;2&lt;/SPAN&gt;&lt;SPAN&gt; &amp;amp;&amp;amp; &lt;/SPAN&gt;&lt;SPAN&gt;'Table'[Product]&lt;/SPAN&gt;&lt;SPAN&gt; = &lt;/SPAN&gt;&lt;SPAN&gt;"Red"&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;Returns 1 when all conditions described by you are accepted, so records are to be kept, returns 0 for records to be discarded.&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;If that's correct, accept my answer as a solution!&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;BBF&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Mon, 16 Jan 2023 11:47:09 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Nested-DAX-iteration-with-multiple-conditions/m-p/3023090#M102941</guid>
      <dc:creator>BeaBF</dc:creator>
      <dc:date>2023-01-16T11:47:09Z</dc:date>
    </item>
    <item>
      <title>Re: Nested DAX iteration with multiple conditions</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Nested-DAX-iteration-with-multiple-conditions/m-p/3023107#M102944</link>
      <description>&lt;P&gt;Hi&amp;nbsp;Anonymous&lt;/LI-USER&gt;&amp;nbsp;&lt;BR /&gt;Please refer to attached sample file with the solution&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;Table 2 = 
SELECTCOLUMNS ( 
    FILTER ( 
        'Table',
        VAR CurrentOfficeCustTable = 
            CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[Office], 'Table'[Customer] ) )
        VAR YellowRecords =
            FILTER ( CurrentOfficeCustTable, 'Table'[Product] = "Yellow" )
        VAR LastYellowExpDate =
            MAXX ( YellowRecords, 'Table'[Expiry Date] )
        RETURN
            'Table'[Product] = "Red" &amp;amp;&amp;amp; 'Table'[Order Date] &amp;lt; LastYellowExpDate
    ),
    "Red Orders",
    'Table'[Order ID]
)&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 16 Jan 2023 11:53:53 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Nested-DAX-iteration-with-multiple-conditions/m-p/3023107#M102944</guid>
      <dc:creator>tamerj1</dc:creator>
      <dc:date>2023-01-16T11:53:53Z</dc:date>
    </item>
  </channel>
</rss>

