Don't miss your chance to take the Fabric Data Engineer (DP-600) exam for FREE! Find out how by attending the DP-600 session on April 23rd (pacific time), live or on-demand.
Learn moreNext up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now
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:
| UserID | Sales | Week Ending |
| 1 | 5 | 1/8/2022 |
| 2 | 2 | 1/8/2022 |
| 1 | 1 | 1/1/2022 |
| 3 | 3 | 1/1/2022 |
| 3 | 2 | 1/8/2022 |
| 1 | 2 | 1/1/2022 |
| 2 | 4 | 1/1/2022 |
| 4 | 1 | 1/8/2022 |
| 3 | 2 | 1/1/2022 |
| 2 | 1 | 1/1/2022 |
| 1 | 6 | 1/1/2022 |
| 2 | 3 | 1/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?
Solved! Go to Solution.
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
)
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
)
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.
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.
| User | Count |
|---|---|
| 48 | |
| 45 | |
| 41 | |
| 20 | |
| 17 |
| User | Count |
|---|---|
| 69 | |
| 63 | |
| 32 | |
| 31 | |
| 23 |