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
Anonymous
Not applicable

how to calculate the price from extract day minus 3 day

How to write DAX formula to calculate the price from extract day - 3 days

I use the below formula and not getting the correct answer

3dayPrice = CALCULATE(AVERAGE(PowerBI_HistoricalMarketPricing[DailyRate]),FILTER(PowerBI_HistoricalMarketPricing,DATEADD(PowerBI_HistoricalMarketPricing[Extract Date].[Date],-3,DAY)))

tejagudipati_0-1624443427022.png

 

1 ACCEPTED SOLUTION

Hi @Anonymous ,

You can create a measure or calculated column as below to get it:

Measure:

3day back price = 
VAR _curdate =
    SELECTEDVALUE ( 'Table'[Extractdate] )
RETURN
    CALCULATE (
        MAX ( 'Table'[Price] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Car] = SELECTEDVALUE ( 'Table'[Car] )
                && DATEDIFF ( 'Table'[Extractdate], _curdate, DAY ) = 3
        )
    )

Calculated column:

Column = 
    CALCULATE (
        MAX ( 'Table'[Price] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Car] = EARLIER( 'Table'[Car] )
                && DATEDIFF ( 'Table'[Extractdate], EARLIER('Table'[Extractdate]), DAY ) = 3
        )
    )

yingyinr_1-1624605220773.png

Best Regards

Community Support Team _ Rena
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

Data sample.PNGHi @amitchandak ,
This formula will give me only for a max date - 3 days But I need the values for every extract date and it's 3day back price in the table

CarExtractdatePrice3day back price
Mid size22/06/2021133.2394.1
Mid size21/06/2021124.47144.23
Mid size20/06/2021112.18118.65
Mid size19/06/202194.198.35
Mid size18/06/2021144.23115.42
Mid size17/06/2021118.65 
Mid size16/06/202198.35 
Mid size15/06/2021115.42 

Hi @Anonymous ,

You can create a measure or calculated column as below to get it:

Measure:

3day back price = 
VAR _curdate =
    SELECTEDVALUE ( 'Table'[Extractdate] )
RETURN
    CALCULATE (
        MAX ( 'Table'[Price] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Car] = SELECTEDVALUE ( 'Table'[Car] )
                && DATEDIFF ( 'Table'[Extractdate], _curdate, DAY ) = 3
        )
    )

Calculated column:

Column = 
    CALCULATE (
        MAX ( 'Table'[Price] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Car] = EARLIER( 'Table'[Car] )
                && DATEDIFF ( 'Table'[Extractdate], EARLIER('Table'[Extractdate]), DAY ) = 3
        )
    )

yingyinr_1-1624605220773.png

Best Regards

Community Support Team _ Rena
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

@Anonymous , Try with a date table

 

CALCULATE(AVERAGE(PowerBI_HistoricalMarketPricing[DailyRate]),DATEADD(Date[Date],-3,DAY))

 

or


CALCULATE(AVERAGE(PowerBI_HistoricalMarketPricing[DailyRate]),FILTER(all(PowerBI_HistoricalMarketPricing),PowerBI_HistoricalMarketPricing[Extract Date] =max(PowerBI_HistoricalMarketPricing[Extract Date]) -3))

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.