I have two tables
Primary Diag
L021
L022
L023
L024
L025
L026
and Look_Up_New
ICD ICD2 Inclusion Type
L021 L021 3
L022 L022 2
L023 L023 2
L024 L024 4
L025 L025 5
L026 L026 4
L027 L029 5
there are two relationship one active and the other not
The active one is ICD
when I wrote the below dax for the active one it works fine
Diag 1 = IF(diag[Primary Diag]=BLANK(),"X",
IF(RELATED(Look_Up_New[ICD]) = BLANK(),"X",
RELATED(Look_Up_New[Inclusion Type])))
but when i wrote for the inactive one i got an error
Diag 2 = CALCULATE(IF(diag[Sec. Diag 2]=BLANK(),"X",
IF(RELATED(Look_Up_New[ICD2]) = BLANK(),"X",
RELATED(Look_Up_New[Inclusion Type]))),
USERELATIONSHIP(Look_Up_New[ICD2],Diag[Primary Diag]))
How can I correct it
The reason this fails is that CALCULATE forces a context transition (i.e. it transforms row context into filter context), which means RELATED no longer has the row context it needs to operate.
Note this remark from the documentation:
The RELATED function needs a row context; therefore, it can only be used in calculated column expression, where the current row context is unambiguous, or as a nested function in an expression that uses a table scanning function. A table scanning function, such as SUMX, gets the value of the current row value and then scans another table for instances of that value.
I'd suggest a slightly different approach:
Diag 1 =
CALCULATE ( SELECTEDVALUE ( Look_Up_New[Inclusion Type], "X" ) )
Diag 2 =
CALCULATE (
SELECTEDVALUE ( Look_Up_New[Inclusion Type], "X" ),
USERELATIONSHIP ( Diag[Primary Diag], Look_Up_New[ICD2] )
)
Related
My data
Fact
iddimUser iddimtime
123 81200
124 84500
DimTime
iddimtime FullTime
81200 08:12:00.0000000
84500 08:45:00.0000000
Desired Table calculation
iddimUser iddimtime Time
123 81200 08:12:00.0000000
124 84500 08:45:00.0000000
I require to bring to the fact table a specific time from the time dimension base on one of the multiple relationships I have (I have several IdTimes in the fact table).
I tried this column calculation but getting an "A table of multiple values was supplied where a single value was expected" error
FullTime = TIME(
HOUR(CALCULATE( VALUES(DimTime[FullTime] ),
USERELATIONSHIP ( Fact[iddimtime], DimTime[iddimtime]))),
MINUTE(CALCULATE( VALUES(DimTime[FullTime] ),
USERELATIONSHIP ( Fact[iddimtime], DimTime[iddimtime]))),
SECOND(CALCULATE( VALUES(DimTime[FullTime] ),
USERELATIONSHIP ( Fact[IniciaProce], DimTime[iddimtime]))))
FullTime1 =
FORMAT(RELATED(DimTime[FullTime]),"hh:nn:ss")
This you can use as alternative to your measure and it will be much faster.
FullTime2 =
FORMAT(
LOOKUPVALUE(
DimTime[FullTime]
,DimTime[iddimtime]
,[iddimtime]
)
,"hh:nn:ss"
)
Why do you use the userelationship if there is only one active relationship between fact and dim tables ? All you need is the related function to access the related row in the dim table:
Note:
Ensure that The [iddimtime] column is the one who creates the relationship between both tables.
Related() is an iterator. Never use it with a calculate involving USERELATIONSHIP() in a calculated field.
RELATED() is used to access the column values from many side to one-side. Conversely, RELATEDTABLE() is used to access columns from one side to many side.
Please test this, and let me know if It solves your problem.
FullTime =
TIME ( HOUR ( RELATED ( DimTime[FullTime] ) ), MINUTE ( RELATED ( DimTime[FullTime] ) ), SECOND ( RELATED ( DimTime[FullTime] ) ) )
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.
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
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:
it seems like PowerBI is auto-ordering my table and I don't know why. So, I have this table (that I exported from a Excel file)
Colonne 1
7
25
1
8
3
Whenever I do a New Mesure, with this formula:
Mesure = FIRSTNONBLANK(Feuil1[Colonne 1];0)
It gives me the number 1. If I do LASTNONBLANK, it returns me the number 25. So it clearly auto-order my dataset, but whenever I look in the «Data» tab of the software, I see that my data are ordered the way I want them to be.
Any idea on how to keep the default ordering?
Unfortunately, that's the exact behavior of FIRSTNONBLANK() and LASTNONBLANK(). They iterate the values of a column based on their native sort order (i.e. data type). Therefore, FIRSTNONBLANK would return the smallest number 1 and and LASTNONBLANK the largest number 25. And in general, you cannot make assumptions on sort order of values in a table or in a column when using DAX.
Therefore, my suggestion is that you can explicitly program the logic to find the target value. Say the table is sorted based on date, you can find the key of the earliest/latest date (based on your use case), then lookup the value using the key.
Target =
VAR TargetKey =
CALCULATE(
VALUES(Table1[Key]),
FILTER(
Table1,
Table1[Date] = MIN(Table1[Date]) // or MAX(Table1[Date])
)
)
RETURN
LOOKUPVALUE(Table1[Colonne 1], Table1[Key], TargetKey)