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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
leekarensl
Helper II
Helper II

Calculating Spend Last week

Hi there, please can anyone help. I am trying to create a measure calculating Spend LW. The issue is my stakeholders want it in such a way that they can select multiple weekstart dates and the Spend LW should automatically calculate the sum of spend for the x weeks before.  In the attached file example, if 27th oct and 3rd Nov is being selected by the end user, the Spend LW will calculate the sum of spend for 13th Oct and 20th Oct. And if on the filter, 3rd Nov, 27th Oct, 20th Oct and 13th oct is selected, then Spend LW will calculate the sum of spend for 6th Oct, 29th Sep, 22nd Sep and 15th Sep. I can't seem to get it to work and would appreciate any help. The point is the end user wants to have the flexibility to select how ever many or few weekstartdates from the weekstartdate filter and the Spend LW should be able to calculate accordingly.

 

The file can be found here  Thanks very much in advance.

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

First create a date table and mark it as a date table. Create a relationship from 'Date'[Date] to your 'Test'[Calendar Date] and then you can create a measure like

Spend Last Week = 
VAR EndDate = MIN('Date'[Date])
VAR NumSelected = COUNTROWS(VALUES('Date'[W / C]))
VAR StartDate = EndDate - (7 * NumSelected)
VAR DatesToUse = DATESBETWEEN(
    'Date'[Date],
    StartDate,
    EndDate - 1
)
VAR Result = CALCULATE(
    SUM('TEST'[SPEND]),
    DatesToUse
)
RETURN
    Result

See the attached PBIX.

View solution in original post

6 REPLIES 6
Bibiano_Geraldo
Super User
Super User

Hi, 
Please try the following dax code:

Spend LW = 
VAR SelectedWeeks = VALUES('TEST'[WeekStartDate])
VAR NumberOfWeeks = COUNTROWS(SelectedWeeks) * 7  // Dynamic offset: number of weeks * 7 days
VAR PreviousWeeks = 
    CALCULATETABLE(
        VALUES('TEST'[WeekStartDate]),
        FILTER(
            ALL('TEST'),
            'TEST'[WeekStartDate] IN 
                SELECTCOLUMNS(
                    SelectedWeeks,
                    "PreviousWeek",
                    IF(
                        HASONEVALUE(TEST[WEEKSTARTDATE]),
                            'TEST'[WeekStartDate] - 7,
                            ('TEST'[WeekStartDate]-7) - NumberOfWeeks
                    )
                )
        )
    )
RETURN
CALCULATE(
    SUM('TEST'[SPEND]),
    'TEST'[WeekStartDate] IN PreviousWeeks
)
johnt75
Super User
Super User

First create a date table and mark it as a date table. Create a relationship from 'Date'[Date] to your 'Test'[Calendar Date] and then you can create a measure like

Spend Last Week = 
VAR EndDate = MIN('Date'[Date])
VAR NumSelected = COUNTROWS(VALUES('Date'[W / C]))
VAR StartDate = EndDate - (7 * NumSelected)
VAR DatesToUse = DATESBETWEEN(
    'Date'[Date],
    StartDate,
    EndDate - 1
)
VAR Result = CALCULATE(
    SUM('TEST'[SPEND]),
    DatesToUse
)
RETURN
    Result

See the attached PBIX.

Thank you so much! This works perfectly!

Kedar_Pande
Super User
Super User

@leekarensl 

Create a calculated column

PreviousWeekStartDate = 
CALCULATE(
MAX(YourTable[WeekStartDate]),
FILTER(
YourTable,
YourTable[WeekStartDate] < EARLIER(YourTable[WeekStartDate])
)
)

Create a measure

SpendLW = 
VAR SelectedWeekStartDates = VALUES(YourTable[WeekStartDate])
VAR PreviousWeekStartDates =
CALCULATETABLE(
VALUES(YourTable[PreviousWeekStartDate]),
FILTER(
YourTable,
YourTable[WeekStartDate] IN SelectedWeekStartDates
)
)
RETURN
CALCULATE(
SUM(YourTable[Spend]),
YourTable[WeekStartDate] IN PreviousWeekStartDates
)

Apply the measure to your report to reflect the spend for the previous weeks based on the selected week start dates.

 

💌 If this helped, a Kudos 👍 or Solution mark would be great! 🎉
Cheers,
Kedar
Connect on LinkedIn

Thanks for your prompt response but it is not calculating the figures correctly. If I select 13 Oct and 6th Oct, the values are as follows:
Screenshot 2024-11-14 144435.png
If I selected 20th and 27th Oct, then the Spend LW column should give me exactly the figures above but it is giving me :

Screenshot 2024-11-14 144629.png

I think you're still using the [Spend LW] measure, not the [Spend Last Week] measure which uses the code I wrote. Check Duplicate of Page 1 in the PBIX

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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