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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
userpien
Helper IV
Helper IV

sales of last week

 
Hi, I wrote this function to calculate the sales for the current week. 

Sales Current Year =
CALCULATE([Sales], FILTER(ALL(Time),Time[Week Rank]=max(Time[Week Rank])))

But I want to know the sold for the week before the current week so I wrote
Sales Past Year = CALCULATE([Sales], FILTER(ALL(Time),Time[Week Rank]=max(Time[Week Rank])-1))

but this doesn't work, beacuse it returns me BLANK. Only if i put -100 it return me something.

Plus, i would like to calculate sales of previous week of past year.

What should i write?

thank you
1 ACCEPTED SOLUTION

Hi @userpien ,

 

Thanks for the reply from @rajendraongole1 , please allow me to provide another insight: 

 

You can try this.

Sales Same Week Last Year =
VAR CurWeek = WEEKNUM(TODAY(), 21)
VAR CurYear = YEAR(TODAY())
VAR SameWeekLastYear = CurWeek
VAR LastYear = CurYear - 1
RETURN
SUMX(
    FILTER(
        ALL(Time),
        Time[number week] = SameWeekLastYear && Time[Year] = LastYear
    ),
    [Sold]
)

 

If your Current Period does not refer to this, please clarify in a follow-up reply.

 

Best Regards,

Clara Gong

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
v-kaiyue-msft
Community Support
Community Support

Hi @userpien ,

 

Have you solved your problem? If so, can you share your solution here and mark the correct answer as a standard answer to help other members find it faster? Thank you very much for your kind cooperation!

 

Best Regards,

Clara Gong

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

rajendraongole1
Super User
Super User

Hi @userpien -Create a measures to avoid issues with context and gaps in the Week Rank, i am using EARLIER function to correctly refer to the previous week.

 

Sales Previous Week =
CALCULATE(
[Sales],
FILTER(
ALL(Time),
Time[Week Rank] = MAX(Time[Week Rank]) - 1
)
)

 

create a measure for previous Week Last Year:

Sales Previous Week Last Year =
VAR CurrentWeekRank = MAX(Time[Week Rank])
VAR PreviousWeekRankLastYear =
CALCULATE(
MAX(Time[Week Rank]),
FILTER(
ALL(Time),
Time[Year] = MAX(Time[Year]) - 1 &&
Time[Week Rank] < CurrentWeekRank
)
)
RETURN
CALCULATE(
[Sales],
FILTER(
ALL(Time),
Time[Week Rank] = PreviousWeekRankLastYear &&
Time[Year] = MAX(Time[Year]) - 1
)
)

 

check the above measure still returns any blanks. Hope it works

 

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





 where did you use EARLIER? i don't see it, thank you!

Hi @userpien - The EARLIER function isn't directly used in the above dax funtion but can be helpful in some scenarios for row contect. we can use direct approach with variables to handle context and gaps in the Week Rank effectively.

 

try with above measure and let me know.

 

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





i wrote this function and now it works, for sales past week

Sales Past Week=
VAR CurWeek = WEEKNUM(TODAY(), 21)
VAR CurYear = YEAR(TODAY())
VAR PrevWeek = IF(CurrtWeek = 1, CALCULATE(MAX(Time[number week]), ALL(Time), Time[Year] = CurrentYear - 1), CurrentWeek - 1)
VAR Prev_Year = IF(CurWeek = 1, CurYear - 1, CurYear)
RETURN
SUMX(
    FILTER(
        ALL(Time),
        Time[number week] = PrevWeek && Time[Year] = Prev_Year
    ),
    [Sold]
)

but how i edit this to have same week for past year? Thank you

Hi @userpien ,

 

Thanks for the reply from @rajendraongole1 , please allow me to provide another insight: 

 

You can try this.

Sales Same Week Last Year =
VAR CurWeek = WEEKNUM(TODAY(), 21)
VAR CurYear = YEAR(TODAY())
VAR SameWeekLastYear = CurWeek
VAR LastYear = CurYear - 1
RETURN
SUMX(
    FILTER(
        ALL(Time),
        Time[number week] = SameWeekLastYear && Time[Year] = LastYear
    ),
    [Sold]
)

 

If your Current Period does not refer to this, please clarify in a follow-up reply.

 

Best Regards,

Clara Gong

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

Check out the September 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

Find out what's new and trending in the Fabric Community.