Forum Discussion

Oros's avatar
Oros
Icon for Post Prodigy rankPost Prodigy
2 years ago
Solved

Get last 2 transactions

Hello,   I have one table that has transactions for several products.  How do you separately get the last 2 transactions for each specific items? The transaction id is unique and increments for al...
  • Anonymous's avatar
    Anonymous
    2 years ago

    I think you'll need to create a rank column for the transactions by product;

     

     

    Rank =
    RANKX( FILTER( ALL('Sales'),
    'Sales'[Product] = EARLIER('Sales'[Product])),
    'Sales'[Transaction Date],, DESC, Dense )

     

    Then for each of the other components, you can create additional columns; based on rank. You might need to adjust max to sum or whichever other aggregate you use for quantity.

     

    Quantity #1 = 
    CALCULATE(MAX('Sales'[Quantity]),
    FILTER(
    'Sales',
    'Sales'[Product] = EARLIER('Sales'[Product]) && 'Sales'[Rank] = 1))
    Expiry Date #1 = 
    CALCULATE(MAX('Sales'[Expiry Date]),
    FILTER(
    'Sales',
    'Sales'[Product] = EARLIER('Sales'[Product]) && 'Sales'[Rank] = 1))

     

    and then create the same for #2, just changing the rank, then you can create your matrix

  • Ahmedx's avatar
    2 years ago

    Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi, 

    Thanks for the solutions Anonymous  and Ahmedx provided, and i want to offer some more information for user to refer to.

    hello Oros , based on your description, you can refer to the following sample.

    Sample data :

    You can create the following measures

     

    Experdate1 =
    VAR a =
        SUMMARIZE (
            TOPN (
                2,
                FILTER ( ALLSELECTED ( 'Table' ), [Product] IN VALUES ( 'Table'[Product] ) ),
                CALCULATE ( MAX ( 'Table'[Transaction#] ) ), DESC
            ),
            [Transaction#]
        )
    RETURN
        CALCULATE (
            MAX ( 'Table'[Expiry] ),
            'Table'[Transaction#] = MINX ( a, [Transaction#] )
        )
    
    Experdate2 =
    VAR a =
        SUMMARIZE (
            TOPN (
                2,
                FILTER ( ALLSELECTED ( 'Table' ), [Product] IN VALUES ( 'Table'[Product] ) ),
                CALCULATE ( MAX ( 'Table'[Transaction#] ) ), DESC
            ),
            [Transaction#]
        )
    RETURN
        CALCULATE (
            MAX ( 'Table'[Expiry] ),
            'Table'[Transaction#] = MAXX ( a, [Transaction#] )
        )
    
    Qty_1 = CALCULATE(SUM('Table'[Qty]),FILTER('Table','Table'[Expiry]=[Experdate1]))
    Qty_2 = CALCULATE(SUM('Table'[Qty]),FILTER('Table','Table'[Expiry]=[Experdate2]))

     

    Output

     

    Best Regards!

    Yolo Zhu

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