Finding Share of Group Total for each element Power BI - powerbi

In Power Bi, I'd like a measure to get share of a group total for each element in that group. For example,
Player
Team
Goals
Rooney
MU
3
Ronaldo
MU
2
Rashford
MU
5
Crouch
SP
5
So for Ronaldo, it would be 20% as it's his goals / team total
Crouch would be 100% etc
How could I do this in DAX?

VAR playersResult = SUM(tbl[Goals])
VAR teamResult = CALCULATE(SUM(tbl[Goals]),ALL(tbl[Player])
RETURN
playersResult /teamResult

Please try this:
Efficiency =
VAR GoalScored =
SUM ( Club[Goals] )
VAR TeamScored =
SUMX (
ADDCOLUMNS (
SUMMARIZE ( Club, Club[Player], Club[Team] ),
"TeamScored", CALCULATE ( SUM ( Club[Goals] ), ALLEXCEPT ( Club, Club[Team] ) )
),
[TeamScored]
)
VAR Efficiency =
DIVIDE ( GoalScored, TeamScored )
RETURN
Efficiency
If we test it:

Related

Sum 5 lines of the max values in a column and get the % in power bi

I have a table with some contracts, like this:
Contract Num
Another header
Value
123456
Cleaning
123.541,00--
544311
Security
200.000,00--
254856
Cleaning
23.581,00--
048941
Security
80.000,00--
128546
Cleaning
300.500,00--
255311
Security
99.000,00--
130056
Cleaning
543.541,00--
859311
Security
10.000,00--
I want to get this in Power Bi:
The value of the 5 most expensive contracts
How much these 5 represents in % of the total sum.
I got the letter A in a card applying filters (N Superior), but I can't work with it in a measure. What expression can I use to get a number of line in a column? How can I solve this in a measure?
thanks
Hello Please test this DAX Code:
This gives you the TOP5 Total:
Top5_Total =
VAR TOP5Total =
SUMX ( TOPN ( 5, YourTable, [Value] ), [Value] )
RETURN
TOP5Total
And This gives you the ratio of Top5 To All Totals:
RatioOfTop5_To_Total =
VAR TOP5Total =
SUMX ( TOPN ( 5, YourTable, [Value] ), [Value] )
VAR Total =
CALCULATE ( SUM ( YourTable[Value] ), ALL ( YourTable ) )
VAR PercentOfTop5_To_Total =
DIVIDE ( 100 * TOP5Total, Total )
RETURN
PercentOfTop5_To_Total

Using DAX to Create a dynamic horizontal line for a bar or line chart in Power BI

Hello Lovely People of SO,
I hope you guys are having a great day!
I have the following dataset
REGION
ID
STATUS
DC
1
NEW
ED
2
NEW
FR
3
OLD
FR
4
NEW
GI
5
OLD
GI
6
OLD
GY
7
NEW
GY
8
OLD
GY
9
OLD
GY
10
OLD
GY
11
OLD
GY
12
NEW
RT
13
NEW
TX
14
NEW
TX
15
NEW
I will first want to know how to use DAX to calculate the percentage of ID with STATUS ="OLD" by REGION
Intuitivatly in Python I can group by REGION and then summarize the number of OLD and NEW STATUS but here in PBI DAX things are not somewhat stright forward yet for me since I am learning so thanks you for helping my out with that, my main goal is to create a bar chart that will show the percentage of OLD STATUS by REGION and add a horizontal line that will display the global average of percentages, this is my own data summary
and my desired viz would look like this:
but I have no clue was to how to do that in dax in power bi thank you so much if you guys can help me out If you can reference some code online to do this or if there is a special bulti-in function to calculate this horizontal line thanks a million I will be super attentive to all of your comments
and let me know if It works for you:
OldPercent_Measure =
VAR TblSummary =
ADDCOLUMNS (
SUMMARIZE ( YourTable, YourTable[REGION] ),
"STATUS OLD",
CALCULATE ( COUNTROWS ( YourTable ) + 0, YourTable[STATUS] = "OLD" ),
"TOTAL", CALCULATE ( COUNTROWS ( YourTable ) ),
"% OLD",
ROUND (
DIVIDE (
CALCULATE ( COUNTROWS ( YourTable ) + 0, YourTable[STATUS] = "OLD" ),
CALCULATE ( COUNTROWS ( YourTable ) )
),
2
)
)
VAR PercentOld =
SUMX ( TblSummary, [% OLD] )
VAR GlobalAverage =
AVERAGEX ( TblSummary, [% OLD] )
RETURN
PercentOld
Then create a column chart, put [REGION] column in X_axis, and put OldPercent_Measure in the Y_axis[Values field]. I hope It solves your problem.
To calculate Global Average, the same code above. The only difference is to replace PercentOld with GlobalAverage after "RETURN" statement. Like This:
......
......
RETURN
GlobalAverage
Extra Info:
You want to see the result of your summary table:
EVALUATE
VAR TblSummary =
ADDCOLUMNS (
SUMMARIZE ( YourTable, YourTable[REGION] ),
"STATUS OLD",
CALCULATE ( COUNTROWS ( YourTable ) + 0, YourTable[STATUS] = "OLD" ),
"TOTAL", CALCULATE ( COUNTROWS ( YourTable ) ),
"% OLD",
ROUND (
DIVIDE (
CALCULATE ( COUNTROWS ( YourTable ) + 0, YourTable[STATUS] = "OLD" ),
CALCULATE ( COUNTROWS ( YourTable ) )
),
2
)
)
VAR PercentOld =
SUMX ( TblSummary, [% OLD] )
RETURN
TblSummary
Resulting Screen:
To find out how to put average line:
You need to go analytics pane.
Here is a good link to do that:
https://learn.microsoft.com/en-us/power-bi/transform-model/desktop-analytics-pane
One way to get the average for OLD is to create a calculated column for %. Then use DAX to find the average of just the ones with OLD status, using:
CALCULATE(AVERAGE([% column]), FILTER(table name, STATUS = “OLD”))
The graph is a cummulative histogramm (the second left to right, top line). The line is a constant line at analytics. I used the same measure for value there. Have a nice day.
Rate =
VAR Regions = VALUES(tbl[REGION])
VAR sumOfRates=
SUMX(
Regions
,DIVIDE(
CALCULATE(COUNTROWS('tbl'),tbl[STATUS]="OLD")
,CALCULATE(COUNTROWS('tbl'))
)
)
RETURN
DIVIDE(sumOfRates,COUNTROWS(Regions))+0

Calculation groups for currency conversion (multiple currencies to multiple currencies)

I have tried to make a calculation group that does my currency conversion for me for all my measures that has a "LC" in its naming. It goes well in most cases BUT when I have a measure with "per sqm" it works when I divide the measure example per country but the total is wrong..
Look at the example - everything is good except for the total for "Average Total Costs per sqm - LC", that should be 562.714 and not 477.788.
enter image description here
VAR MeasureName =
SELECTEDMEASURENAME ()
VAR SkipConversion =
NOT ISCROSSFILTERED ( 'Exchange Rates' )
|| ( SEARCH ( "LC", MeasureName, 1, -1 ) < 0 )
RETURN
IF ( SkipConversion, SELECTEDMEASURE (), VAR AggregatedTotalCost =
ADDCOLUMNS (
SUMMARIZE (
'Fact Tenant Improvement',
'Exchange Rates'[Currency_From],
'Country'[KEY_Countrycode]
),
"#SalesAmountInCurrency", SELECTEDMEASURE (),
"#Rate", CALCULATE (
SELECTEDVALUE ( 'Fact Exchange Rates'[Rate])
)
)
VAR Result =
sumX (
AggregatedTotalCost,
[#SalesAmountInCurrency] * [#Rate]
)
RETURN
Result)
Any ideas on how to handle this?

Measure Including Year Filter

i have done a Measure to get the Revenue from TOP N Products.
N depends on the selected Value from my TOP-N Table (It's containing just the numbers from 1 to 10).
In the same Measure, I also calculate how much was the revenue of all Products how were not in the selected Top N.
The Code for this is:
Top N Produktbez Sum DB3 =
VAR TopNSelected =
SELECTEDVALUE ( 'TOP N-Filter'[TOP N] )
VAR TopNProdbezTable =
TOPN ( TopNSelected, ALLSELECTED ( 'Pseudo Produkbez Table' ), [DB3] )
VAR TopNProdbez =
CALCULATE ( [DB3], KEEPFILTERS ( TopNProdbezTable ) )
VAR OtherSales =
CALCULATE ( [DB3], ALLSELECTED ( 'Pseudo Produkbez Table' ) )
- CALCULATE ( [DB3], TopNProdbezTable )
VAR CurrentProd =
SELECTEDVALUE ( 'Pseudo Produkbez Table'[Produktbezeichnung] )
RETURN
IF ( CurrentProd <> "Others", TopNProdbez, OtherSales )
Now I want to add Year as Filter.
I'm using the Year coming from the Date Hierarchy.
Unfortunately, I can't provide a sample yet.
If a sample is need, I will try to create own.

How to calculate the average of multiple categories in Power-BI DAX?

I have a table with the following columns:
Industry table
Industry_ID Score
1 2
1 3
2 2
2 4
3 0
4 2
I need to calculate the average of each industry and then the average of those averages.
Like avg of scores of
1=(2+3)/2 =>2.5
2=(2+4)/2 =>3
3=0/1 => 0
4=2/1 => 2
Then average of these averages, i.e (2.5+3+0+2)/4 => 1.85
The tables are in direct query so please consider that. Any help is appreciated. Thank you
For creating the average of distinct values, create a calculated column as:
Average =
var no_ID = 'Table'[Industry_ID]
Return
AVERAGEX(
FILTER(ALL('Table'), 'Table'[Industry_ID] = no_ID),
'Table'[Score]
)
This will give you a column having average of distinct Industry_ID.
For creating an average of averages, create a measure as:
Measure = AVERAGEX(SUMMARIZE('Table', 'Table'[Industry_ID], 'Table'[Average]), 'Table'[Average])
Final Output-
Here are 2 ways to achieve that:
Just switch between Individual and Overall Average variables in the RETURN part, also store this code CALCULATE ( COUNTROWS ( Industry ) ) in a separate measure so that it can be re-used in various places without making the code verbose
Industry Average =
VAR AllIndustryAverages =
AVERAGEX (
ALL ( Industry[IndustryID] ),
DIVIDE ( [Total Score], CALCULATE ( COUNTROWS ( Industry ) ) )
)
VAR IndividualAverages =
AVERAGEX (
VALUES ( Industry[IndustryID] ),
DIVIDE ( [Total Score], CALCULATE ( COUNTROWS ( Industry ) ) )
)
RETURN
IndividualAverages
Industry Average 2 =
VAR VisibleIndustries =
VALUES ( Industry[IndustryID] )
VAR AllIndustryAverages =
ADDCOLUMNS (
ALL ( Industry[IndustryID] ),
"Average",
VAR CurrentIndustryTotalScore = [Total Score]
VAR IndustryCount =
CALCULATE ( COUNTROWS ( Industry ) )
RETURN
DIVIDE ( CurrentIndustryTotalScore, IndustryCount )
)
VAR IndividualAverages =
AVERAGEX (
FILTER ( AllIndustryAverages, Industry[IndustryID] IN VisibleIndustries ),
[Average]
)
VAR OverallAverage =
AVERAGEX ( AllIndustryAverages, [Average] )
RETURN
IndividualAverages