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

Prepping for a Fabric certification exam? Join us for a live prep session with exam experts to learn how to pass the exam. Register now.

Reply
RGL
Helper II
Helper II

Why don't I get the previous week sales with this measure?

 

Last Week Sales = 

CALCULATE( Sales ,FILTER(VALUES(Calendar[Date]), MAX(Calendar[Date])-7))
 
I get the same result (sales of the current day)
Example: 
 
Date               Sales    Last Week Sales
08/20/2022    175             175
08/21/2022     80               80
 
 
I need to get the sales from 08/13/2022 and 08/14/2022 in the "Last Week Sales" column
 
If I use ALL instead of VALUES I get the total sales of the current week, not from last week
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @RGL ,

According to your description, When you use the DATEADD() function, it returns an error when using the week as a dimension, and returns null .Right?

DATEADD() is a time intelligence dax function. If you want to expand the Time-Dimension, it is recommended to use it in a standard date table to avoid errors.

You can also refer to the document: DATEADD()

Here are the steps you can follow:

(1)This is my test data:

vyangliumsft_0-1662024030511.png

(2)We can create a data table according our table Sheet2:

Calendar Table = ADDCOLUMNS(
CALENDAR(FIRSTDATE('Sheet2'[LastRefresh]),LASTDATE('Sheet2'[LastRefresh])),
"year", YEAR ( [Date] ),
"quarter", ROUNDUP(MONTH([Date])/3,0),
"month", MONTH([Date]),
"weeknum", weeknum([Date]),
"year & quarter", year([date]) & "Q" & ROUNDUP(MONTH([Date])/3,0),
"year & month", year([Date]) * 100 + MONTH([Date]),
"year & weeknum", year([Date]) * 100 + weeknum([Date]),
"weekday", WEEKDAY([Date])
)

We also need to create a one-to-many relationship between two tables, like this:

vyangliumsft_1-1662024030515.png

(3)We can create a measure : Last Week Sales:

Last Week Sales = CALCULATE(SUM('Sheet2'[Sales]),DATEADD('Calendar Table'[Date],-7,DAY))

Then the measure wont return null now: (We need to use the field in our date table Calendar Table)

vyangliumsft_2-1662024030522.png

If this method does not meet your needs, you can provide us your sample data without sensitive data, or detailed input and output sample data, so that we can better help for you.

Best Regards,

Liu Yang

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

3 REPLIES 3
Anonymous
Not applicable

Hi @RGL ,

According to your description, When you use the DATEADD() function, it returns an error when using the week as a dimension, and returns null .Right?

DATEADD() is a time intelligence dax function. If you want to expand the Time-Dimension, it is recommended to use it in a standard date table to avoid errors.

You can also refer to the document: DATEADD()

Here are the steps you can follow:

(1)This is my test data:

vyangliumsft_0-1662024030511.png

(2)We can create a data table according our table Sheet2:

Calendar Table = ADDCOLUMNS(
CALENDAR(FIRSTDATE('Sheet2'[LastRefresh]),LASTDATE('Sheet2'[LastRefresh])),
"year", YEAR ( [Date] ),
"quarter", ROUNDUP(MONTH([Date])/3,0),
"month", MONTH([Date]),
"weeknum", weeknum([Date]),
"year & quarter", year([date]) & "Q" & ROUNDUP(MONTH([Date])/3,0),
"year & month", year([Date]) * 100 + MONTH([Date]),
"year & weeknum", year([Date]) * 100 + weeknum([Date]),
"weekday", WEEKDAY([Date])
)

We also need to create a one-to-many relationship between two tables, like this:

vyangliumsft_1-1662024030515.png

(3)We can create a measure : Last Week Sales:

Last Week Sales = CALCULATE(SUM('Sheet2'[Sales]),DATEADD('Calendar Table'[Date],-7,DAY))

Then the measure wont return null now: (We need to use the field in our date table Calendar Table)

vyangliumsft_2-1662024030522.png

If this method does not meet your needs, you can provide us your sample data without sensitive data, or detailed input and output sample data, so that we can better help for you.

Best Regards,

Liu Yang

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

amitchandak
Super User
Super User

@RGL , Try like

CALCULATE( Sales ,dateadd(Calendar[Date]), -7, Day ))

 

Or you can refer the Rank approach

 

Power BI — Week on Week and WTD
https://medium.com/@amitchandak.1978/power-bi-wtd-questions-time-intelligence-4-5-98c30fab69d3
https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-La...
https://www.youtube.com/watch?v=pnAesWxYgJ8

 

 

Why Time Intelligence Fails - Powerbi 5 Savior Steps for TI :https://youtu.be/OBf0rjpp5Hw
https://amitchandak.medium.com/power-bi-5-key-points-to-make-time-intelligence-successful-bd52912a5b...

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

I tried CALCULATE( Sales ,dateadd(Calendar[Date]), -7, Day )) 

and it works in a table by date like the one I showed in my example

 

BUT when ,  instead of date, I group by week number, I get blanks,

why don't I get the aggregate of the previous week sales?

 

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

Check out the April 2025 Power BI update to learn about new features.

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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