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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
DeBIe
Post Partisan
Post Partisan

How to use filter based on latest x transactions

Hi all,

 

I am trying to write the logic in PowerBI to return a result based on the latest x(5) transactions from all suppliers. this result should tell me if suppliers have been accounting me the same price or not. I know how to use a date filter for the current month or x previous days. But in this case, I don't know how to filter my measure to check the last 5 transactions. This has to be done for all suppliers (in my dataset I have hundreds).

 

Here is my example data set. Underneath I will show my expected result:

 

SupplierNameInvoiceIDSamePrice True/FalsePaymentDate
AmazonA-10True31-12-2020
AmazonAA-10True1-1-2021
Amazon A-23True2-1-2021
AmazonA-11True4-1-2021
AmazonA-13True5-1-2021
AmazonAA-290True7-1-2021
AmazonA-5True7-1-2021
EbayE-1True4-1-2021
EbayE-2False5-1-2021
EbayEE-1True6-1-2021
EbayE-31True8-1-2021
EbayE-9True12-1-2021
EbayE-111True13-1-2021
WallmartW-4True5-1-2021
WallmartW-5True5-1-2021
WallmartW-6True6-1-2021
WallmartW-9True9-1-2021

 

Expected Result

SuppliersTotal TransactionsSamePriceLast(5)Transactions
Amazon7True
Ebay6False
Wallmart4False (because 5 transactions need to be made)

 

Thank you kindly for your time.

1 ACCEPTED SOLUTION
v-xulin-mstf
Community Support
Community Support

Hi @DeBIe,

 

You can use the following method to meet your need:

Step1: Add an index column:

v-xulin-mstf_0-1610704523944.png

 

Step2: Create three measures as:

Total Transactions =

CALCULATE(

    COUNTROWS('Table'),

    FILTER(

        'Table',

        'Table'[SupplierName]='Table'[SupplierName]

    ))
measure 1 =

var _rank=RANKX(

FILTER(

    ALL('Table'),

    'Table'[SamePrice True/False]=TRUE()&& 'Table'[SupplierName]=MAX('Table'[SupplierName])

),

CALCULATE(MAX('Table'[PaymentDate])),,DESC

)

Return

IF(SELECTEDVALUE('Table'[SamePrice True/False])=FALSE(),BLANK(),IF(_rank<=5,1,0)



)
SamePriceLast(5)Transactions =

var _index=CALCULATE(MIN('Table'[Index]),FILTER(ALLSELECTED('Table'),'Table'[measure 1]&&'Table'[SupplierName]=MAX('Table'[SupplierName])))

var _indexmax=CALCULATE(MAX('Table'[Index]),FILTER(ALLSELECTED('Table'),'Table'[measure 1]&&'Table'[SupplierName]=MAX('Table'[SupplierName])))

Return

IF(_indexmax-_index<>4,FALSE(),TRUE())

Step3: Here is the output:

v-xulin-mstf_1-1610704523949.png

 

Here is the demo, please try it: How to use filter based on latest x transactions

 

Best Regards,

Link

View solution in original post

2 REPLIES 2
v-xulin-mstf
Community Support
Community Support

Hi @DeBIe,

 

You can use the following method to meet your need:

Step1: Add an index column:

v-xulin-mstf_0-1610704523944.png

 

Step2: Create three measures as:

Total Transactions =

CALCULATE(

    COUNTROWS('Table'),

    FILTER(

        'Table',

        'Table'[SupplierName]='Table'[SupplierName]

    ))
measure 1 =

var _rank=RANKX(

FILTER(

    ALL('Table'),

    'Table'[SamePrice True/False]=TRUE()&& 'Table'[SupplierName]=MAX('Table'[SupplierName])

),

CALCULATE(MAX('Table'[PaymentDate])),,DESC

)

Return

IF(SELECTEDVALUE('Table'[SamePrice True/False])=FALSE(),BLANK(),IF(_rank<=5,1,0)



)
SamePriceLast(5)Transactions =

var _index=CALCULATE(MIN('Table'[Index]),FILTER(ALLSELECTED('Table'),'Table'[measure 1]&&'Table'[SupplierName]=MAX('Table'[SupplierName])))

var _indexmax=CALCULATE(MAX('Table'[Index]),FILTER(ALLSELECTED('Table'),'Table'[measure 1]&&'Table'[SupplierName]=MAX('Table'[SupplierName])))

Return

IF(_indexmax-_index<>4,FALSE(),TRUE())

Step3: Here is the output:

v-xulin-mstf_1-1610704523949.png

 

Here is the demo, please try it: How to use filter based on latest x transactions

 

Best Regards,

Link

d_gosbell
Super User
Super User

Something like the following should work 

 

SamePriceLast(5)Transactions = IF (HASONEFILTER(Transactions[SupplierName]),
    var maxDate = MAX(Transactions[PaymentDate].[Date])
    var last5dates = TOPN(5, CALCULATETABLE(Transactions, Transactions[PaymentDate].[Date] <= maxDate), Transactions[PaymentDate])
    return countrows( FILTER( last5dates, [SamePrice True/False] = TRUE())) = 5
,"N/A")

 

First I check if we are filtered by supplier (ie if supplier is on the rows or a single supplier is selected in a filter)

Then I grab the max date (incase the date is filtered, so if you filter for Jan 2021 this measure will only consider transactions on or before 31 Jan 2021) and then get the top 5 transactions before the max date. From there I count the rows where same price is true and check if that is equal to 5. 

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.