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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
JustCraig
Frequent Visitor

Rankx: Finding the Last Transaction per Contract within a Date Period

Good day all,

 

I have a table with multiple banking transactions in per contract. A batch of transactions is run on a pecific date for multiple contracts. The client wants to see what all the last transactions per contract are and to see a summary table of all the last transaction statuses and the amount of contracts associated to each, based on a date slicer.

 

So if this is the sample data for instance:

MandateEffectiveDateStatusContractNoTransactionDateTransactionRank
12/30/2024Payment successful4229912/30/2024 10:05:04 AM1
12/30/2024Payment successful4229912/30/2024 10:36:10 AM2
12/30/2024Payment successful4229912/30/2024 11:03:04 AM3
12/30/2024Payment successful4229912/30/2024 11:40:41 AM4
12/30/2024Transaction declined, please contact your bank for assistance.4229912/30/2024 2:18:29 PM5
1/30/2025Denied by Fraud System.422991/30/2025 3:11:12 PM6
1/30/2025Denied by Fraud System.422991/30/2025 3:12:31 PM7
1/30/2025Denied by Fraud System.422991/30/2025 3:13:43 PM8
1/30/2025System malfunction422991/30/2025 3:14:47 PM9
1/30/2025Invalid account number422991/30/2025 3:27:30 PM10
1/30/2025Payment successful422991/30/2025 7:09:15 AM11
1/30/2025Payment successful422991/30/2025 7:09:49 AM12
1/30/2025Payment successful425711/30/2025 7:32:56 AM1
1/30/2025System malfunction425711/31/2025 10:02:11 AM2
1/30/2025System malfunction425711/31/2025 6:02:03 AM3
1/30/2025Denied by Fraud System.425711/31/2025 6:59:11 AM4
1/30/2025Denied by Fraud System.425711/31/2025 7:01:38 AM5
1/30/2025Denied by Fraud System.432531/31/2025 7:03:47 AM1
1/30/2025Payment successful432531/31/2025 7:22:46 AM2
1/30/2025Transaction declined, please contact your bank for assistance.432532/1/2025 10:31:37 AM3
1/30/2025System malfunction432532/1/2025 11:07:53 AM4
1/30/2025Transaction declined, please contact your bank for assistance.432532/1/2025 3:02:11 PM5
1/30/2025Transaction declined, please contact your bank for assistance.432532/1/2025 3:07:15 PM6
1/30/2025System malfunction432532/1/2025 6:02:07 AM7
1/30/2025Denied by Fraud System.432532/2/2025 10:18:24 AM8

 

Once the client selects lets say 1/30/2025 (which is the MandateEffectiveDate), the two visuals must display

Detail: Show only the last transaction per contract and all the relevant info, so:

MandateEffectiveDateStatusContractNoTransactionDateTransactionRank
1/30/2025Payment successful422991/30/2025 7:09:49 AM12
1/30/2025Denied by Fraud System.425711/31/2025 7:01:38 AM5
1/30/2025Denied by Fraud System.432532/2/2025 10:18:24 AM8

 

And the other table must count the contracts within the table above and group the according the transaction Status:

StatusTotalContracts
Payment successful1
Denied by Fraud System.2

 

If Payment successful is selected in the second table it must filter the second table accordingly to only show that one contract.

 

So I added a calculated column to rank each transaction per contract:

TransactionRank = 
RANKX(
    FILTER(
        MandatesData,
        MandatesData[ContractNo] = EARLIER(MandatesData[ContractNo])
    ),
    MandatesData[TransactionDate],
    ,
    ASC,
    Dense
)

Then I created a measure to determine the last transaction - based on the dynamic date slicer:

MaxTransactionRankMeasure = 
CALCULATE(
    MAX(MandatesData[TransactionRank]),
    FILTER(
        ALLSELECTED(MandatesData),
        MandatesData[ContractNo] = MAX(MandatesData[ContractNo]) &&
        MandatesData[MandateEffectiveDate] <= MAX(MandatesData[MandateEffectiveDate])
    )
)

Then a flag measure to use as a filter on the first table:

IsLastTransactionMeasure = 
IF(
    MAX(MandatesData[TransactionRank]) = [MaxTransactionRankMeasure] &&
    NOT ISBLANK(MAX(MandatesData[TransactionRank])),
    1,
    0
)

And if I apply this as a filter to the first table, where IsLastTransactionMeasure = 1, it creates the desired result within the table after selecting the relevant date range.

 

But I now cannot get the second table visual to group the Transaction statuses and count the contracts associated. I tried this measure but it doesn't produce the desired relust and if you select a status the incorrect amount of contracts appears in the first table

TotalContracts = 
SUMX(
    VALUES(MandatesData[Status]),
    CALCULATE(
        DISTINCTCOUNT(MandatesData[ContractNo]),
        FILTER(
            ALLSELECTED(MandatesData),
            [IsLastTransactionMeasure] = 1
        )
    )
)

No idea if any of this makes any sense at this point

1 ACCEPTED SOLUTION
parry2k
Super User
Super User

@JustCraig solution is attached, tweak it as you see fit. 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

3 REPLIES 3
JustCraig
Frequent Visitor

Ok, finally got it to work. No idea if this is the correct way, but it prodces the correct result:

StatusCount = 
VAR LatestTransactions = 
    FILTER(
        ALLSELECTED('MandatesData'),
        'MandatesData'[TransactionDate] = 
            CALCULATE(
                MAX('MandatesData'[TransactionDate]),
                ALLEXCEPT('MandatesData', 'MandatesData'[ContractNo], 'MandatesData'[MandateEffectiveDate])
            )
    )
VAR StatusSummary = 
    SUMMARIZE(
        LatestTransactions,
        'MandatesData'[Status],
        "ContractCount", DISTINCTCOUNT('MandatesData'[ContractNo])
    )
RETURN
IF(
    NOT ISBLANK([Last Transaction Date]),
    IF(
        HASONEVALUE('MandatesData'[Status]),
        SUMX(
            StatusSummary,
            IF(
                'MandatesData'[Status] = SELECTEDVALUE('MandatesData'[Status]),
                [ContractCount],
                0
            )
        ),
        SUMX(
            StatusSummary,
            [ContractCount]
        )
    ),
    BLANK()
)
parry2k
Super User
Super User

@JustCraig solution is attached, tweak it as you see fit. 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

@parry2k thank you for your response and proposed solution.

 

The [Last Transaction Date] measure works well and after I applied it to a batch with relevant filters, it produced the correct detail as expected.

However, after applying the [Count by Status] measure, it only counts 1 instance it seems of each status, the result being:

JustCraig_0-1742377495547.png

When it should be as follows based on the detail:

JustCraig_1-1742377579962.png

 

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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

Top Solution Authors