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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Misq
Helper I
Helper I

Find value before last date

Hi all,

I need an answare for my question 😉 Looking for the way to get ItemNo for the date before the last date for ID and compare it to the last one.

So, right now my working measure to get the last ItemNo for last date for ID is looking as below (which is working good):

 
Last Item No= 
VAR __lastVisitDate = CALCULATE ( MAX ( Table1[Date] ), ALLEXCEPT ( Table1, Table1[ID] ) )

RETURN
CALCULATE ( MAX ( Table1[ItemNo ), ALLEXCEPT ( Table1, Table1[ID] ), Table1[Date] = __lastVisitDate )

Below is a sample of a table with data marked on green that I want to get:

iddateItemNo
11/1/2021100
12/1/2021200
13/1/2021300
21/1/2021100
22/1/2021200
23/1/2021300
24/1/2021400

 

Is it somehow possiable?

 

Appriciate for all your help!

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @Misq 

please try

2nd Last Item No =
VAR IdTable =
    CALCULATETABLE ( Table1, ALLEXCEPT ( Table1, Table1[ID] ) )
VAR LastTwoDatesTable =
    TOPN ( 2, IdTable, Table1[Date] )
VAR SecondLastDate =
    MINX ( LastTwoDatesTable, Table1[Date] )
RETURN
    MAXX (
        FILTER ( LastTwoDatesTable, Table1[Date] = SecondLastDate ),
        Table1[ItemNo]
    )

View solution in original post

1 REPLY 1
tamerj1
Super User
Super User

Hi @Misq 

please try

2nd Last Item No =
VAR IdTable =
    CALCULATETABLE ( Table1, ALLEXCEPT ( Table1, Table1[ID] ) )
VAR LastTwoDatesTable =
    TOPN ( 2, IdTable, Table1[Date] )
VAR SecondLastDate =
    MINX ( LastTwoDatesTable, Table1[Date] )
RETURN
    MAXX (
        FILTER ( LastTwoDatesTable, Table1[Date] = SecondLastDate ),
        Table1[ItemNo]
    )

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.