Sum of max grouped by column - powerbi

What I want to achieve is to create table where I can display a list of items with max values, but grouped with another dimension.
Here is the example:
[
Have tried this, but the result is wrong
sum_of_max = SUMX(VALUES(T1[dimension1]),CALCULATE(MAX(T1[Value])))

Use this measure instead:
sum_of_max =
CALCULATE(
SUM(T1[value]),
T1[value] = MAX(T1[value])
)

Related

Power BI DAX How to add column to a calculated table that summarizes another

I Have a TestTable that summarizes a table Receipts on the Month column and adds a column that counts the number of times (occurence) that each month appears in the Receipts Table.
TestTable = SUMMARIZE(Receipts, Receipts[Month], "TotalReceiptsIssuedInThisMonth", SUM(Receipts[Receipts Issued]), "OccurenceOfMonth", COUNT(Receipts[Month]))
I want to add two columns to this TestTable which will tell me the following:
Sum the TotalReceiptsIssuedInThisMonth of the TestTable and show the
value in each row
For each Month (row), divide the, TotalReceiptsIssuedInThisMonth by the SumOfTotalReceiptsIssued
I know I can click "New Column" and use these formulas:
AvgPercentageReceiptsIssuedInThisMonth = TestTable[TotalReceiptsIssuedInThisMonth]/TestTable[TotalReceiptsIssued]
TotalReceiptsIssued = SUM(TestTable[TotalReceiptsIssuedInThisMonth])
However, I need to integrate those two columns directly into the original TestTable formula to make it all happen in one step for use as a variable in the original Receipts table (otherwise I end up with circular logic if I try using relationships).
I've tried the following:
TestTable = SUMMARIZE(PPTs, PPTs[Month], "TotalReceiptsIssuedInThisMonth", SUM(PPTs[PPTs Issued]), "OccurenceOfMonth", COUNT(PPTs[Month]), "TotalReceiptsIssued", SUM(TestTable[TotalReceiptsIssuedInThisMonth]), "AvgPercentageReceiptsIssuedInThisMonth", TestTable[TotalReceiptsIssuedInThisMonth]/TestTable[TotalReceiptsIssued])
but this returns an error saying "A single value for column 'TotalReceiptsIssuedInThisMonth' in table 'TestTable" cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result." and I've tried:
TestTable =
VAR first = SUMMARIZE(Receipts, Receipts[Month], "TotalReceiptsIssuedInThisMonth", SUM(Receipts[Receipts Issued]), "OccurenceOfMonth", COUNT(Receipts[Month]))
VAR second = SUM(TestTable[TotalReceiptsIssuedInThisMonth])
VAR third = first[TotalReceiptsIssuedInThisMonth]/second
RETURN
third
But this returns an error saying "The variable'first' cannot be used in current context because a base table is expected."
So my question is, how do I go about combining these three steps into one DAX formula?
I would do something like this. I prefer ADDCOLLUMN(SUMMARIZE()...), because it helps to avoid a miscontexting. As you need a var table, then you need the CALCULATE in ADDCOLUMNS, as it adds the row context.
VAR TestTable =
ADDCOLUMNS(
SUMMARIZE(
Receipts
,Receipts[Month]
)
,"TotalReceiptsIssuedInThisMonth",CALCULATE(SUM(Receipts[Receipts Issued]))
,"OccurenceOfMonth", CALCULATE(COUNT(Receipts[Month]))
,"TotalReceiptsIssued ",SUM(Receipts[Receipts Issued])
)
RETURN
ADDCOLUMNS(
TestTable
,"AvgPercentageReceiptsIssuedInThisMonth"
,DIVIDE([TotalReceiptsIssuedInThisMonth],[TotalReceiptsIssued])
)
Check out More regarding the use of DAX to create a table
https://www.advancelearnlinux.com/how_to_create-table-in-microsoft-power-bi-using-dax-function/

Power BI - DAX - table variable - use column for further computation

I am using DAX language in Power BI Desktop.
I have a tricky situation where I am trying to use the column name generated from a table variable.
Table 1: SourceTable
Table 2: ReferenceTable
I need to develop a calculated column in SourceTable called EmploymentStatus, based on the corresponding column in ReferenceTable. But I need only the EmploymentStatus value from ReferenceTable, for the maximum InternalID for a given EmployeeEmail.
For example, for the email xyz.gmail.com in SourceTable, I need the EmploymentStatus (calculated column) as 'Active' from ReferenceTable, since 'Active' has the maximum of the two available InternalID values (17, 15).
I tried the following code (Calculated Column in SourceTable):
EmploymentStatus_SourceTable_CalculatedColumn =
VAR tabl1 =
SUMMARIZE (
ReferenceTable,
ReferenceTable[EmployeeEmail],
"MaxInteralID", MAX ( ReferenceTable[InternalID] )
)
VAR tabl2 =
FILTER (
ReferenceTable,
ReferenceTable[InternalID] IN VALUES ( tabl1[MaxInteralID] )
)
VAR NewCol =
LOOKUPVALUE (
tabl2[EmploymentStatus],
tabl2[EmployeeEmail], SourceTable[EmployeeEmail]
)
RETURN
NewCol
I realize that I cannot use the column generated from the table variable.
For example, tabl1[MaxInteralID], tabl2[EmployeeStatus], tabl2[EmployeeEmail] - are all invalid.
Any idea on how to handle this? You can even provide me with a solution that does not use variables at all. Am okay with any solution.
Similar to here, you can find the maximal ID for each email and look up the status for that ID.
Table and column names shortened for readability:
CalcCol =
VAR Email = Source[Email]
VAR MaxID = CALCULATE ( MAX ( Ref[ID] ), Ref[Email] = Email )
RETURN
LOOKUPVALUE ( Ref[Status], Ref[Email], Email, Ref[ID], MaxID )

How to get TOPN in DAX ignoring duplicate values

Using filter to get Top 5 by Submitted.
However it gives me more than 5, due to duplicates.
How can I tweak or create a measure that would only give me top 5 disregarding duplicates.
I tried to use RANK function but also no success:
Rank = RANKX ( ALLSELECTED ( Policy[CodeDescription] ), CALCULATE ( SUM ( Policy[Submitted]) ) )
You can create a new column and add "Submitted" + RAND() and then rank it based on the new column. Considering you don't have a preference as to which column value gets priority.
Check out the below link for more options:
https://www.red-gate.com/simple-talk/sql/bi/cracking-dax-the-earlier-and-rankx-functions/
I was able to solve it by using below measure:
Top 5 Code filter =
VAR toprank = RANKX(ALLSELECTED(Policy[CodeDescription]), CALCULATE(SUM(Policy[Submitted]) + MAX(Policy[ControlNo]) * 0.0000001),,DESC,Dense)
RETURN
toprank
enter image description here
Below query helped to resolve this issue.
Top 5 Code filter =
VAR toprank = RANKX(ALLSELECTED(gict_riskregisterdetail), CALCULATE(SUM(gict_riskregisterdetail[PICost_RbM_Rnk]) + MAX(gict_riskregisterdetail[Number]) * 0.0000001),,DESC,Dense)
RETURN
toprank

Calculate MAX of a RELATEDTABLE

I have a IF function and would like to calculate the MAX of a column from a RELATEDTABLE inside the condition. The problem is that MAXA needs a column and I can't figure out how to return one column of a RELATEDTABLE.
= IF(MAXA(RELATEDTABLE(MyTable))
I can't simply use RELATED because the relationships between my tables don't allow this (one to many relationships).
If you simply want to find the maximum value of a column in a table on many-side of the relationship (related table) for each row in a table on the one side of the relationship then simple
VAR max_val = MAX(MyTable(column_name))
.. IF (max_val ...)
would suffice (if used in a measure).
If you want to use it in a DAX formula for a calculated column created in a table on the one side of the relationship then CALCULATE needs to be used to switch row context into a filter context
VAR max_val = CALCULATE ( MAX(MyTable(column_name)) )
.. IF (max_val ... )
Here Mydateble is the table that is on many-side of the relationship

how to distribute the subcategory of an axis in Power Bi

At the beginning I had this table
what I want is to divide the universe into 3 sections, so I do the following
i believe in column
UniqueRank =
RANKX (
inci;
FORMAT (inci[nro_casos]; "0000" ) & inci[region] & inci[site]
)
then I create two measures
ranking_total =
RANKX (
ALLSELECTED ( inci );
inci[UniqueRank];
MAX ( inci[UniqueRank] )
)
tirdh_case = IF(inci[ranking_total]<=COUNTROWS(ALLSELECTED(inci))*0.33;"3P";
IF(inci[ranking_total]<=COUNTROWS(ALLSELECTED(inci))*0.66;"2P";"1P"))
Then I would stay as follows. As you can see, filters of week and region can be applied and normal is divided into three parts, but I want to show it in a graph, and I want to put the tirdh_case as the axis, for this I create a new table called 'axis'
so I create the measure that intersects these two tables
suma_inci = CALCULATE (
SUM( inci[nro_casos] );
FILTER ( inci; [tirdh_case] IN VALUES ('axis'[indice]) )
)
As you can see in the image, the graph works perfectly, but I am inserting the 'site' column as a subcategory, so that when I click on each bar it shows me the sites that belong to that bar, but what happens is that all the sites are accumulate in the first bar ... how would you link the sites and categorize correctly?
This is logical because Measures are dynamically calculated based on your selection. So when you select your bar with the "P1", it needs to recalculate your splitting what is not possible because it is what you select. So if you want to go this level deeper, you need to make your measures a column so it is static divided.
Your column is than like:
tirdh_case =
var maxRank = MAX(inci[UniqueRank])
var splitNr = 3
var divider = maxRank/3
return CEILING((1 + maxRank - inci[UniqueRank])/divider;1) & "P"
You do not need an extra table now because you can use this column as your axis. I made it such that you can change the splitNr to your liking.
End result: