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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Sam7
Advocate II
Advocate II

Create a Previous Day, Week, Day, Year calculations

Hello, 

 

I want to create a calculation, based on my filter created from this: https://www.youtube.com/watch?v=hilfglpKNRQ

I have a daily, weekly etc filter.  I want to create a measure for the previous period of each filter. 

 

I created this but this did not work:  Please help

 

Previous Range =
VAR _typePERIOD = (SELECTEDVALUE ('Period Selection'[Type] ))

RETURN
IF( _typePERIOD = "Day",
CALCULATE(SUM('table'[column]),PREVIOUSDAY('date'[Date])),

IF( _typePERIOD = "Week",
CALCULATE(SUM('table'[column]]),PREVIOUSDAY('date'[Date])-7),

IF( _typePERIOD = "By Month",
CALCULATE(SUM('table'[column]),PREVIOUSMONTH('date'[Date])),

IF( _typePERIOD = "By Year",
CALCULATE(SUM('table'[column]),PREVIOUSYEAR('date'[Date]))

))))

1 ACCEPTED SOLUTION

Hi @Sam7 ,

 

You cannot use PREVIOUSDAY('date'[Date])-7 to calculate the previous week's value. Please try the following measure.

 

Previous Range = 
VAR _typePERIOD =
    SELECTEDVALUE ( 'Period Selection'[Type] )
VAR CurWeek =
    WEEKDAY ( MAX ( 'date'[Date] ), 2 )
RETURN
    SWITCH (
        _typePERIOD,
        "Day", CALCULATE ( SUM ( 'table'[column] ), PREVIOUSDAY ( 'date'[Date] ) ),
        "Week",
            CALCULATE (
                SUM ( 'table'[column] ),
                DATESBETWEEN (
                    'date'[Date],
                    MAX ( 'date'[Date] ) - 6 - CurWeek,
                    MAX ( 'date'[Date] ) - CurWeek
                )
            ),
        "By Month", CALCULATE ( SUM ( 'table'[column] ), PREVIOUSMONTH ( 'date'[Date] ) ),
        "By Year", CALCULATE ( SUM ( 'table'[column] ), PREVIOUSYEAR ( 'date'[Date] ) )
    )

vkkfmsft_0-1655793585003.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
rohit_singh
Solution Sage
Solution Sage

Hi @Sam7 ,

The variable storing the selected values and the variables in the conditional statements are not the same

rohit_singh_0-1655310069712.png

Try replacing _typeDWMY with _typePERIOD in the conditional statements.

Kind regards,

Rohit


Please mark this answer as the solution if it resolves your issue.
Appreciate your kudos! 🙂

Oops, edited my query, now showing correctly. Can you help with it now?

Hi @Sam7 ,

Thanks. Can you please share your current output?
Also, please make sure you are using 'date[Date]' as the date field in your visual, else you will get blank values.

Kind regards,

Rohit

 

Please mark this answer as the solution if it resolves your issue.
Appreciate your kudos! 🙂

Thanks for reply. My date column is the one within my results. 

 

My error is: A function 'CALCULATE' has been used in a True/False expression that is used as a table filter expression. This is not allowed.

Hi @Sam7 ,

 

You cannot use PREVIOUSDAY('date'[Date])-7 to calculate the previous week's value. Please try the following measure.

 

Previous Range = 
VAR _typePERIOD =
    SELECTEDVALUE ( 'Period Selection'[Type] )
VAR CurWeek =
    WEEKDAY ( MAX ( 'date'[Date] ), 2 )
RETURN
    SWITCH (
        _typePERIOD,
        "Day", CALCULATE ( SUM ( 'table'[column] ), PREVIOUSDAY ( 'date'[Date] ) ),
        "Week",
            CALCULATE (
                SUM ( 'table'[column] ),
                DATESBETWEEN (
                    'date'[Date],
                    MAX ( 'date'[Date] ) - 6 - CurWeek,
                    MAX ( 'date'[Date] ) - CurWeek
                )
            ),
        "By Month", CALCULATE ( SUM ( 'table'[column] ), PREVIOUSMONTH ( 'date'[Date] ) ),
        "By Year", CALCULATE ( SUM ( 'table'[column] ), PREVIOUSYEAR ( 'date'[Date] ) )
    )

vkkfmsft_0-1655793585003.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.