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
bml123
Post Patron
Post Patron

Get previous week value with a condition

HI,

 

I have data like this as below. For each date, I want to get the  amount of the previous week where Active column is 'Yes'.

 

DateAmountActiveWeekRank
28/01/2022100Yes4
01/02/2022-50 5

 

How do I achieve that?

4 REPLIES 4

Hi @bml123 ,

 

1. I have created a  calendar table with two columns, Date and WeekRank for slicer.

Calendar =
ADDCOLUMNS (
    CALENDAR ( MIN ( 'Table'[Date] ), MAX ( 'Table'[Date] ) ),
    "WeekRank", WEEKNUM ( [Date], 2 )
)

vjinweilimsft_0-1647582436651.png

 

2. Create a measure to sum of the week.

Sum of last week =
VAR _week =
    MAX ( 'Calendar'[WeekRank] ) - 1
RETURN
    CALCULATE (
        SUM ( 'Table'[Amount] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Active] = "Yes"
                && WEEKNUM ( 'Table'[Date], 2 ) = _week
        )
)

vjinweilimsft_1-1647582478722.png

 

Best Regards,
Jinwei Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

VahidDM
Super User
Super User

Hi @bml123 

 

Try this:

 

Previous Week =
Var _Current_Week = Max('Table'[WeekRank])
Var _Last_Week = _Current_Week-1
return
Calculate(Max('Table'[Amount]),filter(All('Table'),'Table'[WeekRank]=_Last_Week&&'Table'[Active]="Yes"))
 
 
Output:
VahidDM_1-1647299893702.png

 


 

 

Sample file attached.

 

 

Check this link for more details:

 

https://www.vahiddm.com/post/weekly-time-intelligence-dax

 

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

Hi @VahidDM 

 

WeekRank is there in a separate calendar table and is not part of 'Table'.

How do I change the formula to reflect that?

 

@bml123 

 

Can you share a sample of your tables in a text format? and let us know the relationship between tables?

 

BTW, the easiest way is to use lookupvalue to add a WeekRank column to the main table.

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

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.