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
Related
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/
The Films table looks like this
There is a ComfortBreaks table looking like image
In the Films table I need to create a calculated column called NumberBreaks which shows for each film the number of breaks needed. To do this I have to pick out the value of the Breaks column where:
The value of the lower limit in the ComfortBreaks table is less than or equal to this film's running time in minutes
and
The value of the upper limit in the ComfortBreaks table is greater than this film's running time in minutes.
the result should look like the image below
There cannot be a relationship between the two tables. so this has to be done without creating relationship between them.
I tried lookup function but it showed error:A table of multiple values was supplied where a single value was expected.
You can use this below code for your custom column. Considering
number_of_breaks =
VAR current_row_run_time_minutes = Films[RunTimeMinutes]
RETURN
MINX (
FILTER(
ComfortBreaks,
ComfortBreaks[LowerLimit] <= current_row_run_time_minutes
&& ComfortBreaks[UperLimit] > MonthNumber
),
ComfortBreaks[Breaks]
)
You can perform your further average calculation on the new custom column now.
I know this must be extremely simple, but every example I can find online only works within a single table. I've simplified my situation to these two tables:
I want to add a calculated column to the first table, showing the most recent value for that id. It also needs to work with text.
There are a variety of ways to do this kind of thing as I've explained before and all of the solutions there can be adjusted to work in this case.
Doing this as a calculated column and with a second table, you need to make sure you are using row context and filter context appropriately.
Here's are a couple different possibilities I think may work:
MostRecentValue =
MAXX ( TOPN ( 1, RELATEDTABLE ( Table2 ), Table2[date] ), Table2[value] )
In this one, RELATEDTABLE is doing the work of filtering Table2 to only the rows where id matches Table1.
MostRecentValue =
VAR PrevDate = CALCULATE ( MAX ( Table2[date] ) )
RETURN CALCULATE ( MAX ( Table2[value] ), Table2[date] = PrevDate )
The relationship is more subtle here. Wrapping the MAX in CALCULATE forces a context transition so that the row context (which includes id) is applied to Table2 as filter context.
Basically, I’d like to get one entity totals, but calculated for another (but still related/associated!) entity. Relation type between these entities is many-to-many.
Just to be less abstract, let’s take Trips and Shipments as mentioned entities and Shipments’ weight as a total to be calculated.
Calculating weight totals just per each trip is pretty easy task. Here is a table of Shipments weights:
We place them into some amounts of trucks/trips and get following weight totals per trip:
But when I try to show SUM of Trip weight totals (figures from 2nd table) per each related Shipment (Column from 1st table), it becomes much harder than I expect.
It should look like:
And I can’t get such table within Power BI.
Data model for your reference:
Seems like SUMMARIZE function is almost fit, but it doesn’t allow me to use a column from another table than initialized in the function:
Additional restrictions:
Selections made by user (clicks on cells etc.) should not affect calculation anyhow.
The figures should be able to be used in further calculations, using them as a basis.
Can anyone advise a solution? Or at least proper DAX references to consider? I thought I could find a quick answer in DAX reference guide on my own. However I failed to find a quick answer.
Version 1
Try the following DAX function as a calculated column inside of your shipments table:
TripWeight =
VAR tripID =
RELATED ( Trips[TripID] )
RETURN
CALCULATE (
SUM ( Shipments[ShipmentTaxWeightKG] );
FILTER ( Shipments; RELATED ( InkTable[TripID] ) = tripID )
)
The first expression var tripID is storing the TripID of the current row and the CALCULATE function gets the SUM of all of the weight for all the shipments that belong to the current trip.
Version 2
You can also create a calculated table using the following DAX and create a relationship between the newly created table and your Trips table and simply display the weight in this table:
TripWeight =
GROUPBY (
Shipments;
Trips[TripID];
"Total Weight KG"; SUMX ( CURRENTGROUP (); Shipments[ShipmentTaxWeightKG] )
)
Version 3
Version 1 and 2 are only working if the relationship between lnkTrip and Shipment is a One-to-One relationship. If it is a many-to-one relationship, the following calculated column can be created inside of the Trips table:
ShipmentTaxWeightKG by Trip = SUMX(RELATEDTABLE(Shipments); Shipments[ShipmentTaxWeightKG])
hope this helps.
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: