I have the following simplified DAX measure. The real world example does a lot of heavy lifting to end up at the information that's in var t1
Here's the example for set up in DAX Studio:
EVALUATE
var t1 = DATATABLE("item", STRING, {{"a"}, {"b"}})
return
ADDCOLUMNS(
SUMMARIZE(
t1,
[item]
),
"count", calculate(countrows(t1))
)
This query returns a table with a count of 2 for each item, a and b.
If I were to create the simplified table in the data model and use the query it would return the correct result with a count of 1 for each item, a and b.
I understand that the reason is that t1 is a constant and that the two references to the t1 in the query are seperate references and therefor the filter context is different on the two instances.
Is there a way to do this using the table var t1 or do I need to solve this in a different method?
Current incorrect output:
[item],[count]
a,2
b,2
Expected output:
[item],[count]
a,1
b,1
You could use a FILTER like this:
ADDCOLUMNS (
SUMMARIZE ( t1, [item] ),
"count", COUNTROWS ( FILTER ( t1, [item] = EARLIER ( [item] ) ) )
)
Using GROUPBY is cleaner for this sort of thing, IMO.
GROUPBY ( t1, [item], "count", SUMX ( CURRENTGROUP(), 1 ) )
Related
I have a big model in PowerBI where there are many different aggregation and grouping based on columns being displayed or not on the final table.
Simplifying: I need to do a conditional statement doing the sum if the value of column 1 is A1 but doing the MAX() if the value of column 1 is A2.
I need to have that information in the same column of the final output.
How would you go for this one?
Thank you very much for your help!
if you have only two values you can do a simple IF like this :
Measure = IF ( SELECTEDVALUE('Table'[Column1]) = "A1", SUM('Table'[Column2]), MAX('Table'[Column2]))
Please try this code:
TblMeasure =
VAR TblSummary =
ADDCOLUMNS (
VALUES ( FactTable[Column1] ),
"Test",
IF (
FactTable[Column1] = "A1",
SUM ( FactTable[Column2] ),
MAX ( FactTable[Column2] )
)
)
RETURN
SUMX ( TblSummary, [Test] )
If we test it on a table visual:
I'm working with a Soccer League Database with a table like this:
Soccer League Table
I would like to create a measure to calculate the most repeated Division that every team has played in all the seasons.
For example: For Team A it will be 1 and for Team B 3
Thank you!
Regards
Assuming a table named Table1, this measure:
MostRepeatedDivision :=
VAR T1 =
SUMMARIZE( Table1, Table1[DIVISION], "Count", COUNTROWS( Table1 ) )
VAR MostRepeated =
MAXX( T1, [Count] )
RETURN
MAXX( T1, IF( [Count] = MostRepeated, Table1[DIVISION] ) )
You can achieve this with the following expressions:
Create a calculated table counting the frequency of each division
Frequency =
SUMMARIZE(
Soccer,
Soccer[Team], Soccer[Divison],
"Number", Count(Soccer[Divison])
)
Add a calculated column that ranks the frequencies per team
Rank =
RANKX(
FILTER(
ALL('Frequency'),
Frequency[Team] = EARLIER(Frequency[Team])
),
'Frequency'[Number]
)
The resulting calculated table showing teams and top divisions
Top Division =
SELECTCOLUMNS(
FILTER(
Frequency,
Frequency[Rank] = 1
),
"Team", Frequency[Team],
"Division", Frequency[Divison]
)
I have three tables with these relationships:
'T1'[B]<-'T2'[A] (Many to one respectively) &
'T2'[D]<-'T3'[A] (Many to one respectively)
Now imagine I want to have a report like this table:
Items of 'T2'[C] | sum('T1'[C])
I also need to filter this table by selecting some items of T3[B]. What I did was creating a Measure:
SumByT3 = CALCULATE(SUM('T1'[C]), USERELATIONSHIP('T1'[B], 'T2'[A]), USERELATIONSHIP('T2'[D],'T3'[A]))
(While having these relationships defined.)
Then I put a page filter on T3[B]. After that I created a table with T2[C] as axis and SumByT3 as value. But it is not working. Even if I remove the page filter.
Try this measure
which gives the SUM of all T1[C]
{that has the same filteredT2[A] as T1[B]}
{filteredT2 contains all T2[D] where T2[D]=T3[A]}
Measure =
CALCULATE (
SUM ( 'T1'[C] ),
TREATAS (
SUMMARIZE (
CALCULATETABLE ( T2, FILTER ( T2, ( T2[D] ) IN SUMMARIZE ( T3, T3[A] ) ) ),
T2[A]
),
T1[B]
)
)
The following returns T2 that only has T2[D]=T3[A]
Table =
CALCULATETABLE ( T2, FILTER ( T2, ( T2[D] ) IN SUMMARIZE ( T3, T3[A] ) ) )
Then, only unique T2[A] are extracted with
SUMMARIZE (
CALCULATETABLE ( T2, FILTER ( T2, ( T2[D] ) IN SUMMARIZE ( T3, T3[A] ) ) ),
T2[A]
)
which is used inside a TREATAS which creates an inner join of T2[A] and T1[B]
While executing the below DAX expression, I am getting an error "USERELATIONSHIP function can only use the two columns reference participation in relationship".
So could you please help me with that what's wrong with the expression?
Accuracy_Last_6_Month =
VAR ReferenceDate = MAX(Calender[Date])
VAR Last_6Month =
DATESINPERIOD(
Calendar_Last6Month[Date].[Date],
ReferenceDate,
-6,
MONTH
)
VAR Result =
CALCULATE(
[Accuracy],
REMOVEFILTERS(Calender[Date]),
KEEPFILTERS(Last_6Month),
USERELATIONSHIP(Calender[Date],Calendar_Last6Month[Date].[Date])
)
RETURN
Result
Relationship created between tables as inactivated form:
Columns used in both the table:
You should be able to use a single Calendar. Your second calendar is redundant.
I would write something like this:
Accuracy_Last_6_Month =
CALCULATE([Accuracy],
FILTER(ALL(Calender),
Calender[Date] > MAX(Calender[Date])-180 &&
Calender[Date] <= MAX(Calender[Date])))
I'm guessing the error is because you are using Calendar_Last6Month[Date].[Date] inside USERELATIONSHIP, which isn't actually a table column. Try deleting that .[Date] suffix everywhere in your measure:
Accuracy_Last_6_Month =
VAR ReferenceDate = MAX ( Calender[Date] )
VAR Last_6Month =
DATESINPERIOD ( Calendar_Last6Month[Date], ReferenceDate, -6, MONTH )
VAR Result =
CALCULATE (
[Accuracy],
REMOVEFILTERS ( Calender[Date] ),
KEEPFILTERS ( Last_6Month ),
USERELATIONSHIP ( Calender[Date], Calendar_Last6Month[Date] )
)
RETURN
Result
I generally avoid using these Time Intelligence suffixes entirely.
How can I filter the table by Category = Animal, then group by Class, and count the distinct id for each group?
So far I can only group by and count distinct values:
output = SUMMARIZE(my_table, my_table[Class], "Distinct Count", DISTINCTCOUNT(my_table[Id]))
I tried:
output = CALCULATETABLE(SUMMARIZE(my_table, my_table[Class]), DISTINCTCOUNT(my_table[Id]), FILTER(my_table, my_table[Category ] = "Animal"))
which caught error:
The True/False expression does not specify a column. Each True/False expressions used as a table filter expression must refer to exactly one column.
I also tried the way suggested by this post but it only counts the number of rows in the table rather than the distinct number of Id.
Try this:
output =
SUMMARIZECOLUMNS (
my_table[Class],
TREATAS ( { "Animal" }, my_table[Category] ),
"Distinct Count", DISTINCTCOUNT ( my_table[Id] )
)
Another option:
output =
CALCULATETABLE (
ADDCOLUMNS (
VALUES ( my_table[Class] ),
"Distinct ID", CALCULATE ( DISTINCTCOUNT ( my_table[Id] ) )
),
my_table[Category ] = "Animal"
)
SUMMARIZECOLUMNS version is generally an optimized way of writing the code so you should prefer that one. I have included ADDCOLUMNS/CALCULATETABLE construct only for the learning purpose.
Check this out: Filter SUMMARIZECOLUMNS
You filter in this way:
VAR __MyFilterTable = FILTER( ALL(T[category]), T[category] = "animal" )
RETURN
SUMMARIZECOLUMNS (
T[category],
__MyFilterTable
)
Or even shorter:
VAR __MyFilterTable = TREATAS ({"animal"}, T[category] )
RETURN
SUMMARIZECOLUMNS (
T[category],
__MyFilterTable
)