Forum Discussion

gchn's avatar
gchn
Frequent Visitor
3 years ago

Measure to get previous value with same attributes

I have a table with multiple attribute/category columns (call them A1, A2, ...), a date column and a value column.

I've been trying for too long to get a measure that gives me the value for the previously available date, given that all attribute are the same.

Example:
date              A1  A2 .... value   new_measure
06/30/2023   a    c            1      BLANK

06/30/2023   b    d           2      BLANK

07/03/2023   a    c            3      1

07/04/2023   a    c           4      3

07/03/2023   a    d           5      BLANK

07/04/2023   a    d          6       5


How can I do it?

6 Replies

  • gchn's avatar
    gchn
    Frequent Visitor

    Just the EARLIER function doesnt' work, can you be more specific?

  • Hi gchn 

    Try using:-

    new_measure =
    VAR CurrentDate = MAX('YourTable'[date])
    VAR PrevDate =
    CALCULATE(
    MAX('YourTable'[date]),
    FILTER(
    ALL('YourTable'),
    'YourTable'[date] < CurrentDate &&
    'YourTable'[A1] = EARLIER('YourTable'[A1]) &&
    'YourTable'[A2] = EARLIER('YourTable'[A2]) &&
    ...
    )
    )
    RETURN
    IF(ISBLANK(PrevDate), BLANK(),
    CALCULATE(
    MAX('YourTable'[value]),
    FILTER(
    ALL('YourTable'),
    'YourTable'[date] = PrevDate &&
    'YourTable'[A1] = EARLIER('YourTable'[A1]) &&
    'YourTable'[A2] = EARLIER('YourTable'[A2]) &&
    ...
    )
    )
    )
    Hope this will help.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi gchn ,

    Did the above suggestions help with your scenario? if that is the case, you can consider Kudo or Accept the helpful suggestions to help others who faced similar requirements.

    If these also don't help, please share more detailed information and description to help us clarify your scenario to test.

    How to Get Your Question Answered Quickly 

    Regards,

    Xiaoxin Sheng

  • gchn's avatar
    gchn
    Frequent Visitor

    Nothing of the above worked. I ended up using an actually useful tool (a Python script) to do that before feeding it into Power BI. I guess it's my fault for thinking a Microsoft product would work.

    • Anonymous's avatar
      Anonymous
      Not applicable

      HI gchn,

      Here is the dax measure formula and sample file about get the previous value based on use current date and multiple attribute field values:

      formula =
      VAR currDate =
          MAX ( 'Table'[date] )
      VAR prevDate =
          CALCULATE (
              MAX ( 'Table'[date] ),
              FILTER ( ALLSELECTED ( 'Table' ), [date] < currDate ),
              VALUES ( 'Table'[A1] ),
              VALUES ( 'Table'[A2] )
          )
      RETURN
          IF (
              prevDate <> BLANK (),
              CALCULATE (
                  SUM ( 'Table'[value] ),
                  FILTER ( ALLSELECTED ( 'Table' ), [date] = prevDate ),
                  VALUES ( 'Table'[A1] ),
                  VALUES ( 'Table'[A2] )
              )
          )


      Regards,

      Xiaoxin Sheng