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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

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
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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