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
praveen_k
Helper I
Helper I

Difference between Columns dynamically

Temp.jpg

 

I have a requirement to calculate the percentage difference between columns 1,2,3,..., etc dynamically based on user selection on the report. These columns represent months and increase regularly. 

 

I created a measure to calculate between two columns :

Diff = DIVIDE(SUM(Query1[3])-SUM(Query1[2]),SUM(Query1[2]))

 

If have a slicer on the report with column names and want to calculate the percentage difference based on user selection, how can we do this ? Is this possible in Power BI ? 

 

Example - 

Month slicer - 1,2,3

 

when the user selects option 2, the difference that need to be calculated is 

Diff = DIVIDE(SUM(Query1[3])-SUM(Query1[2]),SUM(Query1[2]))

 

when the option 1 is selected, the difference that need to be calculated is - 

Diff = DIVIDE(SUM(Query1[3])-SUM(Query1[1]),SUM(Query1[1]))

 

When a extra column 4 is added in the month end; the measure should be able to pick that as the latest and compare with other columns dynamically.

 

 

2 ACCEPTED SOLUTIONS

OK, I actually tested this one. Had my ALL clause in the wrong spot:

 

diff = DIVIDE(CALCULATE(SUM([Total]), FILTER(ALL(ReportMo),ReportMo[ReportMo] = MAXX(ALL(ReportMo),[ReportMo])))-SUM([Total]),SUM([Total]))

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

@praveen_k and @Sean - Here is the revised formula that accounts for Level, basically replace the ALL clauses with ALLEXCEPT and get ALL of the rows with the exception of those filtered by Level.

 

diff2 = DIVIDE(CALCULATE(SUM([Total]), FILTER(ALLEXCEPT(ReportMo,ReportMo[Level]),ReportMo[ReportMo] = MAXX(ALLEXCEPT(ReportMo,ReportMo[Level]),[ReportMo])))-SUM([Total]),SUM([Total]))

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

17 REPLIES 17
Greg_Deckler
Super User
Super User

I'm thinking that you are going to have to unpivot your "month" columns so that you have a column that has month in it. Then, you could FILTER in your formulas based upon the MAX of that column. @Sean and I just went through a similar thing with someone here:

 

http://community.powerbi.com/t5/Desktop/Filter-on-the-measure/m-p/31907

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Temp.jpg

 

I removed the pivot table and here is how the original table looks like. 

I am not sure how to proceed after this is my first trial in BI. 

OK, I am thinking something like putting a slicer on the report for ReportMo and then having a measure like:

 

Diff = DIVIDE(CALCULATE(SUM([Total]),ALL(Table),MAX([ReportMo]))-SUM([Total]),SUM([Total]))

If I did that right, what should happen is that the measure takes the SUM of all rows with a MAX reportmo (regardless of what is selected), you might need an ALLEXCEPT([PersonID]) instead of ALL and then the rest of the calculations are filtered by your slicer.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thanks for the code smoupre

 

diff = DIVIDE(CALCULATE(SUM([Total]), ALL(Query2), MAX([ReportMo]))-SUM([Total]),SUM([Total]))

 

This measure came up with an error - 

A function 'MAX' has been used in a True/False expression that is used as a table filter expression. This is not allowed.

diff = DIVIDE(CALCULATE(SUM([Total]), ALL(Query2), [ReportMo] = MAX([ReportMo]))-SUM([Total]),SUM([Total]))

try that derivative, I'll go test it.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

This is what I get now. Am I missing something or doing it wrong ?

 

Temp.jpg

 

 

Temp.jpg

OK, this should get you there without errors. This is why I shouldn't try to write DAX without testing it. Needed the FILTER clause.

 

diff = DIVIDE(CALCULATE(SUM([Total]), ALL(ReportMo), FILTER(ReportMo,ReportMo[ReportMo] = MAX([ReportMo])))-SUM([Total]),SUM([Total]))

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

 

This give me a value of 0 when ReportMo 3 is compared with ReportMo 2, which is incorrect.

 

The measure used is - 

 

diff2 = DIVIDE(CALCULATE(SUM([Total]), ALL(Query2[ReportMo]), FILTER(Query2,Query2[ReportMo]= MAX([ReportMo])))-SUM([Total]),SUM([Total]))

Temp.jpg

OK, I actually tested this one. Had my ALL clause in the wrong spot:

 

diff = DIVIDE(CALCULATE(SUM([Total]), FILTER(ALL(ReportMo),ReportMo[ReportMo] = MAXX(ALL(ReportMo),[ReportMo])))-SUM([Total]),SUM([Total]))

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thanks a lot! This works perfect. My next step now is how can we extend the filter to another column? 

 

diff2 = DIVIDE(CALCULATE(SUM([Total]), FILTER(ALL(Query2),Query2[ReportMo] = MAXX(ALL(Query2),[ReportMo])))-SUM([Total]),SUM([Total]))

 

 

Temp.jpg.png

 

Temp.jpg.png

 

Is this doable?

Sorry that was a bit painful. But we got there in the end. Not sure what you are referring to for your next question, can you explain it a little more?


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

All credits to you!

 

I have another column value that i would like to filter and caluclate the percentage difference. 

 

Ex- 

I have another column - Level. 

When I select Report Mo - 2 and Level - High; I should see the percentage difference between months 3 and 2 where Level = High.

 

I am able to see the some value being caluclated, but it is obviously wrong. Here is what i see on report - 

 

Temp.jpg.png

 

 

 And the table looks like this - 

 

Temp.jpg.png

 

 

 

@praveen_k and @Sean - Here is the revised formula that accounts for Level, basically replace the ALL clauses with ALLEXCEPT and get ALL of the rows with the exception of those filtered by Level.

 

diff2 = DIVIDE(CALCULATE(SUM([Total]), FILTER(ALLEXCEPT(ReportMo,ReportMo[Level]),ReportMo[ReportMo] = MAXX(ALLEXCEPT(ReportMo,ReportMo[Level]),[ReportMo])))-SUM([Total]),SUM([Total]))

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thanks @Greg_Deckler!

No problem @praveen_k, glad we got it figured out!


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Sean
Community Champion
Community Champion

@praveen_k or @Greg_Deckler I followed along with this example but I can't figure out what diff2 represents?

What does it help you see in the data? Did I construct my data set properely? Thanks!

Difference Between Columns.png

Sean
Community Champion
Community Champion

@praveen_k You may be able to do this with a Parameter Table

 

http://www.daxpatterns.com/parameter-table/

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.