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
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
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.

Top Solution Authors