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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Sander1401
Helper I
Helper I

Last week calculation

Hi all,
 
I have a problem with calculating the result of last week.
A collegue of mine wrote the dax stated below, and it worked. Unfortunately not when we started the new year.
Then it looks for week 0 (week 1 minus 1), and it obviously can't find it.
My idea would be that I need to adjust this VAR:
VAR MaxWeekNumber = CALCULATE(MAX(Datetabel[Weeknr])-1, ALL(Datetabel))

In this VAR I need to calculate the max weeknr of last year, so for example:
MAX(Datetabel[Weeknr]) where YEAR(today())-1

But how can I write this in DAX??
I hope everything is clear and someone can help me out on this.
thanks in advance!
 
Base internet - last week =
VAR CurrentWeek = SELECTEDVALUE(Datetabel[Weeknr])
VAR CurrentYear = SELECTEDVALUE(Datetabel[Year])
VAR MaxWeekNumber = CALCULATE(MAX(Datetabel[Weeknr])-1, ALL(Datetabel))
RETURN

 

SUMX(
FILTER( ALL(Datetabel),
IF( CurrentWeek = 1,
Datetabel[Weeknr] = MaxWeekNumber && Datetabel[Year] = CurrentYear - 1,
Datetabel[Weeknr] = CurrentWeek - 1 && Datetabel[Year] = CurrentYear )),
[Base internet - week]
)
1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@Sander1401 I would recommend implementing something like Sequential and then this is trivial. Can also do something similar in PQ. https://community.powerbi.com/t5/Quick-Measures-Gallery/Sequential/m-p/380231#M116

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

4 REPLIES 4
Greg_Deckler
Super User
Super User

@Sander1401 Make sure you are using the right return type for WEEKNUM:

WEEKNUM function (DAX) - DAX | Microsoft Docs



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler I forgot to change one VAR, my mistake, it works now! Thanks again!

Greg_Deckler
Super User
Super User

@Sander1401 I would recommend implementing something like Sequential and then this is trivial. Can also do something similar in PQ. https://community.powerbi.com/t5/Quick-Measures-Gallery/Sequential/m-p/380231#M116

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_DecklerGood suggestion, thanks for that. I only are having troubles with the iso weeks/year. So the first 2 days of 2022 (1 january and 2 january 2022) are in Iso week 52-2021. But unfortunately the column calculation doesn't recognize this...
How can I solve this? Do you have an idea?

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.