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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Difference between Rows two rows

Hi everyone!

 

My data is structured in Excel (displayed below). I am having translating the Excel formula in Power Bi however, and I need your help please! 

 

In Excel I sort the data by Key and by Date. In column D I then use this formula : IF(A2=A1,C2-C1,"")

 

Could you please help me translate this to PowerBi language please? 

 

Thank you

 

 

 

   A        B    C     D
Key DateValueExcel Formula
101/01/2019            500 
101/02/2019            800300
201/01/2019            200 
201/02/2019            400200
201/03/2019            900500
201/04/2019         1,700800
301/01/2019            600 
401/01/2019            800 
401/02/2019         1,000200
501/01/2019               50 
501/02/2019               8030

 

2 ACCEPTED SOLUTIONS
v-lid-msft
Community Support
Community Support

Hi @Anonymous ,

 

We can create a calculate column using following DAX to meet your requirement, but if you have two rows contain same key and the same date, it may not work fine.

 

Difference = 
VAR k = [Key]
VAR d = [Date]
VAR f =
    FILTER ( 'Table', AND ( 'Table'[Key] = k, 'Table'[Date] < d ) )
VAR t =
    TOPN ( 1, f, [Date] )
RETURN
    IF ( ISBLANK ( COUNTROWS ( t ) ), BLANK (), [Value] - MAXX ( f, [Value] ) )

Difference-between-Rows-two-rows-1.png

 

BTW, pbix as attached.

 

Best regards,

 

Community Support Team _ DongLi
If this post helps, then please consider Accept it as the solution to help the other members find it more

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Ashish_Mathur
Super User
Super User

Hi,

This calculated column formula works

=if(ISBLANK(CALCULATE(MAX(Data[Date]),FILTER(Data,Data[Key]=EARLIER(Data[Key])&&Data[Date]<EARLIER(Data[Date])))),BLANK(),Data[Value]-LOOKUPVALUE(Data[Value],Data[Date],CALCULATE(MAX(Data[Date]),FILTER(Data,Data[Key]=EARLIER(Data[Key])&&Data[Date]<EARLIER(Data[Date]))),Data[Key],Data[Key]))

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

This calculated column formula works

=if(ISBLANK(CALCULATE(MAX(Data[Date]),FILTER(Data,Data[Key]=EARLIER(Data[Key])&&Data[Date]<EARLIER(Data[Date])))),BLANK(),Data[Value]-LOOKUPVALUE(Data[Value],Data[Date],CALCULATE(MAX(Data[Date]),FILTER(Data,Data[Key]=EARLIER(Data[Key])&&Data[Date]<EARLIER(Data[Date]))),Data[Key],Data[Key]))

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Many thanks to you both!

 

Both solutions worked great - thank you

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-lid-msft
Community Support
Community Support

Hi @Anonymous ,

 

We can create a calculate column using following DAX to meet your requirement, but if you have two rows contain same key and the same date, it may not work fine.

 

Difference = 
VAR k = [Key]
VAR d = [Date]
VAR f =
    FILTER ( 'Table', AND ( 'Table'[Key] = k, 'Table'[Date] < d ) )
VAR t =
    TOPN ( 1, f, [Date] )
RETURN
    IF ( ISBLANK ( COUNTROWS ( t ) ), BLANK (), [Value] - MAXX ( f, [Value] ) )

Difference-between-Rows-two-rows-1.png

 

BTW, pbix as attached.

 

Best regards,

 

Community Support Team _ DongLi
If this post helps, then please consider Accept it as the solution to help the other members find it more

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.