Convert DAX expression table into a scalar - powerbi

How to convert the following DAX expression to a scalar?
SELECTCOLUMNS(FILTER(rules, rules[RuleNumber] = 1), "AntecedentID", [AntecedentID])
This returns a table with a single cell, but I need a scalar value.
Tried wrapping the expression into a VALUES or a MAX, but no luck.

M =
CONCATENATEX (
SELECTCOLUMNS (
FILTER ( rules, rules[RuleNumber] = 1 ),
"#AntecedentID", [AntecedentID]
),
[#AntecedentID],
", "
)

Related

DAX function to count if a cell value begins with a certain character

I need a function that allows me to count how many rows, for a specific column, start with a specific letter.
For example:
I have a table with columns A, B, and C.
I want to count how many rows have a value starting with M in column B.
I am new to this and need to develop a DAX function.
Any idea of how to do that?
Count =
COUNTROWS (
FILTER (
'Table' ,
LEFT ( 'Table'[column B] , 1 ) = "M" )
)
)
You could make a temp table by filtering the column using a regex to match the first character. Then count the rows in the temp table.
[test] = VAR tempTable =
FILTER ( 'Table', IsMatch('Table'[B],"^M",MatchOptions.Contains)))
RETURN
COUNTROWS ( tempTable )

DAX: Filter, group by and count distinct values in Power BI

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
)

DAX - Filter Table by Multi Select Filter Condition

Am trying to filter a table with Multi Selection Filter.
DAX: To select the values in the filter selected (Multi)
SelectedEnvironments =
CONCATENATEX (
VALUES ( Environments[ShortEnvName] ),
Environments[ShortEnvName],
", "
)
Result:
But when trying to filter table based on above filter variable doesnt return anything.
DAX:
Aggregated Usage =
VAR __SelectedEnvironments =
CONCATENATEX (
VALUES ( Environments[ShortEnvName] ),
Environments[ShortEnvName],
", "
)
RETURN
CALCULATETABLE (
LastestDBUsage,
LastestDBUsage[Environment] IN { __SelectedEnvironments }
)
If I hard code the values within IN operator it work fine. What am doing wrong? Do I need to format the string for IN operator
DAX (Works fine with Hard Code Values)
Aggregated Usage =
VAR __SelectedEnvironments =
CONCATENATEX (
VALUES ( Environments[ShortEnvName] ),
Environments[ShortEnvName],
", "
)
RETURN
CALCULATETABLE (
LastestDBUsage,
LastestDBUsage[Environment] IN { "DEV", "TST" }
)
Actually, the IN operator works on tables, CONCATENATEX returns a string.
{ __SelectedEnvironments }
returns a table with one row consisting of one column like for instance "DEV, TST"
to make the code work it would be changed to use a table instead, like for instance
Aggregated Usage =
VAR __SelectedEnvironments = VALUES ( Environments[ShortEnvName] )
RETURN
CALCULATETABLE (
LastestDBUsage,
LastestDBUsage[Environment] IN __SelectedEnvironments
)

PowerBI DAX to find value of a column from another column in same table

I'm trying to match the rankorg column with a calculated measure ceiling and pull out the corresponding blackout value -
ValMaxSequence =
CALCULATE (
VALUES ( org[Blackout] ),
FILTER ( ALL ( org[rankorg] ), org[rankorg] = ( [ceiling] ) )
)
I also tried a lookup function, but it needs column value, but I have the result in a measure
The ceiling measure is being calculated within the context of the FILTER function.
Try pre-calculating it before you use it inside another function.
ValMaxSequence =
VAR Ceil = [ceiling]
RETURN
CALCULATE (
VALUES ( org[Blackout] ),
FILTER ( ALL ( org[rankorg] ), org[rankorg] = Ceil )
)

How to perform a conversion based on a given value using dax

I'm trying to create a conversion based on distinct column values(emails) divided by number of transactions(clients) carried out.
I've tried pointing out a formula to count email in the column.
emailConversionRate =
DIVIDE (
DISTINCTCOUNT (
ClientTransView[TransEntityID]
),
Count( Interactions[chaneltype='email'
)
)
I expect the output to be the number of entity IDs divided by the channel type used.
You could try using CALCULATE function for filtering the chaneltype and use double quotes for email as it must be a string.
emailConversionRate =
DIVIDE (
DISTINCTCOUNT ( ClientTransView[TransEntityID] ),
CALCULATE (
COUNT ( Interactions[chaneltype] ),
Interactions[chaneltype] = "email"
)
)