Power BI measure with condition - powerbi

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

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
```

Powerbi to return a 'NA' using the IF that is used in a std table

I have a code in a powerbi table that displays all. It basically counts how long a particular task is performed for mgt. It was done by someone else. I am trying to find out how to get the rows to display a 'No Data' or 'NA' if there is no data. I tried several approaches but could not get it to work. Any suggestions? Linda from Wisconsin.
TimeTaken =
//----CALCULATIONS
VAR DIFF = 'Opened'[Resolved] - 'Opened'[Opened]
VAR NumOfMinutes = DIFF * 24
* 60
VAR DAYS =
IF ( DIFF >= 1, INT ( DIFF ), BLANK () )
VAR HOURS =
INT ( ( DIFF - DAYS ) * 24 )
VAR MINUTES = NumOfMinutes
- ( DAYS * 24
* 60 )
- ( HOURS * 60 )
//---TEXTS
VAR DaysText =
IF ( DAYS >= 1, FORMAT ( DAYS, "00" ) & "days" )
//ELSE ("No Data") >>>>> this not working.. I deleted some
VAR HoursText =
FORMAT ( HOURS, "00" ) & "hrs"
VAR MinutesText =
FORMAT ( MINUTES, "00" ) & "mins"
RETURN
COMBINEVALUES ( " ", DaysText, HoursText, MinutesText )
Hi You can try below code. Just replace your last two line with this code.
RETURN
IF (
ISBLANK ( DaysText ) && ISBLANK ( HoursText )
&& ISBLANK ( MinutesText ),
"NA",
COMBINEVALUES ( " ", DaysText, HoursText, MinutesText )
)

POWER BI : Calculate the percentage excluding last 5 days

I am trying to compute the percentage of Free to Paid Sales conversions for a particular period.
Sales become paid after the threshold of 5 days. So Conversion % should exclude the last 5 days. Expected output is below.
Below are the measure I have created.
FreeSales : SUM( DATA[Free_Trials])
Conversions : SUM(DATA[Conversions])
Conv % : Calculate ( DIVIDE( FreeSales/Conversions,0), DATESBETWEEN(DATA[DATE], STARTDATE, ENDDATE-5))
(P.S: STARTDATE & ENDDATE are the min & max values from the date slicer)
Conv % is not working properly . It giving same value for all the rows in the table. Please help to fix this issue.
Thanks in advance!
You can create a calculated column with the below DAX formula.
Column2 =
VAR C = MAX ( Sheet1[Date] )
VAR RESULT =
IF (
( Sheet1[Date] ) = C,
0,
IF (
Sheet1[Date] = C - 1,
0,
IF (
Sheet1[Date] = C - 2,
0,
IF (
Sheet1[Date] = C - 3,
0,
IF (
Sheet1[Date] = C - 4,
0,
CALCULATE ( DIVIDE ( SUM ( Sheet1[Paid Sales] ), SUM ( Sheet1[Free Sales] ) ) )
)
)
)
)
)
RETURN
RESULT
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

wrong values with customized double header in matrix

I use a custom calculated table for the header which was an answer of a my previous question: https://stackoverflow.com/a/61469905/5950313
The measure AN is calculated within the following script:
The goal of the dimensionmeasure is to calculate the count of rows from fact_an cumul of 12 months where
Fact_AN[Vitesse_Transf_Mois]<=
SELECTEDVALUE(Dim_VieillissementAN[ID_Tranche])
AN =
VAR a = SELECTEDVALUE(Dim_DateFicheAgent[ID_DateFicheAgent])
VAR b =SELECTEDVALUE('Seniority banking'[banking seniority])
RETURN
CALCULATE(
COUNTROWS(FILTER(Fact_AN;
(Fact_AN[banking seniority]<=b && NOT ISBLANK (Fact_AN[banking seniority]))));
DATESBETWEEN (
Dim_DateFicheAgent[ID_DateFicheAgent];
NEXTDAY ( SAMEPERIODLASTYEAR (LASTDATE ( Dim_DateFicheAgent[ID_DateFicheAgent] ) ));
LASTDATE ( Dim_DateFicheAgent[ID_DateFicheAgent] )
))
the measure DimensionMeasure returns wrong values, it's almost the same value for all middle in the matrix.
How to correct it?
I use a star schema which means; I have only one fact table fact_an.
The table fact is linked to dim_produit by code_produit.
The description of the table dim_produit:
Codeproduit Dim5Rapport Dim6rapport
I try
Formules =
VAR Top1 = SELECTEDVALUE ( EnteteRapportAgentClient[Top] )
VAR Middle = SELECTEDVALUE ( EnteteRapportAgentClient[Middle] )
VAR BottomIndex = SELECTEDVALUE ( EnteteRapportAgentClient[Index3] )
VAR Val =
SWITCH (
TRUE ();
Top1 = "Nombre de leads"; [Lead]+ 0;
Top1 = "Affaires nouvelles" && BottomIndex <> 0; [AN]+0;
Middle = "Total AN";[AN]+ 0;
Middle = "Taux Transfo"; DIVIDE([AN];[Lead])
)
VAR ValF=
IF( Middle = "Taux Transfo";
FORMAT ( Val; "0.0%" );
FORMAT ( Val; "0" ))
VAR Val2=
IF (ValF="0";"";ValF
)RETURN Val2
But it returns always error. I put an example here https://drive.google.com/file/d/1i5HEnpoJ5mgEl98xUZzPFo7D6S0C-_tm/view?usp=drivesdk
The wrong values is for AN it returns the same value everywhere
here are the expected results:
The key here is that you need to pass the header context as filter context to your measure.
For example, instead of the line
Top1 = "Affaires nouvelles" && BottomIndex <> 0, [AN]+0,
You need to replace that [AN] with
CALCULATE ( [AN], Dim_Produit[Dim5Rapport] = Middle, Dim_Produit[Dim6Rapport] = Bottom )
where
VAR Middle = SELECTEDVALUE ( EnteteRapportAgentClient[Middle] )
VAR Bottom = SELECTEDVALUE ( EnteteRapportAgentClient[Bottom] )
The same goes for all of the different calculations. You need to set the filter context that you'd expect if the header were based on your actual dimensions rather than an artificial constructed header.

If it is the first date or last date use one aggregation; if any other day, use another aggregation

Let's assume I have the below dataset.
What I need to create the below matrix where if it is the beginning or month end, I aggregate A or B in Category 1 and calculate SUM but if it is any other day in a month but 1st or last, I am tagging A or B in Category 2 and calculate SUM. I guess I need to use SWITCH, don't I?
Edit in info from comments
Like to create 3 col:
isStart = IF ( main_table[date] = STARTOFMONTH ( main_table[date] ), 1, 0 )
isEnd = IF ( main_table[date] = ENDOFMONTH ( 'main_table'[date] ), 1, 0 )
in_between_date =
IF ( AND ( main_table[date] <> ENDOFMONTH ( 'main_table'[date] ),
main_table[date] <> STARTOFMONTH ( main_table[date] ) ), 1, 0 )
Then, create the columns with my categories, like
start_end =
IF ( OR ( NOT ( ISERROR ( SEARCH ( "A", main_table[code] ) ) ),
main_table[code] = "B" ),
"Category 1",
BLANK () )
and
in_between =
IF ( OR ( main_table[code] = "B", main_table[code] = "A" ), "Category 2", BLANK () )
But then, what should I use in switch/if ? = if(VALUES('main_table'[isStart]) = 1, then what?
You where on the right track but overcomplicated a bit. You only need one extra column "Category" giving for each row in what category the item falls.
Category =
IF (
startEnd[date] = STARTOFMONTH ( startEnd[date] )
|| startEnd[date] = ENDOFMONTH ( startEnd[date] );
"Category1";
"Category2"
)
table end result is: