Forum Discussion
Need help calculation difference between two % of CT Columns
Hi,
I have been struggling with calculating the difference between two values represented as % of column totals to find the % change that works dynamically. Hopefully through these mock tables the issue I'm having will be clear. My base data is drug class and drug, with the quantity of drugs filled at each year being the metrics I need to work with:
| Drug Class | Drug | Year 1 Quantity | Year 2 Quantity |
| Class A | Drug 1 | 700 | 600 |
| Class A | Drug 2 | 500 | 700 |
| Class A | Drug 3 | 700 | 700 |
| Class A | Drug 4 | 300 | 100 |
| Class B | Drug 5 | 1000 | 900 |
| Class C | Drug 6 | 1000 | 1100 |
Using show value as % of column total, I get exactly what I want to show which re-calculates as the % of the class on drill down:
| Drug Class | % CT Year 1 | % CT Year 2 |
| Class A | 52.38% | 51.22% |
| Class B | 23.81% | 21.95% |
| Class C | 23.81% | 26.83% |
| Total | 100% | 100% |
| Drug Class | % CT Year 1 | % CT Year 2 |
| Class A | 100% | 100% |
| Drug 1 | 31.82% | 28.57% |
| Drug 2 | 22.73% | 33.33% |
| Drug 3 | 31.82% | 33.33% |
| Drug 4 | 13.64% | 4.76% |
| Total | 100% | 100% |
What I am having difficultly trying to calculate is the difference between these in a new column that works dynamically. The % change between the classes and the % change of the drugs within the class. I'm not concerned with what the "total" column would show in this at all but this is how I'd want the data to look for the drugs and classes:
| Drug Class | % CT Year 1 | % CT Year 2 | New Column |
| Class A | 52.38% | 51.22% | -1.16% |
| Class B | 23.81% | 21.95% | -1.86% |
| Class C | 23.81% | 26.83% | 3.02% |
| Total | 100% | 100% | 0% |
| Drug Class | % CT Year 1 | % CT Year 2 | New Column |
| Class A | 100% | 100% | 0% |
| Drug 1 | 31.82% | 28.57% | -3.25% |
| Drug 2 | 22.73% | 33.33% | 10.60% |
| Drug 3 | 31.82% | 33.33% | 1.51% |
| Drug 4 | 13.64% | 4.76% | -8.88% |
| Total | 100% | 100% | 0.00% |
Any help with this would be greatly appreciated. I've tried to calculate the %CT funtion myself using DAX that works dynamically so I could use it in the new column calc but thats been fruitless so far. Any proposed solution would be greatly appreciated.
Thanks!
You need to create measures to make this work dynamically when you drill up or down.
ie
measure Year 1 Quantity total = calculate(sum(table[Year 1 Quantity ]),ALLSELECTED())
measure Year 2 Quantity total = calculate(sum(table[Year 2 Quantity ]),ALLSELECTED())
Year 1 % = divide(sum(table[Year 1 Quantity]),[measure Year 1 Quantity total])Year 2 % = divide(sum(table[Year 2 Quantity]),[measure Year 2 Quantity total])% diff = [Year 2 %]-[Year 1 %]
3 Replies
- omitche2Frequent Visitor
You need to create measures to make this work dynamically when you drill up or down.
ie
measure Year 1 Quantity total = calculate(sum(table[Year 1 Quantity ]),ALLSELECTED())
measure Year 2 Quantity total = calculate(sum(table[Year 2 Quantity ]),ALLSELECTED())
Year 1 % = divide(sum(table[Year 1 Quantity]),[measure Year 1 Quantity total])Year 2 % = divide(sum(table[Year 2 Quantity]),[measure Year 2 Quantity total])% diff = [Year 2 %]-[Year 1 %] - amitchandakSuper User
You can create this year and last year total like anyone these
Year = CALCULATE(SUM(table[column]),DATESYTD('Date'[Date Filer])) Last Year = CALCULATE(SUM(table[column]),DATESYTD(dateadd('Date'[Date Filer],-12,MONTH))) Year Sales = CALCULATE(TOTALYTD(sum(Sales[Sales Amount]),('Date'[Date Filer]))) Last Year Sales = CALCULATE(TOTALYTD(sum(Sales[Sales Amount]),dateadd('Date'[Date Filer].-12,month))) Year = CALCULATE(SUM(table[column]),DATESYTD(endofmonth(dateadd('Date'[Date Filer],-1,MONTH)))) Last Year = CALCULATE(SUM(table[column]),DATESYTD(endofmonth(dateadd('Date'[Date Filer],-13,MONTH)))) Year Sales = CALCULATE(TOTALYTD(sum(Sales[Sales Amount]),endofmonth(dateadd('Date'[Date Filer].-1,month)))) Last Year Sales = CALCULATE(TOTALYTD(sum(Sales[Sales Amount]),endofmonth(dateadd('Date'[Date Filer].-13,month))))Add additional filter of all(Table). That should give you GT.
Then use the divide function. to get % of the total and the calculate change %.
Another way. Check Quick formula. There are options to create some of these things quickly. Right-click on a field or measure.
Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution. In case it does not help, please provide additional information and mark me with @
Thanks.
My Recent Blog - https://community.powerbi.com/t5/Community-Blog/Comparing-Data-Across-Date-Ranges/ba-p/823601- NJ23New Member
amitchandak I don't think this works on the drill down. The issue I am having is I need % of CT, so the denominator would be the sum of the drug class at the drug level and would be the sum of all table at the class level.