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
Anonymous
Not applicable

I want to create column in power bi which gives us previous id based on date column

I want to create column in power bi which gives us previous id based on date column.

 

 

Alice08_0-1733041048955.png

 

2 ACCEPTED SOLUTIONS
TomMartens
Super User
Super User

Hey @Anonymous ,

 

the below DAX creates a calculated column that stores the previous Shid based on the Date column.

The DAX:

PREVIOUS ID = 
var prevDate = 
    OFFSET(
        -1,
        SUMMARIZE(
            'Table',
            'Table'[Date]
        ),
        ORDERBY( 'Table'[Date] , ASC )
    )
return
SELECTCOLUMNS(
    TOPN(
        1,
        FILTER( 'Table', 'Table'[Date] = prevdate)
    ),
    "id", [Shid]
)

This is my data:

image.png

Hopefully, this provides what you are looking for.

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

View solution in original post

DataNinja777
Super User
Super User

Hi  @Anonymous , 

 

To create a column in Power BI that provides the previous ID based on the date column, there are several alternative approaches. However, I noticed that your date column only displays the day within the month, which may not be sufficient as a unique identifier, especially if the dates span across different months.

Due to this limitation, I have utilized the first column as an index column to ensure accuracy.

You can create a calculated column using the following formula:

 

Previous = VAR CurrentShid = HistoryRecord[Index]
RETURN
    CALCULATE(
        MAX(HistoryRecord[Shid]),
        FILTER(
            HistoryRecord,
            HistoryRecord[Index] < CurrentShid
        )
    )

 

DataNinja777_0-1733045214943.png

 

Alternatively, you can create a measure to use directly in a visualization.

 

To convert your calculated column Previous into a measure, you need to adjust the logic slightly, as measures operate on the aggregation context rather than row-by-row computations. Here's how you can write the measure equivalent:

Previous (measure) = 
VAR CurrentIndex = MAX(HistoryRecord[Index])  -- Get the current Index in the current context
RETURN
    CALCULATE(
        MAX(HistoryRecord[Shid]),
        FILTER(
            ALL(HistoryRecord),  -- Remove any filters to compare all rows
            HistoryRecord[Index] < CurrentIndex
        )
    )

The resulting output is as shown in the matrix visual below:

DataNinja777_1-1733045560502.png

 

 I have attached an example pbix file for your reference.

 

Best regards,

View solution in original post

2 REPLIES 2
DataNinja777
Super User
Super User

Hi  @Anonymous , 

 

To create a column in Power BI that provides the previous ID based on the date column, there are several alternative approaches. However, I noticed that your date column only displays the day within the month, which may not be sufficient as a unique identifier, especially if the dates span across different months.

Due to this limitation, I have utilized the first column as an index column to ensure accuracy.

You can create a calculated column using the following formula:

 

Previous = VAR CurrentShid = HistoryRecord[Index]
RETURN
    CALCULATE(
        MAX(HistoryRecord[Shid]),
        FILTER(
            HistoryRecord,
            HistoryRecord[Index] < CurrentShid
        )
    )

 

DataNinja777_0-1733045214943.png

 

Alternatively, you can create a measure to use directly in a visualization.

 

To convert your calculated column Previous into a measure, you need to adjust the logic slightly, as measures operate on the aggregation context rather than row-by-row computations. Here's how you can write the measure equivalent:

Previous (measure) = 
VAR CurrentIndex = MAX(HistoryRecord[Index])  -- Get the current Index in the current context
RETURN
    CALCULATE(
        MAX(HistoryRecord[Shid]),
        FILTER(
            ALL(HistoryRecord),  -- Remove any filters to compare all rows
            HistoryRecord[Index] < CurrentIndex
        )
    )

The resulting output is as shown in the matrix visual below:

DataNinja777_1-1733045560502.png

 

 I have attached an example pbix file for your reference.

 

Best regards,

TomMartens
Super User
Super User

Hey @Anonymous ,

 

the below DAX creates a calculated column that stores the previous Shid based on the Date column.

The DAX:

PREVIOUS ID = 
var prevDate = 
    OFFSET(
        -1,
        SUMMARIZE(
            'Table',
            'Table'[Date]
        ),
        ORDERBY( 'Table'[Date] , ASC )
    )
return
SELECTCOLUMNS(
    TOPN(
        1,
        FILTER( 'Table', 'Table'[Date] = prevdate)
    ),
    "id", [Shid]
)

This is my data:

image.png

Hopefully, this provides what you are looking for.

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

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.