PowerBi calculating date only if another column has a value DAX - powerbi

Trying to utilize powerbi to help me calculate a date. Currently it is calculating properly if the column "spend_start_Result" has a value, but if this column has no value it is still calculating and giving me a date of 12/30/1899. Is there something wrong with my DAX code?
Arch_review_Calc =
IF (
Projects[spend_start_Result] = BLANK (),
0,
IF (
Projects[Complexity] = "Low",
Projects[spend_start_Result] - 45,
IF (
Projects[Complexity] = "Medium",
Projects[spend_start_Result] - 60,
IF ( Projects[Complexity] = "High", Projects[spend_start_Result] - 90, 0 )
)
)
)
I would like the Arch_review_calc column to be blank in that row if the spend_start_result column is blank in that row. Instead it is still calculating and I am unsure where I'm going wrong.

Your code replaces blanks with zeros, which are formatted as dates. To avoid that, instead of zeros use BLANK() function.
I would re-write your formula as follows:
Arch_review_Calc =
IF (
ISBLANK ( Projects[spend_start_Result] ), BLANK (),
SWITCH (
TRUE,
Projects[Complexity] = "Low", Projects[spend_start_Result] - 45,
Projects[Complexity] = "Medium", Projects[spend_start_Result] - 60,
Projects[Complexity] = "High", Projects[spend_start_Result] - 90,
BLANK ()
)
)
I am not sure about the last blank (inside the SWITCH statement) - if you want 0 instead of blank, replace BLANK() with 0.

Related

Count Specific Word by Row in DAX

I'm trying to count the number of times the word "text" appears per row in Power BI. I've done a lot of google searching and seen examples like this:
Formula :=
CALCULATE (
COUNTROWS ( FILTER ( 'TestData', FIND ( "text", 'TestData'[Description],, 0 ) > 0 ) ),1=1
)
but it isn't quite getting me there. How can I get for row 1, a result of 1 and row 2, a result of 3.
CREATE TABLE [dbo].[TestData](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Description] [varchar](100) NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[TestData] ON
GO
INSERT [dbo].[TestData] ([ID], [Description]) VALUES (1, N'this is my demo text')
GO
INSERT [dbo].[TestData] ([ID], [Description]) VALUES (2, N'text text demo text')
GO
SET IDENTITY_INSERT [dbo].[TestData] OFF
GO
Expected Result
ID Description Text Word Count
1 this is my demo text 1
2 text text demo text 3
Calculated Column:
=
VAR MySearchText = "text"
RETURN
DIVIDE(
LEN( Table1[Description] )
- LEN( SUBSTITUTE( Table1[Description], MySearchText, "" ) ),
LEN( MySearchText )
)
Measure:
=
VAR MySearchText = "text"
VAR ThisDescription =
MIN( Table1[Description] )
RETURN
DIVIDE(
LEN( ThisDescription )
- LEN( SUBSTITUTE( ThisDescription, MySearchText, "" ) ),
LEN( MySearchText )
)
though note that both of these will return positive counts where MySearchText is found within other words: a description of "this is textual", for example, will return 1.
I don't believe DAX has a textsplit function, but you can do something like this to ensure you don't pick up words of which text is a substring.
Text Count (DAX) =
VAR pad = SUBSTITUTE(" " & [Description] & " ","text","~text~")
VAR lenPad = LEN(pad)
VAR lenText = LEN("~text~")
VAR lenRemText = LEN(SUBSTITUTE(pad,"~text~",""))
RETURN (lenPad-lenRemText)/lenText
```

Power BI measure with condition

It's a silly power BI question, but I can't figure it out using measures.
This is a condition that I'd like to convert into a measure.
Condition:
i.) When there is no flight time for direction 'APAC' AND the first character in the code is 'V', will return '0', otherwise '1'.But other than code start with 'V', with or without date, will return blank.
ii.) When there is no flight time for direction 'AMER', will return '0'
iii.) Direction 'EMEA' , with or without time flight, will return blank
Expected result:
Attached with pbix: https://drive.google.com/file/d/1PEkk4PX37H4w8_1c6Dcl_TpEUa8IpEmW/view?usp=sharing
Appreciate any helps provided !
Try this one as a calculated column:
Result =
SWITCH(
TRUE(),
FlightTable[Direction] = "APAC",
IF(
LEFT(FlightTable[Code], 1) = "V",
IF (FlightTable[Flight Time] = BLANK(), 0, 1),
BLANK()
),
FlightTable[Direction] = "APAC",
IF(LEFT(FlightTable[Code], 1) = "V", 0, BLANK()),
FlightTable[Direction] = "AMER",
IF( FlightTable[Flight Time] = BLANK(), 0, 1),
FlightTable[Direction] = "EMEA", BLANK(),
BLANK()
)
the solution should be as follows
measue= Switch ( True(),
if(condition1,"Result1" ,
if(condition1,"Result1" ,
etc... )
this should give you what you need, because Switch in DAX acts as if & else if
Here is a measure you can use, based on your stated logic.
Use SWITCH with TRUE as the first argument. The first SWITCH predicate that returns TRUE will be used as the result for each row.
Result =
VAR _dir = SELECTEDVALUE ( 'Table'[Direction] )
VAR _code = SELECTEDVALUE ( 'Table'[Code] )
VAR _time = SELECTEDVALUE ( 'Table'[Flight Time] )
VAR _out =
SWITCH (
TRUE ,
_time = BLANK () && _dir = "AMER" , 0 , // If direction is AMER with no flight time, return 0
_dir = "EMEA" , BLANK () , // If direction is EMEA, return blank with or without flight time
_dir = "APAC" && LEFT(_code, 1) <> "V" , BLANK () , // If direction is APAC but code does not start with V, return blank
_time = BLANK () && _dir = "APAC" && LEFT(_code,1) = "V" , 0 , // When there is no flight time for APAC, and code starts with V, return 0
1 // Else 1
)
RETURN
_out

Slicer with few TOP N options // COUNT

I'm having problems with implementing TOP N slicer into my dashboard. I would like to have a single select TOP 5, TOP 10, TOP 20 and TOP 30 slicer which will show items with the biggest count.
I partially managed to achieve this and it works while I have only one column added to the y-axis (I use horizontal bar chart) and my measure to the x-axis but when I add another column to legend field everything falls apart.
These are my measures:
TopN_test =
VAR Selected_Top = SELECTEDVALUE('Top N'[Select Top N])
RETURN
SWITCH(TRUE(),
Selected_Top = 0,[count_all_current],
RANKX(
ALLSELECTED(Skills[Skill correct]),
[count_all_current]
)
<= Selected_Top,
[count_all_current]
)
count_all_current =
CALCULATE(
COUNT('Skills'[Skill correct]),
FILTER('Skills', 'Skills'[DateIndex] = 6),
FILTER('Skills', 'Skills'[Proficiency]<>"-")
)
I will be grateful for any advice how to adjust it.
I added a calculate and Selected_Top Variable as a filter. Can you please check if this code works for you.
TopN_test =
SWITCH (
TRUE (),
Selected_Top = 0, [count_all_current],
RANKX ( ALLSELECTED ( Skills[Skill correct] ), [count_all_current] ) <= Selected_Top, CALCULATE ( [count_all_current], Selected_Top )
)

DAX PowerBI: Replaced blank values with zero and issue with chart

I tried to create report in Power BI with sales month by month for last 20 months, when sales is blank I want to see month with 0 value.
I decided to change Blank() values with zero adding a 0 at the end of calculation.
It works great, however I have an issue with limitaton date hierarchy, because now my chart contains a lot of months without value (first value begins in 2017, date hierarchy first value begins in 2000).
Test:=
CALCULATE (
SUM( quantity ),
flag = 1,
title = "WEEKS"
) + 0
Instead of a plain 0, you could add an IF to specify to only add that after the first value. Something like this:
Test:=
VAR FirstDate = CALCULATE ( MIN ( date ), ALL( Dates ), quantity > 0 )
RETURN
CALCULATE (
SUM( quantity ),
flag = 1,
title = "WEEKS"
) + IF( date > FirstDate, 0 )
If the condition is false, the IF returns a blank and it shouldn't show up.

Calculation Error: A table of multiple values was supplied where a single value was expected

I want to show this year store count based on to date and from date.The following code is throwing this error when i am placing this column in table.
ThisYearStoreCount =
IF (
[DateDiff] > 365,
0,
IF (
DATESBETWEEN ( WUSA_CAL_DIM[End_Date], [From_Date], [To_Date] ),
DISTINCTCOUNTNOBLANK ( WUSA_STORE_DIM[Store Code] )
)
)
#RADO is spot on,
If I understand your logic, something like this should work.
ThisYearStoreCount =
IF (
[DateDiff] > 365,
0,
IF (
[From_Date] >= WUSA_CAL_DIM[End_Date]
&& [To_Date] <= WUSA_CAL_DIM[End_Date]
&& WUSA_STORE_DIM[Store Code] <> BLANK(),
1
)
)
That error usually means that you're using a function that returns a set of data, in a column context ( column contexts only allow a single value to exist per row ).
I'm reading between the lines a little bit, but the new column formula above will check if the end date is between the From_Date and To_Date before allowing the number 1 through.
Then you can just add the ThisYearStoreCount to any visual and it will sum wherever appropriate.
Your existing code should work as a measure ( instead of a column ) I think but it's impossible to tell without more information.