Forum Discussion

JemmaD's avatar
JemmaD
Icon for Helper V rankHelper V
1 year ago
Solved

Looking up data to compare when not on the same row

Hi there,    I have a data table with a unique id, then an attribute for current product, previous product, current cost, previous cost etc.   Each unique id can have more than one row per produc...
  • danextian's avatar
    1 year ago

    Hi JemmaD 

    The unique ID alone will not tell us which record comes first. There should be an indicator column for such like an update or change timestamp. I wouldn't use Power Query to get the previous row's value as it is not optimized for scanning tables but depending on the size of the dataset, I would use a combination of both DAX and M for an optimized calculation. 

     

    In the below, I example I used an index column to indicate the order of the records and which will be used in calculating the previous value. I am unsure though how you got Pear as the previous product in row 1 when there isn't anything before that.

     

    Previous Product = 
    MAXX (
        FILTER (
            'Table',
            'Table'[UniqueID] = EARLIER ( 'Table'[UniqueID] )
                && 'Table'[Index]
                    = EARLIER ( 'Table'[Index] ) - 1
        ),
        [Product]
    )
    

     

     

    Please see the attached pbix.