Handling of selectedvalue when both options are chosen - powerbi

I am trying to create a dynamic chart tile when using drill-down. One of my slicers looks like this:
Here is my DAX formula for creating the snippet of the chart title:
var Standard = if(
isfiltered(yrs_data[Standard]),
if(exact(SELECTEDVALUE(yrs_data[Standard]),"Yes"),
"LOT TYPE=PROD",
"LOT TYPE=ENG"
),
"LOT TYPE=ALL"
)
The chart title works correctly except when both 'Yes' and 'No' are selected, then it incorrectly chooses "LOT TYPE=ENG", where I want it to be "LOT TYPE=ALL".
thx

Can you try this
test =
IF (
ISFILTERED ( yrs_data[Standard] ),
IF (
EXACT ( SELECTEDVALUE ( yrs_data[Standard] ), "Yes" ),
"LOT TYPE=PROD",
IF (
EXACT ( SELECTEDVALUE ( yrs_data[Standard] ), "No" ),
"LOT TYPE=ENG",
"LOT TYPE=ALL"
)
)
)

Related

Want to show null values in Dax

I have this sentence in DAX:
DEFINE
MEASURE 'BUYING SHOP'[FromDate] =
CALCULATETABLE (
DATEADD ( 'BUYING SHOP'[FROM_DATE], -1, YEAR ),
KEEPFILTERS ( TREATAS ( { #sadCode }, 'BUYING SHOP'[SAD_CODE] ) )
)
MEASURE 'BUYING SHOP'[ToDate] =
CALCULATE (
[Prior Completed Month],
KEEPFILTERS ( TREATAS ( { #sadCode }, 'BUYING SHOP'[SAD_CODE] ) )
)
MEASURE 'PRODUCT'[Maintenance_Repair] =
CALCULATE (
SUMX (
FILTER (
PRODUCT,
PRODUCT[PRODUCT_CATEGORY_CODE] = "MAINTENANCE_AND_REPAIR"
),
PURCHASE[Sum Purchases Prior]
)
)
EVALUATE
SUMMARIZECOLUMNS (
'DATE'[cMonth],
KEEPFILTERS ( TREATAS ( { #sadCode }, 'BUYING SHOP'[SAD_CODE] ) ),
KEEPFILTERS (
FILTER (
ALL ( 'DATE'[FullDate] ),
'DATE'[FullDate] >= 'BUYING SHOP'[FromDate]
&& 'DATE'[FullDate] <= 'BUYING SHOP'[ToDate]
)
),
"Maintenance_Repair", [Maintenance_Repair]
)
ORDER BY 'DATE'[cMonth] ASC
that returns this table:
enter image description here
What I want is that all months appear in the table even if it does not have any record.
for example:
03-Mar 20.8
04-Apr 0
05-May 222.04
06'Jub 0
and goes like that
Could anybody help me?
Try changing your measure to add zero.
MEASURE 'PRODUCT'[Maintenance_Repair] =
CALCULATE (
SUMX (
FILTER (
PRODUCT,
PRODUCT[PRODUCT_CATEGORY_CODE] = "MAINTENANCE_AND_REPAIR"
),
PURCHASE[Sum Purchases Prior]
)
) + 0

Why is var inside DAX measure not working?

I have my model with 3 tables:
Fact
Project
Roles
I have this measure working fine:
Basic:=
VAR _sumat = [Net Revenue] - [Expected Revenue]
VAR _sumlevel2 =
IF ( _sumat > 0, 0, _sumat )
RETURN
IF ( ISINSCOPE ( Fact[Level3] ), _sumat,
IF ( ISINSCOPE ( Fact[Level2] ), _sumlevel2 ))¨
But on this measure my var _Level0 does not work:
Result:=
VAR _LEVEL1 =
SUMX (SUMMARIZE (
FILTER (ALL ( 'revenue' ),
Fact[L1] = MAX ( Revenue[L1] ) && Fact[Level2] IN VALUES ( Fact[Level2] )
),
Fact[Level1],
Fact[Level2]
),
[Basic])
VAR _LEVEL0 =
SUMX (SUMMARIZE (
FILTER (ALL ( ‘Roles’ ),
Roles[Name] = MAX ( Roles[Name] ) && Fact[Level1] IN VALUES ( Fact[Level1] )
),
Roles[Name],
Fact[Level1]
),
[Basic])
RETURN
SWITCH (
TRUE(),
ISINSCOPE (Fact[Level3] ), [Basic],
ISINSCOPE ( Fact [Level2] ), [Basic],
ISINSCOPE ( Fact [Level1] ), _LEVEL1,
ISINSCOPE ( Roles[Name] ), _LEVEL0,
)
How can I rewrite _LEVEL0 in order to make it work??

DAX New & Lost Customers Table

I am trying to create a List for New Customers Added during the year and List of Lost Customers, I have written a DAX which works fine in summary count but doesn't work in table matrix.
NTB =
VAR currentCustomers =
VALUES ( Deposits[CIF ID] )
VAR currentDate =
MAX ( Deposits[Source.Date] )
VAR pastCustomers =
CALCULATETABLE (
VALUES ( Deposits[CIF ID] ),
ALL (
Deposits[Source.Date].[Month],
Deposits[Source.Date].[MonthNo],
Deposits[Source.Date].[Year]
),
Deposits[Source.Date] < currentDate
)
VAR newCustomers =
EXCEPT ( currentCustomers, pastCustomers )
RETURN
COUNTROWS ( newCustomers )
Total Row is correct, even if I remove one function the table remains same..
Appreciate your help
try this :
Modelling --> Add Table
Table =
VAR _max =
MAX ( Deposits[Source.Date] )
RETURN
ADDCOLUMNS (
SUMMARIZE ( Deposits, Deposits[CIF ID] ),
"NTB",
CALCULATE (
COUNT ( Deposits[CIF ID] ),
FILTER (
ALLEXCEPT ( Deposits, Deposits[CIF ID] ),
Deposits[Source.Date] >= _max
)
),
"Lost Customers",
CALCULATE (
COUNT ( Deposits[CIF ID] ),
FILTER (
ALLEXCEPT ( Deposits, Deposits[CIF ID] ),
Deposits[Source.Date] < _max
)
)
)

Power Bi: Top N visual level filter as Measure

Need a measure that will provide the same output as TopN visual level filter (than I can parameterize it).
The solution for simple cases provided HERE
But it doesn't work for more complicated cases...
EXAMPLE:
Don't work if you add any dimension that has Many to One Product Name relationship (Order Number for example).
Desired output: both tables (top and bottom) should be equal:
Example from screen available here HERE
NB! From usability perspective it's preferable to return Sales Rank in measure.
Here you go.
BottomN =
VAR param = [TopN Value]
VAR topNTable = CALCULATETABLE( TOPN(param,'Product', [Sales Amount], ASC), ALLSELECTED('Product'[Category],'Product'[Product Name]), FILTER(allselected(Sales), [Sales Amount] <> BLANK()))
RETURN
IF(NOT(ISEMPTY(Sales)),IF( SELECTEDVALUE('Product'[Product Name]) IN SELECTCOLUMNS( topNTable,"a", 'Product'[Product Name]) ,[Sales Amount]))
A bit reworked solution from David Bacci:
1. If you need just TopN Sales:
TopnSalesAmount =
VAR param = [TopN Value]
VAR topNTable =
CALCULATETABLE (
TOPN ( param, 'Product', [Sales Amount], ASC ),
ALLSELECTED ( 'Product'[Product Name] ),
FILTER ( ALLSELECTED ( Sales ), [Sales Amount] <> BLANK () )
)
RETURN
IF (
NOT ( ISEMPTY ( Sales ) ),
IF (
SELECTEDVALUE ( 'Product'[Product Name] )
IN SELECTCOLUMNS ( topNTable, "a", 'Product'[Product Name] ),
[Sales Amount]
)
)
2. If you need Rank for TopN Sales:
rnkTopnSalesAmount =
IF (
//ISINSCOPE ( 'Product Names'[Product Name]), -- depends, which one is used in visual
ISINSCOPE ( 'Product'[Product Name] )
&& NOT ( ISEMPTY ( Sales ) ),
VAR ProductsToRank = [TopN Value]
VAR topNTable =
CALCULATETABLE (
TOPN (
ProductsToRank,
ADDCOLUMNS ( VALUES ( 'Product'[Product Name] ), "#Amt", [Sales Amount] ),
[Sales Amount], ASC
),
FILTER ( ALLSELECTED ( Sales ), [Sales Amount] <> BLANK () )
)
RETURN
IF (
SELECTEDVALUE ( 'Product'[Product Name] )
IN SELECTCOLUMNS ( topNTable, "a", 'Product'[Product Name] ),
RANKX ( topNTable, [#Amt], [Sales Amount], ASC )
)
)

How to filter table after DAX UNION of other tables

How to filter table after UNION of other tables. How to reference new column name after SELECTCOLUMNS?
UNION (
SELECTCOLUMNS ( Tab1, "NewColor", Tab1[Color] ),
SELECTCOLUMNS ( Tab2, "NewColor", Tab2[Color] )
)
How can we filter this table?
I tried this:
CALCULATETABLE (
UNION (
SELECTCOLUMNS ( Tab1, "NewColor", Tab1[Color] ),
SELECTCOLUMNS ( Tab2, "NewColor", Tab2[Color] )
),
[NewColor] = "Red"
)
But it raises error:
Cannot find name '[NewColor]'
This should work:
Table =
FILTER (
UNION (
SELECTCOLUMNS ( Tab1, "NewColor", [Color] ),
SELECTCOLUMNS ( Tab2, "NewColor", [Color] )
),
[NewColor] = "Red"
)
Why you want to filter this table AFTER UNION, why not before?
You can do this in this way:
UNION(
FILTER(
SELECTCOLUMNS('Tab1',"__dummy[color]",[Region])
,__dummy[color]="Red"),
FILTER(
SELECTCOLUMNS('Tab2',"__dummy[color]",[Region])
,__dummy[color]="Green")
)
You can try this below option with Filter the source table first and then apply the UNION on filtered table-
UNION (
SELECTCOLUMNS ( FILTER(Tab1,Tab1[Color] = "Red"), "NewColor", Tab1[Color]),
SELECTCOLUMNS ( FILTER(Tab1,Tab2[Color] = "Red"), "NewColor", Tab2[Color] )
)
But here is a big question, as you are filtering Tab1 and Tab2 and keeping only color Red, this will keep only one value Red in your destination table. Is that really meaningful? If so, you can just create a Custom Table with 1 value Red. Why some other steps like UNION should use?