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
PBI-BOLA
Frequent Visitor

Marking the last occurence in a table

Hi,

 

I'm trying to have a cell to indicate whether a particular intry is the last occurence of an item in a table.

 

Below is an example. For each item I want to know if it is the last occurance of that item in the table. For example for Item E the last transaction is 1008

 

So the field I need help writing a formula for is "Last Occurance?"

 

TransactionItemAmountLast occurrence?
1001B510Y
1002E114N
1003A464N
1004D499N
1005C874N
1006D102Y
1007E181N
1008E316Y
1009C976Y
1010A308Y

 

Thanks

3 REPLIES 3
v-eqin-msft
Community Support
Community Support

Hi @PBI-BOLA ,

 

According to my understanding, you want to set a "Y"/"N" flag for each item if it appears for the last time, right?

You could use the following formula to create a column :

 

Last Column =
IF (
    [Transaction]
        = CALCULATE ( MAX ( 'Table'[Transaction] ), ALLEXCEPT ( 'Table', 'Table'[Item] ) ),
    "Y",
    "N"
)

 

or measue like this:

 

Last Measure =
IF (
    MAX ( 'Table'[Transaction] )
        = MAXX (
            FILTER ( ALL ( 'Table' ), 'Table'[Item] = MAX ( 'Table'[Item] ) ),
            [Transaction]
        ),
    "Y",
    "N"
)

 

The final output is shown below:

1.29.3.1.PNG

You could take a look at the pbix file here.

 

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

Thanks for this. I think we may be on the right track. Initially I couldn't get your measure to work so I gave my Item field an index (Item Ind) as I figured it may be because the string is too complicated and applied the measure to this new field. Unfortunately it incorrectly recognises the last occurance for some lines. 

 

Below is some of my actual data which may help

 

PBI-BOLA_2-1612452513547.png

 

 

 

I extracted the "MAXX" into a column named "Maxx Test" formula from the measure to see what was happening and as you can see for some lines it comes out blank

Any thoughts on what may need to be adjusted to fix this?

Greg_Deckler
Super User
Super User

@PBI-BOLA  - Perhaps a measure like:

Last occurrence = 
  VAR __Current = MAX('Table'[Transaction])
  VAR __Item = MAX('Table'[Item])
  VAR __Table = FILTER(ALL('Table'),[Item]=__Item)
  VAR __Last = MAXX(__Table,[Transaction])
RETURN
  IF(__Last = __Current,"Y","N")

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.