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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
jackj
Helper I
Helper I

Calculated Column for Previous Week Sales

Hi,

 

I am struggling to write dax for a calculated column, not a measure, that will provide me with the previous week's subtotal sales for a user id.  My data table is structured like this:

 

UserIDSalesWeek Ending
151/8/2022
22

1/8/2022

111/1/2022
331/1/2022
321/8/2022
121/1/2022
241/1/2022
411/8/2022
321/1/2022
211/1/2022
161/1/2022
231/8/2022

 

So, for example, in the first Row, I want to be able to show in a calculated column that UserID 1 had a total of 9 sales in the previous week (1/1/2022).  How can I accomplish this, dynamically based on the week ending listed in the table?

1 ACCEPTED SOLUTION
AUaero
Responsive Resident
Responsive Resident

In that case you could rewrite the column to act as a binary flag.

Prev Week Sales Flag = 
VAR CurID = 'table'[UserID]
VAR CurWeek = 'table'[Week Ending]
VAR PrevWeek = CurWeek - 7

VAR LastWeekSales = 
CALCULATE(
    SUM('table'[Sales]),
    FILTER(
        ALL('table'),
        'table'[UserID] = CurID &&
        'table'[Week Ending] = PrevWeek
    )
)

RETURN
SWITCH(
    TRUE(),
    LastWeekSales > 5, 1
    0
)

View solution in original post

3 REPLIES 3
AUaero
Responsive Resident
Responsive Resident

In that case you could rewrite the column to act as a binary flag.

Prev Week Sales Flag = 
VAR CurID = 'table'[UserID]
VAR CurWeek = 'table'[Week Ending]
VAR PrevWeek = CurWeek - 7

VAR LastWeekSales = 
CALCULATE(
    SUM('table'[Sales]),
    FILTER(
        ALL('table'),
        'table'[UserID] = CurID &&
        'table'[Week Ending] = PrevWeek
    )
)

RETURN
SWITCH(
    TRUE(),
    LastWeekSales > 5, 1
    0
)
AUaero
Responsive Resident
Responsive Resident

This will always provide the sum of sales for the prior week for each user ID. 

Prev Week Sales = 
VAR CurID = 'table'[UserID]
VAR CurWeek = 'table'[Week Ending]
VAR PrevWeek = CurWeek - 7

RETURN
CALCULATE(
    SUM('table'[Sales]),
    FILTER(
        ALL('table'),
        'table'[UserID] = CurID &&
        'table'[Week Ending] = PrevWeek
    )
)

 

Just curious why you can't use a measure here?

 

I'm looking to use it as a filter in another measure, at the UserID level - basically if last week's sales for that userID are >5, then don't count them.

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors