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
KumarGS
New Member

Compare values in same column - Direct query Power BI

Hi,

I have a requirement where I need to compare values from same column.

For example, assume below table

KumarGS_0-1669216301505.png

Here, I need to add a new column and compare the 'Value' column data with it's previous row value. If the current value is greater than the previous, then it should store 1 and if the current value is lesser than the previous, then it should store 0. (as below)

KumarGS_2-1669216579588.png

Please let me know how this can ben achieved.

 

Thanks!

 

 

2 REPLIES 2
v-stephen-msft
Community Support
Community Support

Hi @KumarGS ,

 

You need to add the index column to Power Query first(Adding index columns is allowed).

If you also want to create group index with Power Query, please kindly refer to

How to create group index with Power Query or R

vstephenmsft_0-1669279710151.png

vstephenmsft_1-1669279714722.png

Then go back to Power BI Desktop, create a measure to get the previous value.

Pervious value = CALCULATE(SUM('Table'[Value]),FILTER(ALLSELECTED('Table'),[Index]=MAX('Table'[Index])-1))

vstephenmsft_3-1669279812466.png

 

Now you can get your expected result by creating another measure.

New value = IF(ISBLANK([Pervious value]),BLANK(),IF([Pervious value]>SUM('Table'[Value]),0,1))

vstephenmsft_4-1669279963724.png

 

Also, you can get previous valu in Power Query, please kindly refer to

Value from previous row – Power Query, M language

 

 

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

Hi Stephen,

 

Thanks for your response. I tried to add an Index column at first and it was not getting supported in direct query mode. I am getting the below error and couldn't proceed further. Please advise.

KumarGS_0-1669295679159.png

Thanks!

Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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