<?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 Understand filter context with FILTER, ALL and MIN  (compute a previous Value (no Date column!)) in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Understand-filter-context-with-FILTER-ALL-and-MIN-compute-a/m-p/782261#M4162</link>
    <description>&lt;P&gt;I've a fact table with a column &lt;EM&gt;cycle,&lt;/EM&gt; which refer to the cycle of the collected quantity.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to compare the total quantity of a cycle to the previous cycle. Here it's how the result look likes.&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Following measure is first defined&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;qty := SUM ( yield[quantity] )&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I first tried without success, to compute with the previous cycle number&lt;/P&gt;&lt;PRE&gt;previous cycle :=
VAR Cycle = VALUES(yield[cycle])

RETURN 
IF(COUNTROWS(Cycle) = 1;
    Cycle - 1
)&lt;/PRE&gt;&lt;P&gt;using the above measue with this one I got no number at all (last column on pivot, s. screenshot above)&lt;/P&gt;&lt;PRE&gt;quantity previous cycle wrong :=
CALCULATE ( 
    [qty]; 
    FILTER (
        ALL ( yield[cycle] );
        yield[cycle] = [previous cycle]
    )
)&lt;/PRE&gt;&lt;P&gt;I presume, that the filter context on the cycle column is not removed so.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;after struggling quite a bit, I found this solution&lt;/P&gt;&lt;PRE&gt;quantity previous cycle :=
CALCULATE ( 
    [qty]; 
    FILTER (
        ALL ( yield[cycle] );
        MIN(yield[cycle]) - 1 = yield[cycle]
    )
)&lt;/PRE&gt;&lt;P&gt;With this last measure, I get exactly what I need, however I don't understand completely WHY it works.&lt;/P&gt;&lt;P&gt;So is my current understanding:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;ALL remove the filter context on the cycle column&lt;/LI&gt;&lt;LI&gt;MIN()&amp;nbsp; (btw, MAX() would work as well) has for me some magic there.&lt;UL&gt;&lt;LI&gt;it takes the minimum from the row context (in Pivot table, there is one single value, e.g. 4) -&amp;gt; scalar value&lt;/LI&gt;&lt;LI&gt;substract 1 of it (=3)&lt;/LI&gt;&lt;LI&gt;and filter through all Value of the filter context, the only single cycle I want to compare to&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Did I miss something? I would appreciate if an expert would correct me if I understood something wrong. Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 03 Sep 2019 10:58:29 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2019-09-03T10:58:29Z</dc:date>
    <item>
      <title>Understand filter context with FILTER, ALL and MIN  (compute a previous Value (no Date column!))</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Understand-filter-context-with-FILTER-ALL-and-MIN-compute-a/m-p/782261#M4162</link>
      <description>&lt;P&gt;I've a fact table with a column &lt;EM&gt;cycle,&lt;/EM&gt; which refer to the cycle of the collected quantity.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to compare the total quantity of a cycle to the previous cycle. Here it's how the result look likes.&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Following measure is first defined&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;qty := SUM ( yield[quantity] )&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I first tried without success, to compute with the previous cycle number&lt;/P&gt;&lt;PRE&gt;previous cycle :=
VAR Cycle = VALUES(yield[cycle])

RETURN 
IF(COUNTROWS(Cycle) = 1;
    Cycle - 1
)&lt;/PRE&gt;&lt;P&gt;using the above measue with this one I got no number at all (last column on pivot, s. screenshot above)&lt;/P&gt;&lt;PRE&gt;quantity previous cycle wrong :=
CALCULATE ( 
    [qty]; 
    FILTER (
        ALL ( yield[cycle] );
        yield[cycle] = [previous cycle]
    )
)&lt;/PRE&gt;&lt;P&gt;I presume, that the filter context on the cycle column is not removed so.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;after struggling quite a bit, I found this solution&lt;/P&gt;&lt;PRE&gt;quantity previous cycle :=
CALCULATE ( 
    [qty]; 
    FILTER (
        ALL ( yield[cycle] );
        MIN(yield[cycle]) - 1 = yield[cycle]
    )
)&lt;/PRE&gt;&lt;P&gt;With this last measure, I get exactly what I need, however I don't understand completely WHY it works.&lt;/P&gt;&lt;P&gt;So is my current understanding:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;ALL remove the filter context on the cycle column&lt;/LI&gt;&lt;LI&gt;MIN()&amp;nbsp; (btw, MAX() would work as well) has for me some magic there.&lt;UL&gt;&lt;LI&gt;it takes the minimum from the row context (in Pivot table, there is one single value, e.g. 4) -&amp;gt; scalar value&lt;/LI&gt;&lt;LI&gt;substract 1 of it (=3)&lt;/LI&gt;&lt;LI&gt;and filter through all Value of the filter context, the only single cycle I want to compare to&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Did I miss something? I would appreciate if an expert would correct me if I understood something wrong. Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Sep 2019 10:58:29 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Understand-filter-context-with-FILTER-ALL-and-MIN-compute-a/m-p/782261#M4162</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-09-03T10:58:29Z</dc:date>
    </item>
    <item>
      <title>Re: Understand filter context with FILTER, ALL and MIN  (compute a previous Value (no Date column!))</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Understand-filter-context-with-FILTER-ALL-and-MIN-compute-a/m-p/782558#M4170</link>
      <description>&lt;PRE&gt;[quantity previous cycle] :=
// SELECTEDVALUE returns the value as visible in the
// current context and blank if more than 1 value
// is visible.
var __currentCycle = SELECTEDVALUE( yield[cycle] )
// This code works because if __currentCycle is blank
// then __previousCycle is -1 and such a cycle
// does not exist, so blank will be returned
// as expected.
var __previousCycle = __currentCycle - 1
RETURN
// Please bear in mind that any filter created
// in CALCULATE OVERWRITES any filter on the
// same column that comes from outside. If you want
// to put the filters on the same column in an AND
// condition you have to use KEEPFILTERS.
	CALCULATE (
	    [qty],
	    // Filter returns the values in the cycle
	    // column that are equal to __previousCycle.
	    FILTER (
	    	// ALL returns all distinct values
	    	// in the cycle column disregarding
	    	// any current filters.
	        ALL ( yield[cycle] ),
	        // This filters all the cycle's
	        // for one cycle - the previous one.
	        yield[cycle] = __previousCycle
	    )
	)
	
// The above is fully equivalent to the following:

[quantity previous cycle] :=
// SELECTEDVALUE returns the value as visible in the
// current context and blank if more than 1 value
// is visible.
var __currentCycle = SELECTEDVALUE( yield[cycle] )
// This code works because if __currentCycle is blank
// then __previousCycle is -1 and such a cycle
// does not exist, so blank will be returned
// as expected.
var __previousCycle = __currentCycle - 1
RETURN
	CALCULATE (
	    [qty],
	    yield[cycle] = __previousCycle
	)
&lt;/PRE&gt;
&lt;P&gt;Best&lt;/P&gt;
&lt;P&gt;D.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Sep 2019 16:32:50 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Understand-filter-context-with-FILTER-ALL-and-MIN-compute-a/m-p/782558#M4170</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-09-03T16:32:50Z</dc:date>
    </item>
    <item>
      <title>Re: Understand filter context with FILTER, ALL and MIN  (compute a previous Value (no Date column!))</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Understand-filter-context-with-FILTER-ALL-and-MIN-compute-a/m-p/783178#M4195</link>
      <description>&lt;P&gt;Thank you for the excellent alternative with SELECTEDVALUE and the use of variables. I didn't know this function resp. I'm still not so familiar with using variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here for the sake of completeness your last version, who works with Power Pivot / Excel 2016 as well (SELECTEDVALUE not available):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;[quantity previous cycle] :=
var __currentCycle = IF ( HASONEVALUE( yield[cycle] ), VALUES ( yield[cycle] ) )
var __previousCycle = __currentCycle - 1
RETURN
	CALCULATE (
	    [qty],
	    yield[cycle] = __previousCycle
	)&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Sep 2019 07:31:10 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Understand-filter-context-with-FILTER-ALL-and-MIN-compute-a/m-p/783178#M4195</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-09-04T07:31:10Z</dc:date>
    </item>
  </channel>
</rss>

