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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Understand filter context with FILTER, ALL and MIN (compute a previous Value (no Date column!))

I've a fact table with a column cycle, which refer to the cycle of the collected quantity.

 

I would like to compare the total quantity of a cycle to the previous cycle. Here it's how the result look likes.2019-09-03_12-35-13.jpg

 

Following measure is first defined

 

qty := SUM ( yield[quantity] )

 

I first tried without success, to compute with the previous cycle number

previous cycle :=
VAR Cycle = VALUES(yield[cycle])

RETURN 
IF(COUNTROWS(Cycle) = 1;
    Cycle - 1
)

using the above measue with this one I got no number at all (last column on pivot, s. screenshot above)

quantity previous cycle wrong :=
CALCULATE ( 
    [qty]; 
    FILTER (
        ALL ( yield[cycle] );
        yield[cycle] = [previous cycle]
    )
)

I presume, that the filter context on the cycle column is not removed so.

 

after struggling quite a bit, I found this solution

quantity previous cycle :=
CALCULATE ( 
    [qty]; 
    FILTER (
        ALL ( yield[cycle] );
        MIN(yield[cycle]) - 1 = yield[cycle]
    )
)

With this last measure, I get exactly what I need, however I don't understand completely WHY it works.

So is my current understanding:

  • ALL remove the filter context on the cycle column
  • MIN()  (btw, MAX() would work as well) has for me some magic there.
    • it takes the minimum from the row context (in Pivot table, there is one single value, e.g. 4) -> scalar value
    • substract 1 of it (=3)
    • and filter through all Value of the filter context, the only single cycle I want to compare to

Did I miss something? I would appreciate if an expert would correct me if I understood something wrong. Thanks!

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

[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
	)

Best

D.

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

[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
	)

Best

D.

Anonymous
Not applicable

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.

 

Here for the sake of completeness your last version, who works with Power Pivot / Excel 2016 as well (SELECTEDVALUE not available):

 

[quantity previous cycle] :=
var __currentCycle = IF ( HASONEVALUE( yield[cycle] ), VALUES ( yield[cycle] ) )
var __previousCycle = __currentCycle - 1
RETURN
	CALCULATE (
	    [qty],
	    yield[cycle] = __previousCycle
	)

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors