0
votes

enter image description here

I have three columns: date, companyID & Users. Below is a sample table:

Date         CompanyID     Users
2/14/2020        1           20
1/30/2020        1           45
12/30/2019       1           55

What I am looking to do is create a DAX measure, that will look up the previous value. For example:

Date         CompanyID     Users       Pre_value
2/14/2020        1           20           45
1/30/2020        1           45           55
12/30/2019       1           55            0

The date add functions are not working, because my dates are inconsistent. My date column will show today date & the end of month date for previous months.

1

1 Answers

0
votes

Perhaps something like this:

Pre_Value = 
    VAR CompanyID = 'Table'[CompanyID]
    VAR Date1 = 'Table'[Date]
    VAR Pre_Date = CALCULATE(MAX('Table'[Date]),FILTER('Table','Table'[CompanyID]=CompanyID),FILTER('Table','Table'[Date]<Date1))    
    VAR Prev_Val = CALCULATE(MAX('Table'[Users]),FILTER('Table','Table'[CompanyID]=CompanyID),FILTER('Table','Table'[Date]=Pre_Date))
RETURN IF(Prev_Val=BLANK(),0,Prev_Val)

The idea is to get the previous date from the current row (VAR pre_date) and use that to get the corresponding value. Hope this helps.

Edit: If you want to go back 2 records:

Pre_Value_2 = 
    VAR CompanyID = 'Table'[CompanyID]
    VAR Date1 = 'Table'[Date]
    VAR Pre_Date = CALCULATE(MAX('Table'[Date]),FILTER('Table','Table'[CompanyID]=CompanyID),FILTER('Table','Table'[Date]<Date1))    
    VAR Pre_Date_2 = CALCULATE(MAX('Table'[Date]),FILTER('Table','Table'[CompanyID]=CompanyID),FILTER('Table','Table'[Date]<Pre_date))
    VAR Prev_Val_2 = CALCULATE(MAX('Table'[Users]),FILTER('Table','Table'[CompanyID]=CompanyID),FILTER('Table','Table'[Date]=Pre_Date_2))
RETURN IF(Prev_Val_2=BLANK(),0,Prev_Val_2)

I have created another variable to go back one more date (pre_date_2).