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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
MarkSL
Helper V
Helper V

Get stock for same day last year

Hi all,

I have a large fact table which contains StockDate, Product, Units and which is related to my Dates table on StockDate.

I have a matrix which has Products in the Rows and with the following 3 measures:

Stock (Today)

Stock (Yesterday)

Stock (Same Day Last Year)

matrix.png

And the following measures:

 

Tot Units= SUM(Units)
Stock (Today) = CALCULATE([Tot Units], Dates[Date] = TODAY())
Stock (Yesterday) = CALCULATE([Tot Units], Dates[Date] = TODAY()-1)

 

Instead of calculating Stock (Same Day Last Year) as Today()-365, I would prefer to use SAMEPERIODLASTYEAR or DATEADD -1 year, so it does cater for the correct date each year.

However as the field Date is not in my Matrix, there is no row context for date, therefore SAMEPERIODLASTYEAR does not work. 

I have tried creating a column on my date table called 'IsToday', which will return 'Yes' if the date is today.  However I am struggling to get my DAX correct to use this date to retrieve Stock (Same Day Last Year).

Tot Units SPLY = CALCULATE([Tot Units], dateadd(Dates[Date], -1, YEAR), Dates[IsToday] = "Yes")
Any thoughts appreciated!
 
Mark

 

1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

HI @MarkSL ,

You can try to use following measure formula to calculate same day last year sales:

Measure =
CALCULATE (
    [Tot Units],
    FILTER (
        ALLSELECTED ( Table ),
        [Date]
            = DATE ( YEAR ( TODAY () - 1 ), MONTH ( TODAY () ), DAY ( TODAY () ) )
    )
)

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

2 REPLIES 2
v-shex-msft
Community Support
Community Support

HI @MarkSL ,

You can try to use following measure formula to calculate same day last year sales:

Measure =
CALCULATE (
    [Tot Units],
    FILTER (
        ALLSELECTED ( Table ),
        [Date]
            = DATE ( YEAR ( TODAY () - 1 ), MONTH ( TODAY () ), DAY ( TODAY () ) )
    )
)

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Hi @v-shex-msft ,

 

Many thanks that worked - I just had to make a small tweak as your code was returning yesterday, not same day last year, just a small typo I am sure:

Measure =
CALCULATE (
    [Tot Units],
    FILTER (
        ALLSELECTED ( Table ),
        [Date]
            = DATE ( YEAR ( TODAY () ) -1, MONTH ( TODAY () ), DAY ( TODAY () ) )
    )
)

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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