Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Sandeep13
Helper III
Helper III

Subtract 2nd row with first row in table view

 Hi Team

I want to subtract 2nd row with first row in table view. I have table name 

Sort_Client_MIS with 2 columns HIT_FROM and  ECV_VISIT  and want to create new column Visit_cnt

Below is sample data for your reference and my output expection. 
 
Note: this is in example with formula I am using in excel
    =IF([@[HIT_FROM]]=B1|C2-C1|[@[ECV_VISIT]])
 
you can see I am subtrating ECV_VISIT column value 2nd row with 1st row from second row.
my first row will be 1770 but second row will 3629-1770 =1859 and so on 
 
HIT_FROMECV_VISITVisit expected output
ABCD_EM17701770
ABCD_EM36291859    how 1859 comes (3629-1170)
ABCD_EM57022073
ABCD_EM77612059
ABCD_EM106752914
ABCD_EM139613286
ABCD_EM170713110
ABCD_EM209273856
ABCD_EM238692942
ABCD_EM272563387
ABCD_EM297902534
ABCD_EM330013211
ABCD_EM349141913
ABCD_EM369212007
ABCD_EM390452124
ABCD_EM390450 this is bcoz of same  vlues subtraction
ADMINFM 51365136  here as new hit form ADMINFM comes so it start with 5136
ADMINFM  75972461    (7597-5136 =2461)
ADMINFM 139056308

 

@Expert @microsa @BI 

8 REPLIES 8
AntrikshSharma
Super User
Super User

@Sandeep13 First make sure you have an Index column that enforces uniqueness of rows otherwise rows might appear in different order than the source and DAX functions will only rely on = and >< operations.

 

AntrikshSharma_0-1746700041779.png

 

Load the Table and create the calculated column with the following DAX:

Result = 
VAR PreviousVisit = 
    SELECTCOLUMNS ( 
        OFFSET ( 
            -1, 
            ALL ( Sort_Client_MIS ), 
            ORDERBY ( Sort_Client_MIS[Index], ASC ),
            PARTITIONBY ( Sort_Client_MIS[HIT_FROM] ),
            MATCHBY ( Sort_Client_MIS[Index] )
        ),
        Sort_Client_MIS[ECV_VISIT]
    )
VAR CurrentVisit = Sort_Client_MIS[ECV_VISIT]
VAR Result = 
    CurrentVisit - PreviousVisit
RETURN
    Result

 

AntrikshSharma_1-1746700248968.png

 

v-vpabbu
Community Support
Community Support

Hi @Sandeep13,

 

May I ask if you have gotten this issue resolved?

If it is solved, please mark the helpful reply or share your solution and accept it as solution, it will be helpful for other members of the community who have similar problems as yours to solve it faster.


Regards,
Vinay Pabbu

Hi @Sandeep13,

 

May I ask if you have gotten this issue resolved?

If it is solved, please mark the helpful reply or share your solution and accept it as solution, it will be helpful for other members of the community who have similar problems as yours to solve it faster.


Regards,
Vinay Pabbu

Hi @Sandeep13,

 

May I ask if you have gotten this issue resolved?

If it is solved, please mark the helpful reply or share your solution and accept it as solution, it will be helpful for other members of the community who have similar problems as yours to solve it faster.


Regards,
Vinay Pabbu

danextian
Super User
Super User

Hi @Sandeep13 

 

Try this:

Difference =
VAR _hit_from = 'datatable'[HIT_FROM] -- Stores the current row's HIT_FROM value
VAR _ecv_visit = 'datatable'[ECV_VISIT] -- Stores the current row's ECV_VISIT value
VAR _prevvalue =
    MAXX (
        FILTER (
            ALL ( 'datatable' ),
            -- Ignores existing filters to scan the entire table
            'datatable'[HIT_FROM] = _hit_from -- Keeps only rows with the same HIT_FROM
                && 'datatable'[ECV_VISIT] < _ecv_visit -- Finds rows with an earlier ECV_VISIT
        ),
        [ECV_VISIT] -- Retrieves the maximum (latest) previous ECV_VISIT
    )
RETURN
    'datatable'[ECV_VISIT] - _prevvalue
-- Calculates the difference from the previous ECV_VISIT

danextian_0-1742389018458.png

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Hi @danextian 

Sandeep13_0-1742393279676.png

 

One correction: If you see ABCD_EM 39045, it is showing difference 2124, but ideally, it should show 0. This is because the subtraction should be 39045 (15th Mar) - 39045 (16th Mar), which equals 0.

 

 

bhanu_gautam
Super User
Super User

@Sandeep13 Select your table Sort_Client_MIS.
Create a new column by clicking on "New Column" in the "Modeling" tab.
Use the following DAX formula to create the Visit_cnt column:

DAX
Visit_cnt =
VAR CurrentRow = 'Sort_Client_MIS'[ECV_VISIT]
VAR PreviousRow =
CALCULATE(
MAX('Sort_Client_MIS'[ECV_VISIT]),
FILTER(
'Sort_Client_MIS',
'Sort_Client_MIS'[HIT_FROM] = EARLIER('Sort_Client_MIS'[HIT_FROM]) &&
'Sort_Client_MIS'[ECV_VISIT] < EARLIER('Sort_Client_MIS'[ECV_VISIT])
)
)
RETURN
IF(
ISBLANK(PreviousRow),
CurrentRow,
CurrentRow - PreviousRow
)




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Thsnks for your reply

I am using your script for sample data but still getting wrong output

 

Not HIT_From is sorted in asc order

Sandeep13_0-1742386883751.png

DateHIT_FROMECV_VISITVisit
3/1/2025ABCD_EM17701770
3/2/2025ABCD_EM36291859
3/3/2025ABCD_EM57022073
3/4/2025ABCD_EM77612059
3/5/2025ABCD_EM106752914
3/6/2025ABCD_EM139613286
3/7/2025ABCD_EM170713110
3/8/2025ABCD_EM209273856
3/9/2025ABCD_EM238692942
3/10/2025ABCD_EM272563387
3/11/2025ABCD_EM297902534
3/12/2025ABCD_EM330013211
3/13/2025ABCD_EM349141913
3/14/2025ABCD_EM369212007
3/15/2025ABCD_EM390452124
3/16/2025ABCD_EM390450
3/1/2025ADMINFM51365136
3/2/2025ADMINFM75972461

excel formula

Sandeep13_1-1742387017172.png

 



Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.