Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Lagged Values using M

Hi, I have a table similar to below.  Region Month of Period End Attribute Value State A 4/1/2022 Homes Sold 24% State A 3/1/2022 Homes Sold 22% State A 2/1/2022 Homes Sold 5...
  • jbwtp's avatar
    3 years ago

    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:

    which you use as the slicer:

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

     

     

     

     

    Cheers,

    John