Forum Discussion

likvors_husband's avatar
likvors_husband
Frequent Visitor
2 years ago
Solved

DAX Calculated Column for Comparing Values on Non-Consecutive Dates In the matching context

Hi everyone,

I'm working on a DAX calculation in Power BI where I need to compare a value on a given date with the value on the previous date for the same context of Product, Partner, Projection, and Element. However, the dates are not always consecutive and have duplicates*, so I'm using an Index column based on the date to help with this comparison.

In my example table, I'm encountering an issue where the rows with Index 13 and Index 32 are not recognized as the previous value.

edit*

 

PreviousValue =
VAR PreviousValue = 
    CALCULATE(
        SUM(FactData[Value]),
        TOPN(
            1,
            FILTER(
                ALLSELECTED('FactData'),
                'FactData[Product] = EARLIER('FactData'[Product]) &&
                'FactData'[Partner] = EARLIER('FactData'[Partner]) &&
                'FactData'[Element] = EARLIER('FactData'[Element]) &&
                'FactData'[Projection] = EARLIER('FactData'[Projection]) &&
                'FactData'[Index] < EARLIER('FactData'[Index])
            ),
            'FactData'[Index],
            DESC
        )
    )
RETURN
PreviousValue

 

 

Many thanks in advance for your help!

Index     Date     Product     Partner     Element     Value     Projection     PreviousValue     
108-Jan-24Product1Partner1Element17201-Apr-24 
215-Jan-24Product1Partner1Element17201-Apr-2472
322-Jan-24Product1Partner1Element17201-Apr-2472
429-Jan-24Product1Partner1Element17201-Apr-2472
505-Feb-24Product1Partner1Element15001-Apr-2472
612-Feb-24Product1Partner1Element15001-Apr-2450
719-Feb-24Product1Partner1Element15001-Apr-2450
826-Feb-24Product1Partner1Element15001-Apr-2450
904-Mar-24Product1Partner1Element15001-Apr-2450
1011-Mar-24Product1Partner1Element15001-Apr-2450
1118-Mar-24Product1Partner1Element15001-Apr-2450
1225-Mar-24Product1Partner1Element15001-Apr-2450
1302-Apr-24Product1Partner1Element14801-Apr-2450
1913-May-24Product1Partner1Element19601-May-24 
2020-May-24Product1Partner1Element19601-May-2496
3229-Jul-24Product1Partner1Element19601-Aug-24 
3331-Jul-24Product1Partner1Element19601-Aug-2496
3401-Aug-24Product1Partner1Element19601-Aug-2496
3505-Aug-24Product1Partner1Element19601-Aug-2496
3613-Aug-24Product1Partner1Element19601-Aug-2496
3719-Aug-24Product1Partner1Element19601-Aug-2496
  • Thank you, everyone, for your input! I wanted to share that I found the solution I was looking for right here on the forum. The method to compare values across non-consecutive rows was simpler than I thought and was already discussed in this thread. I hope this helps someone in the future as it did for me. Cheers!

3 Replies

    • likvors_husband's avatar
      likvors_husband
      Frequent Visitor

      Thank you, DataNinja777! However, the date column does contain duplicates, which is why I included the index column. Apologies for not mentioning that earlier—I completely overlooked it.

  • Thank you, everyone, for your input! I wanted to share that I found the solution I was looking for right here on the forum. The method to compare values across non-consecutive rows was simpler than I thought and was already discussed in this thread. I hope this helps someone in the future as it did for me. Cheers!