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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
aashoe
Regular Visitor

How to calculate LY comp with a non-contiguous selection

My file has 5 filters. I used the "dateadd" function to calculate the LY's sales (

Total Sales LY =CALCULATE([Total Sales],dateadd(Sales[Day],-1,YEAR))). When I use the filter apart from the date, I encounter an error as not every day last year had sales of each brand/shipping region/category. How should I solve this?

filter.JPG

Capture.JPG

 

11 REPLIES 11
Anonymous
Not applicable

Hi @aashoe 

 

Maybe you can use filter instead of Time intelligence functions, just as follow:

Total Sales LY =
CALCULATE (
    [Total Sales],
    ALLSELECTED ( 'Sales' ),
    Sales[Day] < TODAY ()
        && Sales[Day]
            >= DATE ( YEAR ( Sales[Day] ) - 1, MONTH ( Sales[Day] ), DAY ( Sales[Day] ) )
)

 

 

Best Regards

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

This formula works for the filter, but the LY sales is not correctly shown. It adds up all the sales data in the table. Any way to solve it? 

 

sales.JPG

Anonymous
Not applicable

Hi @aashoe 

 

Please try this:

IF (
    MAX ( Sales[Day] ) < TODAY ()
        && MAX ( Sales[Day] )
            >= DATE ( YEAR ( Sales[Day] ) - 1, MONTH ( Sales[Day] ), DAY ( Sales[Day] ) ),
    [Total Sales]
)

 

 

Best Regards

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

I encountered the below error. 

Screenshot 2024-10-21 101837.png

The system only let me picked from these measures in line 5. 

Screenshot 2024-10-21 102104.png

Anonymous
Not applicable

Hi @aashoe 

 

I'm sorry, I misunderstood what you meant before,

Try this please:

Total Sales LY =
VAR _CurrentRowDate =
    MAX ( Sales[Day] )
RETURN
    CALCULATE (
        [Total Sales],
        ALL ( 'Sales' ),
        Sales[Day]
            = DATE ( YEAR ( Sales[Day] ) - 1, MONTH ( Sales[Day] ), DAY ( Sales[Day] ) )
            && Sales[Day] < TODAY ()
    )

 

 

Hope this helps,

 

Best Regards

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

 

There’s no error showing in the calculation, but it’s still returning 0 for the LY sales. 😞

Anonymous
Not applicable

Hi @aashoe 

 

Could you please provide more raw data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples? It would be helpful to find out the solution. You can refer the following links to share the required info:

How to provide sample data in the Power BI Forum

How to Get Your Question Answered Quickly

And It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

 

 

Best Regards

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

AlexisOlson
Super User
Super User

DATEADD should usually operate on your date table, not your Sales fact table.

 

If you don't have a date table, you really should add one.

 

Do You Need a Date Dimension? - RADACAD

I created a date table (Table = CALENDAR ( DATE ( 2023, 1, 1 ), DATE ( 2024, 12, 31 ) )), and connected it with the "Day" in other tables. Yet, I still encountered the same error. Which part did I miss?

connection.JPG

Also, I want the filter to show the date in 2024 only. But when I use the filter on this visual funtion to change it, the LY sales cannot be calculated. Is there a way to solve it? Thank you so much! 

filter.JPG

Yikes! Why do you have two bidirectional many-to-many relationships between fact tables? I can't think of a situation that should never happen.

 

Are you using the date table for the slicer? Does the date table contain all the dates that are in the fact tables or do either of those go back before 2023?

I currently have 5 tables: 4 fact tables and 1 date table. I removed the day link between the fact tables and maintained a many-to-many relationship using the product style name. Does that setup make sense to you?

For the slicer, I’m using the date table. The fact tables only contain sales data for September 2023 and September 2024. Should I stick with the date table for the slicer, or would it be better to use the day field from the sales table instead?

Thank you so much for your help! I’m still learning, and your guidance means a lot to me!

tables link.JPG

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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