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
JdBernate
Frequent Visitor

Calculate last buy an prior buy

Hello

 

Im trying to find out  changes on costs of some items through time , so i have to calculate the last buy and the prior buy by item. So far i calculated the last buy using the next script:

 

Lastbuy=

CALCULATE(sum(buy[buy value]);FILTER('buy';MONTH(MAX('buy[Date]))))

 

Like you can see i need to compare the buy by month.

 

Im having troubles to calculate the prior buy to "LastBuy". I haved try this:

 

PriorBuy =
VAR LastDate=
    month(MAX('Buy'[date]))
    
RETURN
CALCULATE(sum('buy'[Buy Value]);FILTER('TBuy';LastDate>month('Buy'[Fecha Ingreso])))

 

But is not working!!. My objetive is to obtain the prior buy doesnt matter if it was the exactly prior month or 4 months ago, for instance:

 

Product:

X

Last buy

$$$$ (for instance the buys of May 2018)

PriorBuy

$$$$ (Should be the buy of january 2018, the prior buy to product X).

 

My model is this table Buy, a calendar table and another tables that doesnt have relationship with this issue

 

I hope you guys can help me

 

My best Regards

 

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

HI @JdBernate,

 

I'd like some sample data and expected result to clarify your requirement.

 

In addition, you can try to use below formula if you want to calculate product sales in previous four month.

PriorBuy =
VAR LastDate =
    MAX ( 'Buy'[date] )
RETURN
    CALCULATE (
        SUM ( 'Buy'[Buy Value] );
        FILTER (
            'TBuy';
            FORMAT ( 'Buy'[Fecha Ingreso]; "mm/yyyy" )
                = FORMAT (
                    DATE ( YEAR ( lastdate ); MONTH ( lastdate ) - 4; DAY ( lastdate ) );
                    "mm/yyyy"
                )
        );
        VALUES ( 'Buy'[Product] )
    )

 

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 @JdBernate,

 

I'd like some sample data and expected result to clarify your requirement.

 

In addition, you can try to use below formula if you want to calculate product sales in previous four month.

PriorBuy =
VAR LastDate =
    MAX ( 'Buy'[date] )
RETURN
    CALCULATE (
        SUM ( 'Buy'[Buy Value] );
        FILTER (
            'TBuy';
            FORMAT ( 'Buy'[Fecha Ingreso]; "mm/yyyy" )
                = FORMAT (
                    DATE ( YEAR ( lastdate ); MONTH ( lastdate ) - 4; DAY ( lastdate ) );
                    "mm/yyyy"
                )
        );
        VALUES ( 'Buy'[Product] )
    )

 

Regards,

Xiaoxin Sheng

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

Thank you very much my friend!!.  Your solution was very useful

 

My best regards

 

Juan Diego.

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.