Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin 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.
I am looking to produce a week-on-week comparison of stock levels. We ideally take stock on the same day each week, but occasionally this gets pushed back a day or two and we just accept the variation this will cause.
We capture the date of stock date, type of stock and stock item as well as quatity, with the following fields.
Date created || TypeID || StockItem || Stock
14/11/2023 00:00:00 || 3 || Paper || 89321
14/11/2023 00:00:00 || 4 || Glass || 89321
In my test data we have c.2000 entries covering stock takes on two dates 14/11/2023 and 20/11/2023.
I am using the following DAX
test =
VAR thisweek =
CALCULATE(
MAX('Date'[Start of Week]),
'Date'[Start of Week] = SELECTEDVALUE('Date'[Start of Week])
)
VAR thisweekstock =
CALCULATE(
SUM('Stock'[instock]),
ALL('Stock'),
'Stock'[Date created] >= thisweek,
'Stock'[Date created] < thisweek + 7
)
VAR lastweek =
CALCULATE(MAX('Date'[Start of Week]), 'Date'[Date] = thisweek - 7)
VAR lastweekstock =
CALCULATE(
SUM('Stock'[instock]),
'Stock'[Date created] >= lastweek,
'Stock'[Date created] < thisweek
)
VAR Change = thisweekstock - lastweekstock
VAR Result =
SWITCH(
TRUE(),
Change > 0, "↑", // Up arrow if positive change
Change < 0, "↓", // Down arrow if negative change
"→" // Sideways arrow if no change
)
RETURN Result
when troubleshooting
thisweek produces the correct date "20/11/2023 00:00:00"
thisweekstock produces the correct total and breakdowns per Type ID when placed on a matrix
lastweek produces the correct date "13/11/2023 00:00:00"
However, lastweekstock produces the correct overall total stock for that period, but then replicates that total figure for each row in the matrix rather than showing the total for that TypeID.
I'm a little stumped to say the least.
*SIDENOTE: I will be using better formating on the variable names once this works.
EDIT:
I've also tried the code below with simlar results..
//Orignal code to this point
VAR lastweekstock =
CALCULATE(
SUM('Stock'[instock]),
FILTER(ALL('Stock'),
'Stock'[Date created] >= lastweek &&
'Stock'[Date created] < lastweek + 7)
)
//Original code from this point
Solved! Go to Solution.
Eureaka!
solved it!
//Orignal code to this point
VAR lastweekstock =
CALCULATE(
SUM('Stock'[instock]),
'Stock'[Date created] >= lastweek.
'Stock'[Date created] < lastweek + 7,
ALLEXCEPT('Stock', 'Stock'[StockItem])
)
//Original code from this point
Eureaka!
solved it!
//Orignal code to this point
VAR lastweekstock =
CALCULATE(
SUM('Stock'[instock]),
'Stock'[Date created] >= lastweek.
'Stock'[Date created] < lastweek + 7,
ALLEXCEPT('Stock', 'Stock'[StockItem])
)
//Original code from this point
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
10 | |
9 | |
8 | |
6 | |
5 |
User | Count |
---|---|
20 | |
14 | |
10 | |
9 | |
6 |