SSAS Tabular DAX using of IF Statement - if-statement

I have this query written in SQL:
SELECT
CustomerId,
CustomerType,
CASE WHEN CustomerStatus = 'VIP'
THEN CustomerDiscountType
ELSE NULL
END AS CustomerDiscountType
FROM Customer
And I would like to write this in DAX query:
I know that I can write like this:
EVALUATE
(
SUMMARIZE
(
'Customer',
'Customer'[CustomerId],
'Customer'[Type],
'Customer'[DiscountType],
"Customer VIP", IF('Customer'[Status] = "VIP", 'Customer'[DiscountType], BLANK())
)
)
But when I write if condition I need include attribute 'Customer'[DiscountType] in that query too, but I would like to write the column name of that IF statement
"DiscountType", but it isn't possible for me like below.
EVALUATE
(
SUMMARIZE
(
'Customer',
'Customer'[CustomerId],
'Customer'[Type],
"DiscountType", IF('Customer'[Status] = "VIP", 'Customer'[DiscountType], BLANK())
)
)
It failed with this error because of existing DiscountType column:
Function 'SUMMARIZE' cannot add column [DiscountType] since it already exists.

Instead of using SUMMARIZE you can use SELECTCOLUMNS function. I think the error is caused because the IF function returns DiscountType column and it already exists in your Customer table. Also I am unsure if SUMMARIZE works without any aggregation like used in your DAX expression.
Try this expression:
EVALUATE
(
SELECTCOLUMNS (
'Customer',
"CustomerId", 'Customer'[CustomerId],
"Type", 'Customer'[Type],
"DiscountType", IF ( 'Customer'[Status] = "VIP", 'Customer'[DiscountType], BLANK () )
)
)
UPDATE: OP tells via comments the version used is SSAS 2014 which doesn't support SELECTCOLUMNS function.
You can use a mix of SUMMARIZE and ADDCOLUMNS functions to get the expected result.
EVALUATE
(
SUMMARIZE (
ADDCOLUMNS (
ADDCOLUMNS (
DISTINCT ( Customer[CustomerID] ),
"Type", CALCULATE ( VALUES ( Customer[Type] ) ),
"Status", CALCULATE ( VALUES ( Customer[Status] ) ),
"DiscountType1", CALCULATE ( VALUES ( Customer[DiscountType] ) )
),
"DiscountType", IF ( [Status] = "VIP", [DiscountType1], BLANK () )
),
[CustomerId],
[Type],
[DiscountType]
)
)
It is not tested but should work, let me know if this helps for you.

Related

Summarize and remove any rows which has a blank total

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 )

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 subquery measure?

I'm struggling with a DAX query and wondered if you could help?
Consider this table (or visualisation) called 'Builds':
Build....App....Status
Build1...App1...UAT
Build1...App2...Complete
Build2...App1...Complete
Build2...App2...Complete
I would like to add a measure column called 'AppsOutstanding' to show a count of Apps for that Build that aren't 'Complete'. Like so:
Build....App....Status......AppsOutstanding
Build1...App1...UAT.........1
Build1...App2...Complete....1
Build2...App1...Complete....0
Build2...App2...Complete....0
I almost need to do a 'subquery' measure!? Something like:
SELECT COUNT(Status) FROM Builds
WHERE Build = [The Build In This Row]
AND Status <> 'Complete'
I'm a bit stumped how to translate this into DAX? Here is my unsuccessful attempt:
AppsUnavailable = CALCULATE (
count(Builds[Build]),
CALCULATETABLE (
SUMMARIZE ( Builds,Builds[Status] ),
Builds[Status] <> "Complete"
))
Thanks in advance!
UPDATE
I've tried this, but the count isn't working, and also this DAX filters "Complete" statuses out of my actual results! Which i don't want. I only want to filter the "Complete" statuses out of my count measure....
AppsUnavailable =
CALCULATE (
COUNT ( Builds[Build] ),
FILTER (
ALL ( Builds[Build] ),
Builds[Build] = SELECTEDVALUE ( Builds[Build] )
),
FILTER (
ALL ( Builds[Status] ),
Builds[Status] <> "Complete"
)
)
UPDATE 2
I think I'm doing something fundamentally wrong. I've really dumbed it down to just find other 'Builds' with the same name, and it still only returns 1's!
AppsUnavailable =
CALCULATE (
COUNT ( Builds[Build] ),
FILTER (
ALL ( Builds[Build] ),
Builds[Build] = SELECTEDVALUE ( Builds[Build] )
)
)
UPDATE 3
This query (when testing on a single table (no joins) sample) produces this:
Build....App....Status......AppsOutstanding
Build1...App1...UAT.........1
Build1...App2...Complete....0
Build1...App2...UAT.........1
Build2...App1...Complete....0
Build2...App2...Complete....0
But i actually need this:
Build....App....Status......AppsOutstanding
Build1...App1...UAT.........2
Build1...App2...Complete....0
Build1...App2...UAT.........2
Build2...App1...Complete....0
Build2...App2...Complete....0
So Build1 has 2 Apps that are not complete.
I then need to look into why I'm getting all 1's in the 'live' environment. It must be to do with filtering on 2 tables as opposed to 1....
You can replace [The Build In This Row] with SELECTEDVALUE ( Builds[Build] ).
Try this:
AppsUnavailable =
CALCULATE (
COUNT ( Builds[App] ),
FILTER (
ALL ( Builds[Status], Builds[Build] ),
Builds[Build] = SELECTEDVALUE ( Builds[Build] )
&& Builds[Status] <> "Complete"
)
) + 0
powerBI Table visualization internal use SUMMARIZECOLUMNS which is removed row without data from the measure. You have 2 options:
-Right click on GroupBy Column -> "Show item with no data"
-Add +0 to calculation after the last parenthesis
UPDATE:
CALCULATE (
COUNT ( YourTable[App] ),
FILTER (
ALL ( YourTable[App], YourTable[Build], YourTable[Status] ),
YourTable[Build] = SELECTEDVALUE ( YourTable[Build] )
&& YourTable[Status] <> "Complete"
)
) + 0

How can I create a new table in Power BI from three columns of another table with a filter?

I have a table with repeating values ​​in the index column, and a third column that has values ​​only in some rows
Table I have now:
My objective is to create a new table that selects the CompanyID, Start Date, and Contact Date columns but excludes the rows that have blank values from any of those
New table I want to have:
I used the following DAX code:
CALCULATETABLE (
Table,
FILTER ( Table, NOT ( ISBLANK ( Table[Start Date] ) ) )
)
The issue with this code is that it selects all 350+ columns of my original data instead of just having the 3 columns I want. Additionally, I couldn't figure out how to add additional filters so I don't have blank rows for Contact Date and ComapanyID
I did try the below code as well, but it doesn't work
CALCULATETABLE (
Table,
FILTER ( Table, NOT ( ISBLANK ( Table[Start Date] ) ) ),
FILTER ( Table, NOT ( ISBLANK ( Table[Order Date] ) ) ),
FILTER ( Table, NOT ( ISBLANK ( Table[Employee Count] ) ) )
)
The SELECTCOLUMNS function allows you to pick specific columns.
For example:
New Filtered Table =
EVALUATE
SELECTCOLUMNS (
CALCULATETABLE (
Table,
NOT ( ISBLANK ( Table[Start Date] ) ),
NOT ( ISBLANK ( Table[Order Date] ) ),
NOT ( ISBLANK ( Table[Employee Count] ) )
),
"CompanyID", Table[CompanyID],
"Contact Date", Table[Contact Date],
"Start Date", Table[Start Date]
)

Is there a way to dynamically switch which relationship is active in powerpivot (based on slicer selection)?

I have a fact table (MORT) and a dimension table (GEO) in PowerPivot (2016). There are two joins between the tables with two columns in MORT relating to one column in GEO. We can call one join RES and the other REG. I have a large number of measures and I want to allow the user to quickly change between seeing the measures using the RES and REG relationships - essentially I want to be able to switch the active relationship (referred to as switch in the code). This would probably be based on a detached slicer.
I have tried nesting an if statement and various ways of defining a variable to use in a CALCUATE but either have the error that USERELATIONSHIP can only be used in a calculate (in the case of the if) or that USERELATIONSHIP only accepts a column reference (in the case of the var).
IF STATEMENT
MEASURE:= CALCULATE(COUNT(MORT[ID]), GEO,
IF(LASTNONBLANK(SWITCH,1)="RES",
USERELATIONSHIP(MORT[RES], GEO[AREA]),
USERELATIONSHIP(MORT[REG], GEO[AREA]))
VAR
MEASURE:=
VAR switch = IF(LASTNONBLANK(Switch,1)="RES", MORT[RES], MORT[REG])
RETURN
CALCULATE(COUNT(MORT[ID]), GEO, USERELATIONSHIP(switch, GEO[AREA])
I could create every measure with an if statement at the start to check the value of switch but this creates a lot of duplicate code.
I want some way for the end user to change the active relationship in a measure but ideally without a lot of duplicated code.
How about like this?
MEASURE :=
IF (
LASTNONBLANK ( Switch, 1 ) = "RES",
CALCULATE ( COUNT ( MORT[ID] ), GEO, USERELATIONSHIP ( MORT[RES], GEO[AREA] ) ),
CALCULATE ( COUNT ( MORT[ID] ), GEO, USERELATIONSHIP ( MORT[REG], GEO[AREA] ) )
)
Edit:
In your comment, you said you tried this:
M :=
VAR calc =
FILTER ( MORT, MORT[CAUSE] >= "I" && 'Remake Data'[CAUSE] <= "I9" )
VAR select =
IF (
HASONEVALUE ( 'SWITCH'[Switch] ),
LASTNONBLANK ( 'SWITCH'[Switch], 1 ),
"ERROR"
)
RETURN
IF (
select = "RES",
CALCULATE ( COUNTROWS ( calc ), USERELATIONSHIP ( MORT[RES], GEO[Area] ) ),
IF (
selection = "REG",
CALCULATE ( COUNTROWS ( calc ), USERELATIONSHIP ( MORT[REG], GEO[Area] ) ),
"ERROR"
)
)
There are a few problems with this, but mainly, if you define something as a variable, then it's constant and won't be affected by other things within a CALCULATE. Try the following instead.
Define a new measure:
CountRowsMeasure =
COUNTROWS ( FILTER ( MORT, MORT[CAUSE] >= "I" && 'Remake Data'[CAUSE] <= "I9" ) )
Then use that measure within your M measure:
M =
VAR select = SELECTEDVALUE( 'SWITCH'[Switch], "ERROR" )
RETURN
SWITCH (
select,
"RES", CALCULATE ( [CountRowsMeasure], USERELATIONSHIP ( MORT[RES], GEO[Area] ) ),
"REG", CALCULATE ( [CountRowsMeasure], USERELATIONSHIP ( MORT[REG], GEO[Area] ) ),
"ERROR"
)