Forum Discussion

wdbirch19's avatar
wdbirch19
New Member
4 years ago
Solved

Difference between rows filtering by ID row

Hi, I'm trying to figure out a way of calculating the difference between two rows of data. Here's an example of what my data looks like:

 

BioreactorIDAcid1IndexIndex.1
1001
2012
3023
4034
1145
2156
3167
4178
1289
31910
221011
411112
321213
131314
221415
421516
431617
331718
231819
141920

 

I'm interested in the difference in consecutive rows of the 'Acid1' column. However, I also want to filter this calculation by the 'BioreactorID'. For example, taking only 'BioreactorID' == 1:

 

BioreactorIDAcid1IndexIndex.1Desired output
10010
11451
12891
1313141
1419201

'Desired output' is simply calculated by 'Acid1' - previous 'Acid1'

 

Overall, I want the resulting dataset to look like this:

 

BioreactorIDAcid1IndexIndex.1Desired output
10010
20120
30230
40340
11451
21561
31671
41781
12891
319100
2210111
4111120
3212131
1313141
2214150
4215161
4316171
3317181
2318191
1419201

 

where the 'Desired output' calculates the difference in 'Acid1' for each 'BioreactorID' independently. 

 

I have managed to do this by manually creating separate tables for each BioreactorID, but I'd prefer to have a scalable solution as the number of BioreactorID's won't always be the same. Also note that BioreactorID won't always be in order (1,2,3,4...).

 

Any help with this would be much appreciated, thanks!

 

  • You could create a calculated column like

    Diff =
    VAR currentBioReactor = 'Table'[BioReactor]
    VAR currentIndex = 'Table'[Index]
    VAR currentValue = 'Table'[Acid1]
    VAR prevValue =
        SELECTCOLUMNS (
            TOPN (
                1,
                FILTER (
                    ALL ( 'Table' ),
                    'Table'[BioReactor] = currentBioReactor
                        && 'Table'[Index] < currentIndex
                ),
                'Table'[Index]
            ),
            "@val", 'Table'[Acid1]
        )
    RETURN
        currentValue - prevValue

2 Replies

  • You could create a calculated column like

    Diff =
    VAR currentBioReactor = 'Table'[BioReactor]
    VAR currentIndex = 'Table'[Index]
    VAR currentValue = 'Table'[Acid1]
    VAR prevValue =
        SELECTCOLUMNS (
            TOPN (
                1,
                FILTER (
                    ALL ( 'Table' ),
                    'Table'[BioReactor] = currentBioReactor
                        && 'Table'[Index] < currentIndex
                ),
                'Table'[Index]
            ),
            "@val", 'Table'[Acid1]
        )
    RETURN
        currentValue - prevValue