I have table contain
name course attendance
I want count how many student in different course
Output I want = 3 student
because Ahmad have two different course
enter image description here
test this measure:
Result =
COUNTROWS ( SUMMARIZE ( TableName, TableName[name], TableName[course name] ) )
You can also add the 3rd column:
Alternative Solution :
Result =
COUNTROWS (
SUMMARIZE (
TableName,
TableName[name],
TableName[course name],
TableName[attendence]
)
)
If we test them, the result is the same:
SUMX(
VALUES(tbl[course name])
,CALCULATE(
COUNTROWS(VALUES(tbl[name]))
)
)
Related
I have written a DAX code which returns names of individuals with a count of how many customers they own, however what I need to do is only retrieve the names or essentially filter out the names who have a blank total, how would I go about this as I have tried everything
summarizedCAM =
SUMMARIZE (
d_cam,
d_cam[name],
"Total", DISTINCTCOUNT(ftm[p_key])
)
Current Output:
summarizedCAM =
FILTER(
SUMMARIZE (
d_cam,
d_cam[name],
"Total", DISTINCTCOUNT(ftm[p_key])
)
,ISBLANK([Total])=FALSE()
)
try like this :
Modelling --> New Table
summarizedCAM =
VAR _tbl =
SUMMARIZE ( d_cam, d_cam[name], "Total", DISTINCTCOUNT ( ftm[p_key] ) )
RETURN
FILTER ( _tbl, [Total] > 0 )
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
)
How to calculate rank within Category defined on sales level. Say, that we want to label products with Sales above some threshold with Category "high", and below that threshold with Category "low".
Here is a sample data.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcisqzSwpVtJRSiwoyEkF0oZKsTpIwkmJeUAIZJigipfn56QlpRYVVQLZpqhSyRlQcWOweFhqempJYlJOKlgusagovwS7XEF+SWJJPtwJKHL5eZn5eUDaHNUqHI5GdkEsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Category = _t, Product = _t, Amount = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Amount", Int64.Type}})
in
#"Changed Type"
My question is a nephew related to its older uncle, who now I want to call in:
Percent Rank within Category =
VAR HasOneValueTrue = HASONEVALUE ( MyTable[Product] )
VAR tbl =
CALCULATETABLE (
VALUES ( MyTable[Product] ),
REMOVEFILTERS ( MyTable[Product] ),
VALUES ( MyTable[Category] )
)
VAR result =
CALCULATE (
DIVIDE (
RANKX (
tbl,
[Sales],
,
ASC
) - 1,
COUNTROWS ( tbl ) - 1
)
)
RETURN
IF (
HasOneValueTrue,
result
)
The difference is that the uncle has Category defined in table column, but now we want to have category calculated on the fly based on sales level. So I tried
replacing the VAR tbl code with the following one with the threshold of 4:
var tbl =
SUMMARIZECOLUMNS (
MyTable[Product],
"CalculatedCategory", IF ( [Sales] > 4, "high", "low" ),
"AggSales", [Sales]
)
Nevertheless, I am not able to refer to such defined variable. I also failed with trial based on creating first a DAX table and then trying to refer to it.
Here are expected results:
References
Here is the family of related questions which members I met on the way while approaching to state this problem.
DAX equivalent of Excel PERCENTRANK.INC per category
DAX RANKX for within Category
DAX REMOVEFILTERS vs ALL
The value parameter in DAX function RANKX
DAX ALLEXCEPT to sum by category of multiple dimension tables
This can be done with a minor modification to my answer here. Copied below:
Percent Rank =
VAR ProductsInCategory =
CALCULATETABLE (
VALUES ( MyTable[Product] ),
ALLSELECTED ( MyTable[Product] )
)
VAR RankProduct = RANKX ( ProductsInCategory, [Sales],, ASC )
RETURN
IF (
HASONEVALUE ( MyTable[Product] ),
DIVIDE ( RankProduct - 1, COUNTROWS ( ProductsInCategory ) - 1 )
)
First, define the calculated category as you suggested.
CalculatedCategory = IF ( [Sales] > 4, "high", "low" )
Then plug that into a filter in the ProductsInCategory variable.
Exp. Results =
VAR CalculatedCategory = [CalculatedCategory] /*Determine current category*/
VAR ProductsInCategory =
CALCULATETABLE (
VALUES ( MyTable[Product] ),
FILTER (
ALLSELECTED ( MyTable[Product] ),
[CalculatedCategory] = CalculatedCategory /*New Condition*/
)
)
VAR RankProduct = RANKX ( ProductsInCategory, [Sales],, ASC )
RETURN
IF (
HASONEVALUE ( MyTable[Product] ),
DIVIDE ( RankProduct - 1, COUNTROWS ( ProductsInCategory ) - 1 )
)
Output:
Edit:
To handle the case where there is only 1 product in a category, you can use MAX to disallow a zero value for the denominator.
Exp. Results =
VAR CalculatedCategory = [CalculatedCategory] /*Determine current category*/
VAR ProductsInCategory =
CALCULATETABLE (
VALUES ( MyTable[Product] ),
FILTER (
ALLSELECTED ( MyTable[Product] ),
[CalculatedCategory] = CalculatedCategory /*New Condition*/
)
)
VAR RankProduct = RANKX ( ProductsInCategory, [Sales],, ASC )
RETURN
IF (
HASONEVALUE ( MyTable[Product] ),
DIVIDE (
RankProduct - 1,
MAX ( COUNTROWS( ProductsInCategory ) - 1, 1 )
)
)
Being very grateful to Alexis Olson, I would like to share a different solution I ended up with. The solution proposed by Alexis works well in my simple example, but it did not work in my complex model. In my complex model the RANKX function does not give the expected results. RANKX returns the same rankings for different sales values.
For the time being this is the solution that works without figuring out what causes RANKX to return ties for different sales values.
First of all, defining Category measure:
CalculatedCategory =
SWITCH (
TRUE (),
NOT ( HASONEVALUE ( MyTable[Product] ) ), "total", -- important to handle totals
[Sales] <= 4, "low",
[Sales] > 4, "high",
"other"
)
It is important to exclude totals from Category. I did it by setting up a different category for totals. Otherwise totals will fall into "high" category bucket. It would distort final results.
I have not used RANKX in calculation of Percent Rank within Category. I used MIXTURE OF COUNTROWS and FILTER.
PercentRank within Category =
VAR category = [CalculatedCategory]
VAR ProductSales = [Sales]
VAR ProductsMatching =
COUNTROWS (
FILTER (
ALLSELECTED ( MyTable[Product] ),
[CalculatedCategory] = category
&& [Sales] >= ProductSales
)
)
var ProductsAll =
COUNTROWS (
FILTER (
ALLSELECTED ( MyTable[Product] ),
[CalculatedCategory] = category
)
)
RETURN
DIVIDE (ProductsMatching-1, MAX( ProductsAll-1, 1 ))
I calculated rows of two tables. First table ProductsMatching has all products that have sales in appropriate category and sales that are higher or equal of the product. ProductsAll returns number of products in category.
I am currently working in Power BI and need to write the DAX code for counting number of string values when string filtered by A is also appearing when same string filtered by B.
I have tried various of codes and everything that came in my mind, but I am lacking experience in comparing values in DAX, and kinda beat at the moment.
eg.
When looking at table, I need to count (distinct) when John, Adam, Julie from A is also appearing in B, which is in this case is 3.
I will be happy if someone has any ideas or directions for me to seek into!
Assuming your table name is "Data":
Count of Common Names =
VAR A_Names =
CALCULATETABLE ( VALUES ( Data[Name] ), Data[Filter] = "A" )
VAR B_Names =
CALCULATETABLE ( VALUES ( Data[Name] ), Data[Filter] = "B" )
VAR Common_Names =
INTERSECT ( A_Names, B_Names )
RETURN
COUNTROWS ( Common_Names )
How it works:
First, we create a table of distinct names for filter A.
Second, we do the same for filter B.
Finally, we find what names exist in both tables, by finding their intersection.
Edit:
To calculate costs for common names, modify the above measure:
Cost of A given B =
VAR A_Names =
CALCULATETABLE ( VALUES ( Data[Name] ), Data[Filter] = "A" )
VAR B_Names =
CALCULATETABLE ( VALUES ( Data[Name] ), Data[Filter] = "B" )
VAR Common_Names =
INTERSECT ( A_Names, B_Names )
RETURN
CALCULATE ( SUM ( Data[Cost] ), Common_Names, Data[Filter] = "A" )
This will count the number of distinct names, having more than one distinct value:
Names with Multiple Distinct Values =
COUNTROWS (
FILTER (
SUMMARIZECOLUMNS (
MyTable[Name],
"Distinct Values",
DISTINCTCOUNT ( MyTable[Values] )
),
[Distinct Values] > 1
)
)
I would like to count the frequency of a value in a column for each row. In Excel my situation can be solved with this formula:
=COUNTIF(I:I;I4)
In PowerBi Report and I have a table of students with a column, "Pääaine" (main subject). There are 81 distinct values in 1580 rows. I would like to calculate the number of similar students for each row (so that I can filter out the main subjects that have 4 or less students).
How do I do it in PowerBI?
With Calculated column like this I get 1580 for each cell:
Pääaine lkm =
CALCULATE(
COUNTROWS(Opiskelunkulku);
FILTER(
Opiskelunkulku;
Opiskelunkulku[Pääaine] = Opiskelunkulku[Pääaine]
)
)
You can use COUNTROWS() and EARLIER() to achieve this. EARLIER() returns the value for the specified column in the current row context.
Pääaine lkm =
COUNTROWS (
FILTER (
Opiskelunkulku,
Opiskelunkulku[Pääaine] = EARLIER ( Opiskelunkulku[Pääaine] )
)
)
As an alternative to Rory's answer try CALCULATE() with ALLEXCEPT() as a filter. Like this:
Pääaine lkm =
CALCULATE (
COUNTROWS ( Opiskelunkulku ),
ALLEXCEPT ( Opiskelunkulku, Opiskelunkulku[Pääaine] )
)