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
UditJ
Helper III
Helper III

Previous Value for a weeks start date

Need to show in a column the previous value for the dates shown in the iamge below. For example, the 5/24/2021 will show no data and 5/31/2021 will show the previous value which is 66,283,907. Dates are calculated as 

Week Start Date = 'Reporting Dashboard_Social'[Date] - WEEKDAY('Reporting Dashboard_Social'[Date],2) + 1

Need a similar function like sameperiodlastyear but instead of year need it for start day of the week. 

Any suggestions? 


wsd.PNG

1 ACCEPTED SOLUTION

Hi @UditJ ,

 

I suggest you to create a DimDate table to help your calculation.

 

DimDate =
VAR _STEP1 =
    ADDCOLUMNS (
        CALENDARAUTO (),
        "Year", YEAR ( [Date] ),
        "Month", MONTH ( [Date] ),
        "Week Start Date",
            [Date] - WEEKDAY ( [Date], 2 ) + 1
    )
VAR _STEP2 =
    ADDCOLUMNS (
        _STEP1,
        "Year of Week", YEAR ( [Week Start Date] ),
        "WEEKNUM", WEEKNUM ( [Week Start Date] ) - 1
    )
RETURN
    _STEP2

 

Measure:

 

Previous Week Impression =
VAR _PREVIOUWEEK =
    MAXX (
        FILTER (
            ALL ( DimDate ),
            DimDate[Week Start Date] < MAX ( DimDate[Week Start Date] )
        ),
        [Week Start Date]
    )
RETURN
    CALCULATE (
        SUM ( 'Reporting Dashboard_Social'[Impressions] ),
        FILTER ( ALL ( DimDate ), DimDate[Week Start Date] = _PREVIOUWEEK )
    )
Previous Year Week Impression = 
CALCULATE (
    SUM ( 'Reporting Dashboard_Social'[Impressions] ),
    FILTER (
        ALL ( DimDate ),
        DimDate[Year of Week]
            = MAX ( DimDate[Year of Week] ) - 1
            && DimDate[WEEKNUM] = MAX ( DimDate[WEEKNUM] )
    )
)

 

Result is as below.

vrzhoumsft_0-1690786457758.png

 

Best Regards,
Rico Zhou

 

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

3 REPLIES 3
Greg_Deckler
Super User
Super User

@UditJ It's essentially this pattern.

See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395....
The basic pattern is:
Column = 
  VAR __Current = [Value]
  VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

  VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
  __Current - __Previous


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

What if i want to show last years weeks values ? so a new column showing last year weeks values. Last year week can be as close as possible. Any suggestions ?

Hi @UditJ ,

 

I suggest you to create a DimDate table to help your calculation.

 

DimDate =
VAR _STEP1 =
    ADDCOLUMNS (
        CALENDARAUTO (),
        "Year", YEAR ( [Date] ),
        "Month", MONTH ( [Date] ),
        "Week Start Date",
            [Date] - WEEKDAY ( [Date], 2 ) + 1
    )
VAR _STEP2 =
    ADDCOLUMNS (
        _STEP1,
        "Year of Week", YEAR ( [Week Start Date] ),
        "WEEKNUM", WEEKNUM ( [Week Start Date] ) - 1
    )
RETURN
    _STEP2

 

Measure:

 

Previous Week Impression =
VAR _PREVIOUWEEK =
    MAXX (
        FILTER (
            ALL ( DimDate ),
            DimDate[Week Start Date] < MAX ( DimDate[Week Start Date] )
        ),
        [Week Start Date]
    )
RETURN
    CALCULATE (
        SUM ( 'Reporting Dashboard_Social'[Impressions] ),
        FILTER ( ALL ( DimDate ), DimDate[Week Start Date] = _PREVIOUWEEK )
    )
Previous Year Week Impression = 
CALCULATE (
    SUM ( 'Reporting Dashboard_Social'[Impressions] ),
    FILTER (
        ALL ( DimDate ),
        DimDate[Year of Week]
            = MAX ( DimDate[Year of Week] ) - 1
            && DimDate[WEEKNUM] = MAX ( DimDate[WEEKNUM] )
    )
)

 

Result is as below.

vrzhoumsft_0-1690786457758.png

 

Best Regards,
Rico Zhou

 

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

 

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.