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

Shape the future of the Fabric Community! Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions. Take survey.

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
v-yangliu-msft
Community Support
Community Support

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
v-yangliu-msft
Community Support
Community Support

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...

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

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
November Carousel

Fabric Community Update - November 2024

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

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.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.