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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
Anonymous
Not applicable

Lagged Values using M

Hi, I have a table similar to below. 

RegionMonth of Period EndAttributeValue
State A4/1/2022Homes Sold24%
State A3/1/2022Homes Sold22%
State A2/1/2022Homes Sold5%
State A1/1/2022Homes Sold6%
State B5/1/2022Home Price8%
State B4/1/2022Home Price9%
State B3/1/2022Home Price2%
State B2/1/2022Home Price34%
State B1/1/2022Home Price22%

 

I want to create a dynamic column of lagged values such that the period of lag (1 month/2 months/3 months/...) can be selected via a slicer. The calculation of the lag column depicted in the last 3 columns below:

RegionMonth of Period EndAttributeValueLagged by 1 (Minus 1 month)Lagged by 2 (Minus 2 months)Lagged by 3 (Minus 3 months)
State A4/1/2022Homes Sold24%22%5%..
State A3/1/2022Homes Sold22%5%6%..
State A2/1/2022Homes Sold5%6%-..
State A1/1/2022Homes Sold6%--..
State B5/1/2022Home Price8%9%2%..
State B4/1/2022Home Price9%2%34%..
State B3/1/2022Home Price2%34%22%..
State B2/1/2022Home Price34%22%-..
State B1/1/2022Home Price22%--..

 

Please help me out with what will work. I want to input these columns using M in the power query. Thanks.

1 ACCEPTED SOLUTION
jbwtp
Memorable Member
Memorable Member

Hi @Anonymous,

 

I think doing this in PQ would be a bit of overkill, why would you need this chunks of duplicated data to be stored?

Then, how do you want to make it slicer dependant? Via slicer parameter?

 

I love PQ/M and, probably, a bit sceptical re DAX, but in this case, it looks like a DAX job.

Assuming your table is called Data and you have another table called Offset, which is just one column of 1,2,3,4,5:

jbwtp_0-1666733445423.png

which you use as the slicer:

jbwtp_1-1666733472298.png

This is the DAX for calculating a measure (which you can add as a column to your visual):

 

Lagged = 
var dt = FIRSTDATE(DATEADD('Data'[Month of Period End], -minx('Offset', 'Offset'[Offset]), MONTH))
return CALCULATE(SUM('Data'[Value]), Filter(ALL('Data'[Month of Period End]), 'Data'[Month of Period End] = dt))

 

 

jbwtp_2-1666733588202.png

 

 

Cheers,

John

 

 

View solution in original post

2 REPLIES 2
jbwtp
Memorable Member
Memorable Member

Hi @Anonymous,

 

I think doing this in PQ would be a bit of overkill, why would you need this chunks of duplicated data to be stored?

Then, how do you want to make it slicer dependant? Via slicer parameter?

 

I love PQ/M and, probably, a bit sceptical re DAX, but in this case, it looks like a DAX job.

Assuming your table is called Data and you have another table called Offset, which is just one column of 1,2,3,4,5:

jbwtp_0-1666733445423.png

which you use as the slicer:

jbwtp_1-1666733472298.png

This is the DAX for calculating a measure (which you can add as a column to your visual):

 

Lagged = 
var dt = FIRSTDATE(DATEADD('Data'[Month of Period End], -minx('Offset', 'Offset'[Offset]), MONTH))
return CALCULATE(SUM('Data'[Value]), Filter(ALL('Data'[Month of Period End]), 'Data'[Month of Period End] = dt))

 

 

jbwtp_2-1666733588202.png

 

 

Cheers,

John

 

 

Anonymous
Not applicable

This was awesome, thanks!

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

February Power BI Update Carousel

Power BI Monthly Update - February 2026

Check out the February 2026 Power BI update to learn about new features.

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.

Top Solution Authors
Users online (6,854)