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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Anonymous
Not applicable

default to latest week

Hi there

 

In the screenshot below, i need the weekly difference figure in green to default to the difference between the 2 most recent weeks i.e. september week 2 and september week 3. At the moment, it's defaulting to the final 2 weeks in august.

 

The weekly difference is a MEASURE.

 

Can I please get some help with this?

 

JAG PBI 19-9.jpg

1 ACCEPTED SOLUTION
v-lili6-msft
Community Support
Community Support

hi, @Anonymous 

From your screenshot, I think the week no. in the visual is a custom week logic, that every almost month has 5 weeks.

So you may try this formula:

 

Measure 2 = 
VAR maxmonth =
    CALCULATE ( MAX ( 'Table'[YearMonth] ) )
VAR maxweek =
    CALCULATE (
        MAX ( 'Table'[Week] ),
        FILTER ( 'Table', 'Table'[YearMonth] = maxmonth )
    )
RETURN
    VAR lastweek =
        IF (
            maxweek = 1
                && MAX ( 'Table'[Month] ) = 1,
            CALCULATE (
                MAX ( 'Table'[Week] ),
                FILTER ( ALL ( 'Table' ), 'Table'[YearMonth] = maxmonth - 89 )
            ),
            IF (
                maxweek = 1,
                CALCULATE (
                    MAX ( 'Table'[Week] ),
                    FILTER ( ALL ( 'Table' ), 'Table'[YearMonth] = maxmonth - 1 )
                ),
                maxweek - 1
            )
        )
    RETURN
        CALCULATE (
            SUM ( 'Table'[Field1] ),
            FILTER ( 'Table', 'Table'[YearMonth] = maxmonth && 'Table'[Week] = maxweek )
        )
            - IF (
                MAX ( 'Table'[Month] ) = 1
                    && maxweek = 1,
                CALCULATE (
                    SUM ( 'Table'[Field1] ),
                    FILTER (
                        ALL ( 'Table' ),
                        'Table'[YearMonth] = maxmonth - 89
                            && 'Table'[Week] = lastweek
                    )
                ),
                IF (
                    maxweek = 1,
                    CALCULATE (
                        SUM ( 'Table'[Field1] ),
                        FILTER (
                            ALL ( 'Table' ),
                            'Table'[YearMonth] = maxmonth - 1
                                && 'Table'[Week] = lastweek
                        )
                    ),
                    CALCULATE (
                        SUM ( 'Table'[Field1] ),
                        FILTER (
                            ALL ( 'Table' ),
                            'Table'[YearMonth] = maxmonth
                                && 'Table'[Week] = lastweek
                        )
                    )
                )
            )

 

Result:

1.JPG

and here is sample pbix, please try it,.

 

 

Best Regards,

Lin

Community Support Team _ Lin
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

2 REPLIES 2
v-lili6-msft
Community Support
Community Support

hi, @Anonymous 

From your screenshot, I think the week no. in the visual is a custom week logic, that every almost month has 5 weeks.

So you may try this formula:

 

Measure 2 = 
VAR maxmonth =
    CALCULATE ( MAX ( 'Table'[YearMonth] ) )
VAR maxweek =
    CALCULATE (
        MAX ( 'Table'[Week] ),
        FILTER ( 'Table', 'Table'[YearMonth] = maxmonth )
    )
RETURN
    VAR lastweek =
        IF (
            maxweek = 1
                && MAX ( 'Table'[Month] ) = 1,
            CALCULATE (
                MAX ( 'Table'[Week] ),
                FILTER ( ALL ( 'Table' ), 'Table'[YearMonth] = maxmonth - 89 )
            ),
            IF (
                maxweek = 1,
                CALCULATE (
                    MAX ( 'Table'[Week] ),
                    FILTER ( ALL ( 'Table' ), 'Table'[YearMonth] = maxmonth - 1 )
                ),
                maxweek - 1
            )
        )
    RETURN
        CALCULATE (
            SUM ( 'Table'[Field1] ),
            FILTER ( 'Table', 'Table'[YearMonth] = maxmonth && 'Table'[Week] = maxweek )
        )
            - IF (
                MAX ( 'Table'[Month] ) = 1
                    && maxweek = 1,
                CALCULATE (
                    SUM ( 'Table'[Field1] ),
                    FILTER (
                        ALL ( 'Table' ),
                        'Table'[YearMonth] = maxmonth - 89
                            && 'Table'[Week] = lastweek
                    )
                ),
                IF (
                    maxweek = 1,
                    CALCULATE (
                        SUM ( 'Table'[Field1] ),
                        FILTER (
                            ALL ( 'Table' ),
                            'Table'[YearMonth] = maxmonth - 1
                                && 'Table'[Week] = lastweek
                        )
                    ),
                    CALCULATE (
                        SUM ( 'Table'[Field1] ),
                        FILTER (
                            ALL ( 'Table' ),
                            'Table'[YearMonth] = maxmonth
                                && 'Table'[Week] = lastweek
                        )
                    )
                )
            )

 

Result:

1.JPG

and here is sample pbix, please try it,.

 

 

Best Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

You can include two more field as 'Weeknum' & 'Year' and use the below DAX to get the difference between current and last week's value

 

VAR MaxWeekNum = MAX('Table'[WeekNum])

 

VAR Max2ndWeekNum = IF(MAX('Table'[WeekNum])=1,CALCULATE(MAX('Table'[WeekNum]),FILTER(ALL('Table'),'Table'[Year]=MAX('Table'[Year])-1)),MAX('Table'[WeekNum])-1)

 

VAR Max2ndWeekNumYear = IF(MAX('Table'[WeekNum])=1,MAX('Table'[Year])-1,MAX('Table'[Year]))

 

RETURN CALCULATE(SUM('Table'[Field1]), FILTER('Table','Table'[WeekNum]= MAX('Table'[WeekNum])) -
CALCULATE(SUM('Table'[Field1]), FILTER('Table','Table'[WeekNum]=Max2ndWeekNum && 'Table'[Year] =Max2ndWeekNumYear))

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.