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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
ptmuldoon
Resolver I
Resolver I

Add Date Variable Filter to Calculate

I'm trying to add a Variable Date filter into a calculate function, and struggling to get this work.   I think/beleive it should be doable, but struggling if need to explicite include FILTER or not>

 

How do you swap the second filter of DATESINPERIOD withe MyPeriod variable?

 

Var Myitem = "OCCUPIED ROOMS"

    Var MyPeriod = 
        SWITCH (
		    VALUES ('Measures List'[Fact Period]),
	    	    "Last 7", DATESINPERIOD ( 'Dim Period'[Date], MAX ( 'Dim Period'[Date] ), -7, DAY ),
		        "MTD", DATESMTD( 'Dim Period'[Date] ),
		        "QTD", DATESQTD( 'Dim Period'[Date] ),
		        "YTD", DATESYTD( 'Dim Period'[Date] )
	    )

    RETURN

        SWITCH (
            VALUES ('FactType'[FactType]),
                "#", CALCULATE(
                    [Qty]
                    , 'Dim Item'[description]=Myitem 
                    //, MyPeriod  // does not work
                    //, 'Dim Period'=MyPeriod  // does not work
                    //, Filter('Dim Period','Dim Period'=MyPeriod)  // does not work
                    ,  DATESINPERIOD ( 'Dim Period'[Date], MAX ( 'Dim Period'[Date] ), -7, DAY) // this works
                ),

.........More code
1 ACCEPTED SOLUTION
ptmuldoon
Resolver I
Resolver I

I figured this out, and again it makes perfect sense once I wrap my head aroudn the logic of it.   The variables will (I think) always compute their values first before you get into the Calculate function.  So the 'trick' was to compute the number of days in the Switch Variable, and then pass that number into the Return/Calculate.

 

So this works and computes as expected.

Var MyPeriod = 
        SWITCH (
		    VALUES ('Measures List'[Fact Period]),
	    	    "Day", 1,
                "Last 7", 7,
		        "MTD", DATEDIFF( STARTOFMONTH('Dim Period'[Date]), MAX('Dim Period'[Date]), DAY ) + 1,
		        "QTD", DATEDIFF( STARTOFQUARTER('Dim Period'[Date]), MAX('Dim Period'[Date]), DAY ) + 1,
		        "YTD", DATEDIFF( STARTOFYEAR('Dim Period'[Date]), MAX('Dim Period'[Date]), DAY ) + 1 
	    )

    RETURN

        SWITCH (
            VALUES ('FactType'[FactType]),
                "#", CALCULATE(
                    [Qty]
                    , 'Dim Item','Dim Item'[description]=Myitem
                    ,  DATESINPERIOD ( 'Dim Period'[Date], MAX ( 'Dim Period'[Date] ), -VALUE(MyPeriod), DAY) // this works
                ),

View solution in original post

1 REPLY 1
ptmuldoon
Resolver I
Resolver I

I figured this out, and again it makes perfect sense once I wrap my head aroudn the logic of it.   The variables will (I think) always compute their values first before you get into the Calculate function.  So the 'trick' was to compute the number of days in the Switch Variable, and then pass that number into the Return/Calculate.

 

So this works and computes as expected.

Var MyPeriod = 
        SWITCH (
		    VALUES ('Measures List'[Fact Period]),
	    	    "Day", 1,
                "Last 7", 7,
		        "MTD", DATEDIFF( STARTOFMONTH('Dim Period'[Date]), MAX('Dim Period'[Date]), DAY ) + 1,
		        "QTD", DATEDIFF( STARTOFQUARTER('Dim Period'[Date]), MAX('Dim Period'[Date]), DAY ) + 1,
		        "YTD", DATEDIFF( STARTOFYEAR('Dim Period'[Date]), MAX('Dim Period'[Date]), DAY ) + 1 
	    )

    RETURN

        SWITCH (
            VALUES ('FactType'[FactType]),
                "#", CALCULATE(
                    [Qty]
                    , 'Dim Item','Dim Item'[description]=Myitem
                    ,  DATESINPERIOD ( 'Dim Period'[Date], MAX ( 'Dim Period'[Date] ), -VALUE(MyPeriod), DAY) // this works
                ),

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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