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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Avinash19
Regular Visitor

last 3 days sales

Hi,

 

I need help with a dax query.

 

I want to calculate last 3 days sales of a product in a particular store, so if there is a sale in last 3 days then it will display result as 1 but if there is no sale in last 3 days then result should be 0.

 

STORE

---------

date     product_id  store_id  cnt_txns  last3days sales
06/12/22A11210
07/12/22A11251(we can see a sale on 06/12/2022 for same product and store)
11/12/22A11200(no sale on 8,9,10,11)
06/12/22B11380
07/12/22B11311(we can see a sale on 06/12/2022 for same product and store)
07/12/22C11410

I'm using below dax to calculate the last 3 days sale on 11/12/2022, but it is returning only output for cnt_txns 0 ie product_id A1 and store_id 12. But it should also return Product B1,C1 since there were no txns for these 2 also for past 3 days.

 

sales last 3 days = var currdate = MAX(Calender[Date])

var dteinx = SELECTEDVALUE(store[Index])

return

IF(dteinx >1,

CALCULATE(sum(store[cnt_trx]),DATESINPERIOD(Calender[Date],currdate,-3,DAY)))

 

 

Can some please help me to understand what I'm doing wrong?

 

1 ACCEPTED SOLUTION

Hi, @Avinash19 

Please try following measure.

vyaningymsft_1-1713428633114.png

previousDate =
VAR _index =
    MAXX (
        FILTER ( ALL ( 'Table' ), [Index] < SELECTEDVALUE ( 'Table'[Index] ) ),
        [Index]
    )
VAR _previous_date =
    CALCULATE (
        MAX ( 'Table'[date(m/d/y)] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Index] = _index )
    )
VAR _previous_product_id =
    CALCULATE (
        MAX ( 'Table'[product_id] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Index] = _index )
    )
VAR _previous_store_id =
    CALCULATE (
        MAX ( 'Table'[store_id] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Index] = _index )
    )
VAR _date_diff =
    IF (
        NOT ISBLANK ( _previous_date ),
        CALCULATE ( INT ( SELECTEDVALUE ( 'Table'[date(m/d/y)] ) - _previous_date ) )
    )
VAR _isSameProductID =
    IF (
        ( NOT ISBLANK ( _previous_product_id ) )
            && SELECTEDVALUE ( 'Table'[product_id] ) = _previous_product_id,
        1
    )
VAR _isSameStoreID =
    IF (
        ( NOT ISBLANK ( _previous_store_id ) )
            && SELECTEDVALUE ( 'Table'[store_id] ) = _previous_store_id,
        1
    )
RETURN
    IF (
        _isSameProductID && _isSameStoreID
            && ( _date_diff >= 0
            && _date_diff <= 3 ),
        1,
        0
    )

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum


 

View solution in original post

5 REPLIES 5
v-yaningy-msft
Community Support
Community Support

Hi, @Avinash19 

Thanks for @Greg_Deckler reply. Based on your description, I have created some measures to achieve the effect you are looking for. Following picture shows the effect of the display.

vyaningymsft_0-1713251529127.png


Measure:

sales last 3 days = 
VAR _currdate =
    MAX ( 'Calendar'[date] )
VAR _result =
    CALCULATE (
        SUM ( store[cnt_trx] ),
        FILTER ( store, store[date] <= _currdate - 3 || store[date] > _currdate )
    )
RETURN
    IF ( NOT ISBLANK ( _result ), 0, 1 )

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

 

Hi Yang,

 

thanks for your reply.

 

but the output should be like below.

STORE

---------

dateproduct_idstore_idcnt_txnslast3days sales
06/12/22A11210
07/12/22A11251(we can see a sale on 06/12/2022 for same product and store)
11/12/22A11200(no sale on 8,9,10,11)
06/12/22B11380
07/12/22B11311(we can see a sale on 06/12/2022 for same product and store)
07/12/22C11410

Hi, @Avinash19 

Please try following measure.

vyaningymsft_1-1713428633114.png

previousDate =
VAR _index =
    MAXX (
        FILTER ( ALL ( 'Table' ), [Index] < SELECTEDVALUE ( 'Table'[Index] ) ),
        [Index]
    )
VAR _previous_date =
    CALCULATE (
        MAX ( 'Table'[date(m/d/y)] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Index] = _index )
    )
VAR _previous_product_id =
    CALCULATE (
        MAX ( 'Table'[product_id] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Index] = _index )
    )
VAR _previous_store_id =
    CALCULATE (
        MAX ( 'Table'[store_id] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Index] = _index )
    )
VAR _date_diff =
    IF (
        NOT ISBLANK ( _previous_date ),
        CALCULATE ( INT ( SELECTEDVALUE ( 'Table'[date(m/d/y)] ) - _previous_date ) )
    )
VAR _isSameProductID =
    IF (
        ( NOT ISBLANK ( _previous_product_id ) )
            && SELECTEDVALUE ( 'Table'[product_id] ) = _previous_product_id,
        1
    )
VAR _isSameStoreID =
    IF (
        ( NOT ISBLANK ( _previous_store_id ) )
            && SELECTEDVALUE ( 'Table'[store_id] ) = _previous_store_id,
        1
    )
RETURN
    IF (
        _isSameProductID && _isSameStoreID
            && ( _date_diff >= 0
            && _date_diff <= 3 ),
        1,
        0
    )

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum


 

Greg_Deckler
Super User
Super User

@Avinash19 Something going awry with CALCULATE and DAX TI functions. Avoid them:

sales last 3 days = var currdate = MAX(Calender[Date])

var dteinx = SELECTEDVALUE(store[Index])

return

IF(dteinx >1,

SUMX(FILTER('store', [date] >= __currdate - 3), [cnt_trx])

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hi Greg,

 

thanks for ur solution but its still returning the same output what i'm getting with my code. Its not returning other values.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.