Forum Discussion
Create Matrix or Table with compare data between dates in PowerBi
- 1 year ago
Hi Someone22 ,
To calculate the change in demand between consecutive days for each month in Power BI, you need to reshape your data first using Power Query. Start by unpivoting all the month columns (like 2025.May, 2025.June, etc.) so they become values in a single column instead of being separate headers. This transforms your table into a long format with columns for the Date, Month, and Demand.
Once loaded into Power BI in this format, you can create a DAX measure that calculates the difference in demand between the current row and the previous date for the same month. Here's the DAX measure you can use:
Demand Change = VAR CurrentDate = MAX('YourTable'[Date]) VAR CurrentMonth = SELECTEDVALUE('YourTable'[Month]) VAR CurrentDemand = MAX('YourTable'[Demand]) VAR PrevDate = CALCULATE( MAX('YourTable'[Date]), FILTER( ALL('YourTable'), 'YourTable'[Date] < CurrentDate && 'YourTable'[Month] = CurrentMonth ) ) VAR PrevDemand = CALCULATE( MAX('YourTable'[Demand]), 'YourTable'[Date] = PrevDate && 'YourTable'[Month] = CurrentMonth ) RETURN IF(ISBLANK(PrevDemand), BLANK(), CurrentDemand - PrevDemand)Replace 'YourTable' with the name of your actual table. This measure compares each date’s demand to the demand from the previous date for the same month and returns the difference. Then, place Date in the rows, Month in the columns, and this measure as the value in a Matrix visual. This will give you exactly what your Excel mock-up shows—highlighting daily changes across months. You can then apply conditional formatting to match your Excel color coding.
Best regards,
- 1 year ago
Hello DataNinja777 ,
Based on your DAX, i start to thinking about the operation of it. After some hours i found the right changes and now it is working :D. I changed 2 MAX to SUM and it is perfect(RED highlight).
Demand Change =VAR CurrentDate = MAX('YourTable'[Date])VAR CurrentMonth = SELECTEDVALUE('YourTable'[Month])VAR CurrentDemand = SUM('YourTable'[Demand])VAR PrevDate =CALCULATE(MAX('YourTable'[Date]),FILTER(ALL('YourTable'),'YourTable'[Date] < CurrentDate &&'YourTable'[Month] = CurrentMonth))VAR PrevDemand =CALCULATE(SUM('YourTable'[Demand]),'YourTable'[Date] = PrevDate &&'YourTable'[Month] = CurrentMonth)RETURNIF(ISBLANK(PrevDemand), BLANK(), CurrentDemand - PrevDemand)Thank you again for your support! 🙂
Hi Someone22 ,
To calculate the change in demand between consecutive days for each month in Power BI, you need to reshape your data first using Power Query. Start by unpivoting all the month columns (like 2025.May, 2025.June, etc.) so they become values in a single column instead of being separate headers. This transforms your table into a long format with columns for the Date, Month, and Demand.
Once loaded into Power BI in this format, you can create a DAX measure that calculates the difference in demand between the current row and the previous date for the same month. Here's the DAX measure you can use:
Demand Change =
VAR CurrentDate = MAX('YourTable'[Date])
VAR CurrentMonth = SELECTEDVALUE('YourTable'[Month])
VAR CurrentDemand = MAX('YourTable'[Demand])
VAR PrevDate =
CALCULATE(
MAX('YourTable'[Date]),
FILTER(
ALL('YourTable'),
'YourTable'[Date] < CurrentDate &&
'YourTable'[Month] = CurrentMonth
)
)
VAR PrevDemand =
CALCULATE(
MAX('YourTable'[Demand]),
'YourTable'[Date] = PrevDate &&
'YourTable'[Month] = CurrentMonth
)
RETURN
IF(ISBLANK(PrevDemand), BLANK(), CurrentDemand - PrevDemand)
Replace 'YourTable' with the name of your actual table. This measure compares each date’s demand to the demand from the previous date for the same month and returns the difference. Then, place Date in the rows, Month in the columns, and this measure as the value in a Matrix visual. This will give you exactly what your Excel mock-up shows—highlighting daily changes across months. You can then apply conditional formatting to match your Excel color coding.
Best regards,
- Someone221 year agoHelper I
Hello DataNinja777 ,
I tried it, working, but it show wrong values. You see in below.
In the database there are other columns, not just Date, Months and Demand. It is cause maybe the problem? But the other columns are needed because of the filters.
Thank you in advance again if you can check what would be the problem!
- Someone221 year agoHelper I
Hello DataNinja777 ,
I am new in PBI and i think i missed something really important. Sorry for that. The database in power query, it is not the same what i sent in my first post. The table in my first post is the summary table about raw table from query and i think this is the problem why the dax can not show what i want because in the same Day and Month rows there are more different demands.
So, for an example review my raw database in power query after unpivot the months columns:
Raw database in power query:
Days Material Type City Months Demand 2025.05.30 A X Big 2025. May 500 2025.05.30 B X Small 2025. May 700 2025.05.30 C X Medium 2025. May 300 2025.05.30 A Y Big 2025. May 500 2025.05.30 B Y Small 2025. May 600 2025.05.30 C Y Medium 2025. May 900 2025.05.30 A X Big 2025.June 1100 2025.05.30 B X Small 2025.June 1500 2025.05.30 C X Medium 2025.June 400 2025.05.30 A Y Big 2025.June 700 2025.05.30 B Y Small 2025.June 320 2025.05.30 C Y Medium 2025.June 675 2025.05.30 A X Big 2025.July 456 2025.05.30 B X Small 2025.July 234 2025.05.30 C X Medium 2025.July 953 2025.05.30 A Y Big 2025.July 1134 2025.05.30 B Y Small 2025.July 1054 2025.05.30 C Y Medium 2025.July 454 2025.05.30 A X Big 2025.August 245 2025.05.30 B X Small 2025.August 1034 2025.05.30 C X Medium 2025.August 103 2025.05.30 A Y Big 2025.August 567 2025.05.30 B Y Small 2025.August 345 2025.05.30 C Y Medium 2025.August 876 2025.05.30 A X Big 2025.September 1256 2025.05.30 B X Small 2025.September 2210 2025.05.30 C X Medium 2025.September 222 2025.05.30 A Y Big 2025.September 621 2025.05.30 B Y Small 2025.September 453 2025.05.30 C Y Medium 2025.September 389 2025.05.28 A X Big 2025. May 550 2025.05.28 B X Small 2025. May 750 2025.05.28 C X Medium 2025. May 350 2025.05.28 A Y Big 2025. May 550 2025.05.28 B Y Small 2025. May 650 2025.05.28 C Y Medium 2025. May 950 2025.05.28 A X Big 2025.June 1150 2025.05.28 B X Small 2025.June 1550 2025.05.28 C X Medium 2025.June 450 2025.05.28 A Y Big 2025.June 750 2025.05.28 B Y Small 2025.June 370 2025.05.28 C Y Medium 2025.June 725 2025.05.28 A X Big 2025.July 506 2025.05.28 B X Small 2025.July 284 2025.05.28 C X Medium 2025.July 1003 2025.05.28 A Y Big 2025.July 1184 2025.05.28 B Y Small 2025.July 1104 2025.05.28 C Y Medium 2025.July 504 2025.05.28 A X Big 2025.August 295 2025.05.28 B X Small 2025.August 1084 2025.05.28 C X Medium 2025.August 153 2025.05.28 A Y Big 2025.August 617 2025.05.28 B Y Small 2025.August 395 2025.05.28 C Y Medium 2025.August 926 2025.05.28 A X Big 2025.September 1306 2025.05.28 B X Small 2025.September 2260 2025.05.28 C X Medium 2025.September 272 2025.05.28 A Y Big 2025.September 671 2025.05.28 B Y Small 2025.September 503 2025.05.28 C Y Medium 2025.September 439 2025.05.27 A X Big 2025. May 450 2025.05.27 B X Small 2025. May 650 2025.05.27 C X Medium 2025. May 250 2025.05.27 A Y Big 2025. May 450 2025.05.27 B Y Small 2025. May 550 2025.05.27 C Y Medium 2025. May 850 2025.05.27 A X Big 2025.June 1050 2025.05.27 B X Small 2025.June 1450 2025.05.27 C X Medium 2025.June 350 2025.05.27 A Y Big 2025.June 650 2025.05.27 B Y Small 2025.June 270 2025.05.27 C Y Medium 2025.June 625 2025.05.27 A X Big 2025.July 406 2025.05.27 B X Small 2025.July 184 2025.05.27 C X Medium 2025.July 903 2025.05.27 A Y Big 2025.July 1084 2025.05.27 B Y Small 2025.July 1004 2025.05.27 C Y Medium 2025.July 404 2025.05.27 A X Big 2025.August 195 2025.05.27 B X Small 2025.August 984 2025.05.27 C X Medium 2025.August 53 2025.05.27 A Y Big 2025.August 517 2025.05.27 B Y Small 2025.August 295 2025.05.27 C Y Medium 2025.August 826 2025.05.27 A X Big 2025.September 1206 2025.05.27 B X Small 2025.September 2160 2025.05.27 C X Medium 2025.September 172 2025.05.27 A Y Big 2025.September 571 2025.05.27 B Y Small 2025.September 403 2025.05.27 C Y Medium 2025.September 339 2025.05.15 A X Big 2025. May 600 2025.05.15 B X Small 2025. May 800 2025.05.15 C X Medium 2025. May 400 2025.05.15 A Y Big 2025. May 600 2025.05.15 B Y Small 2025. May 700 2025.05.15 C Y Medium 2025. May 1000 2025.05.15 A X Big 2025.June 1200 2025.05.15 B X Small 2025.June 1600 2025.05.15 C X Medium 2025.June 500 2025.05.15 A Y Big 2025.June 800 2025.05.15 B Y Small 2025.June 420 2025.05.15 C Y Medium 2025.June 775 2025.05.15 A X Big 2025.July 556 2025.05.15 B X Small 2025.July 334 2025.05.15 C X Medium 2025.July 1053 2025.05.15 A Y Big 2025.July 1234 2025.05.15 B Y Small 2025.July 1154 2025.05.15 C Y Medium 2025.July 554 2025.05.15 A X Big 2025.August 345 2025.05.15 B X Small 2025.August 1134 2025.05.15 C X Medium 2025.August 203 2025.05.15 A Y Big 2025.August 667 2025.05.15 B Y Small 2025.August 445 2025.05.15 C Y Medium 2025.August 976 2025.05.15 A X Big 2025.September 1356 2025.05.15 B X Small 2025.September 2310 2025.05.15 C X Medium 2025.September 322 2025.05.15 A Y Big 2025.September 721 2025.05.15 B Y Small 2025.September 553 2025.05.15 C Y Medium 2025.September 489 2025.05.05 A X Big 2025. May 350 2025.05.05 B X Small 2025. May 550 2025.05.05 C X Medium 2025. May 150 2025.05.05 A Y Big 2025. May 350 2025.05.05 B Y Small 2025. May 450 2025.05.05 C Y Medium 2025. May 750 2025.05.05 A X Big 2025.June 950 2025.05.05 B X Small 2025.June 1350 2025.05.05 C X Medium 2025.June 250 2025.05.05 A Y Big 2025.June 550 2025.05.05 B Y Small 2025.June 170 2025.05.05 C Y Medium 2025.June 525 2025.05.05 A X Big 2025.July 306 2025.05.05 B X Small 2025.July 84 2025.05.05 C X Medium 2025.July 803 2025.05.05 A Y Big 2025.July 984 2025.05.05 B Y Small 2025.July 904 2025.05.05 C Y Medium 2025.July 304 2025.05.05 A X Big 2025.August 95 2025.05.05 B X Small 2025.August 884 2025.05.05 C X Medium 2025.August 47 2025.05.05 A Y Big 2025.August 417 2025.05.05 B Y Small 2025.August 195 2025.05.05 C Y Medium 2025.August 726 2025.05.05 A X Big 2025.September 1106 2025.05.05 B X Small 2025.September 2060 2025.05.05 C X Medium 2025.September 72 2025.05.05 A Y Big 2025.September 471 2025.05.05 B Y Small 2025.September 303 2025.05.05 C Y Medium 2025.September 239 2025.05.01 A X Big 2025. May 0 2025.05.01 B X Small 2025. May 0 2025.05.01 C X Medium 2025. May 0 2025.05.01 A Y Big 2025. May 0 2025.05.01 B Y Small 2025. May 0 2025.05.01 C Y Medium 2025. May 0 2025.05.01 A X Big 2025.June 0 2025.05.01 B X Small 2025.June 0 2025.05.01 C X Medium 2025.June 0 2025.05.01 A Y Big 2025.June 0 2025.05.01 B Y Small 2025.June 0 2025.05.01 C Y Medium 2025.June 0 2025.05.01 A X Big 2025.July 0 2025.05.01 B X Small 2025.July 0 2025.05.01 C X Medium 2025.July 0 2025.05.01 A Y Big 2025.July 0 2025.05.01 B Y Small 2025.July 0 2025.05.01 C Y Medium 2025.July 0 2025.05.01 A X Big 2025.August 0 2025.05.01 B X Small 2025.August 0 2025.05.01 C X Medium 2025.August 0 2025.05.01 A Y Big 2025.August 0 2025.05.01 B Y Small 2025.August 0 2025.05.01 C Y Medium 2025.August 0 2025.05.01 A X Big 2025.September 0 2025.05.01 B X Small 2025.September 0 2025.05.01 C X Medium 2025.September 0 2025.05.01 A Y Big 2025.September 0 2025.05.01 B Y Small 2025.September 0 2025.05.01 C Y Medium 2025.September 0 Summary and compare(what i want to see in PBI) matrix:
I am really sorry again :).
Thank you in advance!
- Someone221 year agoHelper I
Hello DataNinja777 ,
Based on your DAX, i start to thinking about the operation of it. After some hours i found the right changes and now it is working :D. I changed 2 MAX to SUM and it is perfect(RED highlight).
Demand Change =VAR CurrentDate = MAX('YourTable'[Date])VAR CurrentMonth = SELECTEDVALUE('YourTable'[Month])VAR CurrentDemand = SUM('YourTable'[Demand])VAR PrevDate =CALCULATE(MAX('YourTable'[Date]),FILTER(ALL('YourTable'),'YourTable'[Date] < CurrentDate &&'YourTable'[Month] = CurrentMonth))VAR PrevDemand =CALCULATE(SUM('YourTable'[Demand]),'YourTable'[Date] = PrevDate &&'YourTable'[Month] = CurrentMonth)RETURNIF(ISBLANK(PrevDemand), BLANK(), CurrentDemand - PrevDemand)Thank you again for your support! 🙂