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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

Difference between two rows of same column for same ID using DAX

Hello Everyone,

I have a table as below, where I need to calculate "ValueDiff". 

01Abhi_0-1671112396258.png

There is no date for Initial situation, but date is availble for New Situation. However, the date for new situation emains same for all id as, it depends when the data was loaded.Now I need to find teh difference between New Situation and Initial situation for same ID and produce results when there is difference.

Could you please help me out here. I am learning DAX and not able to go to solution as date is same for all ID's.

1 ACCEPTED SOLUTION
Barthel
Solution Sage
Solution Sage

@Anonymous 

Could you show me the table in Power BI? Maybe a reference is missing? If I add the calculated column based on your sample data it works.

Barthel_0-1671117231304.png

View solution in original post

3 REPLIES 3
Barthel
Solution Sage
Solution Sage

@Anonymous 

Could you show me the table in Power BI? Maybe a reference is missing? If I add the calculated column based on your sample data it works.

Barthel_0-1671117231304.png

Barthel
Solution Sage
Solution Sage

You could use this DAX as a calculated column:

Value Diff = 
VAR _initial =
    CALCULATE ( 
        SUM ( 'Table'[Value] ),
        ALLEXCEPT ( 'Table', 'Table'[ID] ),
        'Table'[Situation] = "Initial"
    )
VAR _new =
    CALCULATE ( 
        SUM ( 'Table'[Value] ),
        ALLEXCEPT ( 'Table', 'Table'[ID] ),
        'Table'[Situation] = "New"
    )
VAR _result =
    IF ( 'Table'[Situation] = "New", _new - _initial )
RETURN
    _result 

You want to remove all context except ID. Hence ALLEXCEPT. For each variable, you specifically add the Situation as a filter. The result is the difference between the two, but you only show this for the lines where the Situation is equal to 'New'.

Anonymous
Not applicable

Hello Barthel,

Thank you for your prompt repsonse. I tried the above code, however the result is not as expected.

I have the results as below:

01Abhi_0-1671116217363.png

For the id's that have same Inital and New values, it returns the value as New. For cases where there is a difference, it is returning the value as Initial.

 

 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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