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
clarkey1988
Helper II
Helper II

Previous Week Sum Measure

Hi, I'm a fairly new Power BI user and am having a problem calculating a measure for a Matrix visual.

 

My aim is to calculate the previous weeks miles for each business division in a Matrix. My current function calculates the total previous week miles. However, I need it to calculate the previous weeks miles for each business division.

 

Previous Week Miles = CALCULATE(SUM('Trip Detail'[Miles]),(FILTER(ALLSELECTED('Trip Detail'),'Trip Detail'[Week Number]=MAX('Trip Detail'[Week Number])-1)))
 
Capture.PNG
 
My overall goal is to calculate weekly % change by business division. Please could someone push me in the right direction?
 
1 ACCEPTED SOLUTION

HI @clarkey1988,

I think you also need to consider about the year conversion issue, so I added if statement your formula to dispatch to two conditions based on current week number that extract from your table date fields. (I add one additional condition to your formula to filter records based on the year and week number, it will be filtered more accurately if your table contains many year records)
You can try it if below formula meets to your requirement:

Previous Week =
VAR currDate =
    MAX ( 'Trip Detail'[date] )
RETURN
    CALCULATE (
        SUM ( 'Trip Detail'[Miles] ),
        FILTER (
            ALLSELECTED ( 'Trip Detail' ),
            IF (
                weekNumber > 1,
                YEAR ( 'Trip Detail'[Date] ) = YEAR ( currDate )
                    && WEEKNUM ( 'Trip Detail'[Date] )
                        = WEEKNUM ( currDate ) - 1,
                YEAR ( 'Trip Detail'[Date] )
                    = YEAR ( currDate ) - 1
                    && WEEKNUM ( 'Trip Detail'[Date] )
                        = WEEKNUM ( DATE ( YEAR ( currDate ) - 1, 12, 31 ) )
            )
        ),
        VALUES ( 'Trip Detail'[Business Division] )
    )

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

9 REPLIES 9
clarkey1988
Helper II
Helper II

Thank you all for your responses!

amitchandak
Super User
Super User

@clarkey1988 , Make sure week is in a separate table. In date table or week Table.

 

Refer my blog

https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-La...

PaulDBrown
Community Champion
Community Champion

@clarkey1988 

You don't need ALLSELECTED. What is your current week measure? 

You probably only need something along the lines of:

Previous week = CALCULATE(SUM('Trip Detail'[Miles]),
FILTER(ALL('Trip Detail'),
'Trip Detail'[Week Number]=MAX('Trip Detail'[Week Number])-1))

 

If there are other row contexts (nested for example) you might need ALLEXCEPT

Something along the lines of:

Previous week = CALCULATE(SUM('Trip Detail'[Miles]),
(FILTER(ALLEXCEPT('Table, Table[Business Division]'),'Trip Detail'[Week Number]=MAX('Trip Detail'[Week Number])-1)))

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Hi @PaulDBrown 

 

Thanks for responding. My current measure is simply the sum of the current weeks miles/sum of week 10 miles (pre COVID)

 

Δ Miles = (SUM('Trip Detail'[Miles]))/(CALCULATE(SUM('Trip Detail'[Miles]),'Trip Detail'[Week Number]=10))-1

 

However, I need it to be week over week and not specific to week 10. I have two filters within the Matrix, but it seems the measure you listed without ALLSELECTED still sums the TOTAL miles, and does not break it out by business division. The business division is a column I added and made into an if statement.

 

Capture.PNG

 

 

@clarkey1988 
Hi again,

You probably need to include the fweek number in the ALL clause:

Previous week = CALCULATE(SUM('Trip Detail'[Miles]),
FILTER(ALL('Trip Detail'[Week Number]),
'Trip Detail'[Week Number]=MAX('Trip Detail'[Week Number])-1))

Otherwise try with the second option I posted using ALLEXCEPT, see if that works. 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






@PaulDBrown  so I think I'm close. The first optuion did not work

 

The ALLEXCEPT version below calculatues the previous weeks miles by business division accurately. However, it does not take into considertation the two filters I have in the matrix.

 

Previous Week = CALCULATE(SUM('Trip Detail'[Miles]),
(FILTER(ALLEXCEPT('Trip Detail', 'Trip Detail'[Business Division]),'Trip Detail'[Week Number]=MAX('Trip Detail'[Week Number])-1)))
 
Capture.PNG

HI @clarkey1988,

I think you also need to consider about the year conversion issue, so I added if statement your formula to dispatch to two conditions based on current week number that extract from your table date fields. (I add one additional condition to your formula to filter records based on the year and week number, it will be filtered more accurately if your table contains many year records)
You can try it if below formula meets to your requirement:

Previous Week =
VAR currDate =
    MAX ( 'Trip Detail'[date] )
RETURN
    CALCULATE (
        SUM ( 'Trip Detail'[Miles] ),
        FILTER (
            ALLSELECTED ( 'Trip Detail' ),
            IF (
                weekNumber > 1,
                YEAR ( 'Trip Detail'[Date] ) = YEAR ( currDate )
                    && WEEKNUM ( 'Trip Detail'[Date] )
                        = WEEKNUM ( currDate ) - 1,
                YEAR ( 'Trip Detail'[Date] )
                    = YEAR ( currDate ) - 1
                    && WEEKNUM ( 'Trip Detail'[Date] )
                        = WEEKNUM ( DATE ( YEAR ( currDate ) - 1, 12, 31 ) )
            )
        ),
        VALUES ( 'Trip Detail'[Business Division] )
    )

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Hi @v-shex-msft  , your answer helped with my Last Calendar week Orders calculation.I am struggling to calculate orders for Last 4 Calendar weeks. Any help with that will be greatly appreciated.

@clarkey1988 

Ok, so we are getting close...

Something which I din't ask, and is actually very important (for many reasons, including these kind of calculations): do you have a calendar or Date table?

If not, please create one and link it to your tables in one-to-many relationship.

(As regards the measure not taking into account the filters, it's hard to see what's going on without knowing what/if tables & relationships you are enacting in the filters...).

 

Ps, it would be very helpful if you could provide a sample PBIX file/sample data (with fake data of need be) to work on! 

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






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.