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
Andrey28
Regular Visitor

% Grand total for Top N

Hello,

 

I know that there might be already an answer to my question, but I struggle to find an easy solution.

 

I have a data set with thousands of lines for which I want to do 2 tables. First is backlog by Customer and Service Type, second with Top N expenses by Category. What I struggle to calculate is % of grand total for these Top N expenses vs total backlog. Power BI can easily calculate % of grand total in a table for Top N expenses, but I need % grand total vs whole backlog and need these values to change if I change N in Top N. I assume there shall be some kind of DAX logic. When I add a formula for backlog = sum (expense by category) and divide spend in each category by this DAX expression, it obviously returns me 1 in each case.

 

See example below. Pretty easy to do in Excel....

 

Example.PNG

 

 

 

 

Thanks in advance.

Andrey 

1 ACCEPTED SOLUTION

Hi,

 

See below:

 'Table'[___AggasPrc] = [___Agg] / CALCULATE(SUM('Table'[Cost]), ALLSELECTED('Table'))
 'Table'[___Rank] = RANKX(SUMMARIZE(ALLSELECTED('Table'),'Table'[Category]), CALCULATE(SUM([Cost])), ,DESC,Dense)
 'Table'[___TopNCost] = IF([___Rank] <= 2, SUM('Table'[Cost]),BLANK())

 'Table'[___Agg] = 
IF(ISFILTERED('Table'[Category]),[___TopNCost],
SUMX(VALUES('Table'[Category]),[___TopNCost]))

As seen here, last column:

topn.png

Link to file here

 

Pls mark as a solution if so. Thumbs up for the effort are appreciated.

 

Kind regards,

 

Steve. 

 

 

View solution in original post

4 REPLIES 4
stevedep
Memorable Member
Memorable Member

Sure. Here is an example I was referring to. I added additional customer "C". So I am looking at Top 3 by expense Category in this case - % of grand total for all expenses (backlog). Let me know if you need data in another format.

 

Thanks in advance.

 

CustomerCategoryYearTypeCost
ATables2020Planned Spend20
AChairs2020Planned Spend30
AForks2020Planned Spend40
AKnives2020Planned Spend10
ANapkins2020Planned Spend20
ACups2020Planned Spend0
ATables2020Extra Spend2
AChairs2020Extra Spend7
AForks2020Extra Spend6
AKnives2020Extra Spend7
ANapkins2020Extra Spend4
ACups2020Extra Spend1
BTables2020Planned Spend10
BChairs2020Planned Spend15
BForks2020Planned Spend20
BKnives2020Planned Spend5
BNapkins2020Planned Spend10
BCups2020Planned Spend0
BTables2020Extra Spend1
BChairs2020Extra Spend3.5
BForks2020Extra Spend3
BKnives2020Extra Spend3.5
BNapkins2020Extra Spend2
BCups2020Extra Spend0.5
ATables2021Planned Spend19
AChairs2021Planned Spend28.5
AForks2021Planned Spend38
AKnives2021Planned Spend9.5
ANapkins2021Planned Spend19
ACups2021Planned Spend0
ATables2021Extra Spend1.9
AChairs2021Extra Spend6.65
AForks2021Extra Spend5.7
AKnives2021Extra Spend6.65
ANapkins2021Extra Spend3.8
ACups2021Extra Spend0.95
BTables2021Planned Spend9.5
BChairs2021Planned Spend14.25
BForks2021Planned Spend19
BKnives2021Planned Spend4.75
BNapkins2021Planned Spend9.5
BCups2021Planned Spend0
BTables2021Extra Spend0.95
BChairs2021Extra Spend3.325
BForks2021Extra Spend2.85
BKnives2021Extra Spend3.325
BNapkins2021Extra Spend1.9
BCups2021Extra Spend0.475
CTables2020Planned Spend20
CChairs2020Planned Spend30
CForks2020Planned Spend40
CKnives2020Planned Spend22
CNapkins2020Planned Spend20
CCups2020Planned Spend40
CTables2020Extra Spend2
CChairs2020Extra Spend7
CForks2020Extra Spend0
CKnives2020Extra Spend7
CNapkins2020Extra Spend3
CCups2020Extra Spend1
CTables2021Planned Spend19
CChairs2021Planned Spend28.5
CForks2021Planned Spend38
CKnives2021Planned Spend9.5
CNapkins2021Planned Spend11
CCups2021Planned Spend38
CTables2021Extra Spend1.9
CChairs2021Extra Spend6.65
CForks2021Extra Spend8
CKnives2021Extra Spend6.65
CNapkins2021Extra Spend9
CCups2021Extra Spend0.95

Hi,

 

See below:

 'Table'[___AggasPrc] = [___Agg] / CALCULATE(SUM('Table'[Cost]), ALLSELECTED('Table'))
 'Table'[___Rank] = RANKX(SUMMARIZE(ALLSELECTED('Table'),'Table'[Category]), CALCULATE(SUM([Cost])), ,DESC,Dense)
 'Table'[___TopNCost] = IF([___Rank] <= 2, SUM('Table'[Cost]),BLANK())

 'Table'[___Agg] = 
IF(ISFILTERED('Table'[Category]),[___TopNCost],
SUMX(VALUES('Table'[Category]),[___TopNCost]))

As seen here, last column:

topn.png

Link to file here

 

Pls mark as a solution if so. Thumbs up for the effort are appreciated.

 

Kind regards,

 

Steve. 

 

 

Thanks for the code. This solution works. I am testing it now using an example with the real data.

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

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

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.