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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

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
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.