Forum Discussion

Sandeep13's avatar
Sandeep13
Icon for Helper III rankHelper III
1 year ago

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

  • 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
    )

    • Sandeep13's avatar
      Sandeep13
      Icon for Helper III rankHelper III

      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



      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

       



  • 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
    

     

    • Sandeep13's avatar
      Sandeep13
      Icon for Helper III rankHelper III

      Hi danextian 

       

      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.

       

       

  • Anonymous's avatar
    Anonymous
    Not applicable

    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

    • Anonymous's avatar
      Anonymous
      Not applicable

      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

      • Anonymous's avatar
        Anonymous
        Not applicable

        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

  • AntrikshSharma's avatar
    AntrikshSharma
    Icon for Community Champion rankCommunity Champion

    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.

     

     

    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