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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
sekela
Regular Visitor

How to get the previous row Value In Dax like LAG in TSQL

 

I want to get the previous value partitioned by country and order by year like the screenshot below highlighted in red.

I tried the following and the result is not as expected

PreviousValue =
CALCULATE (
MAX ( Sheet1[Total Sales] ),
FILTER (
Sheet1,
EARLIER ( Sheet1[Year] ) > Sheet1[Year]
&& EARLIER ( Sheet1[RowNumber] ) > Sheet1[RowNumber]
&& EARLIER ( Sheet1[Country] ) = Sheet1[Country]
)
)

sekela_1-1644584328372.png

 

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @sekela 

This calculate column code should work


72B46F3F-8A26-4C01-AFB1-0D2659FF995B.png

PreviousValue =
VAR CurrentCountry = Sheet1[Country]
VAR CurrentYear = Sheet1[Year]
VAR FilteredTable =
    FILTER (
        Sheet1,
        Sheet1[Country] = CurrentCountry
            && Sheet1[Year] = CurrentYear - 1
    )
VAR Result =
    SUMX ( FilteredTable, Sheet1[Total Sales] )
RETURN
    Result

View solution in original post

3 REPLIES 3
tamerj1
Super User
Super User

Hi @sekela 

This calculate column code should work


72B46F3F-8A26-4C01-AFB1-0D2659FF995B.png

PreviousValue =
VAR CurrentCountry = Sheet1[Country]
VAR CurrentYear = Sheet1[Year]
VAR FilteredTable =
    FILTER (
        Sheet1,
        Sheet1[Country] = CurrentCountry
            && Sheet1[Year] = CurrentYear - 1
    )
VAR Result =
    SUMX ( FilteredTable, Sheet1[Total Sales] )
RETURN
    Result

ValtteriN
Super User
Super User

Hi,

Could you add sample data in a table format and a snippet about the "not expected values"? For now, I recommend trying to reverse < directions here:


PreviousValue =
CALCULATE (
MAX ( Sheet1[Total Sales] ),
FILTER (
Sheet1,
EARLIER ( Sheet1[Year] ) < Sheet1[Year]
&& EARLIER ( Sheet1[RowNumber] ) < Sheet1[RowNumber]
&& EARLIER ( Sheet1[Country] ) = Sheet1[Country]
)




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




YearCountryQuantityUnit price Total Sales RowNumber
2017USA510501
2018USA2151052
2019USA411443
2020USA83244
2021USA127845
2017Japan2481
2018Japan611662
2019Japan82163
2020Japan9131174
2021Japan321635
2017China73211
2018China8182
2019China21357353
2020China120204
2021China615905

 

sekela_0-1644593688073.png

 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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