DAX: Performance Issue due to Switch Function - powerbi

I am having serious performance issue due to this measure. Anyone could help please to have an alternative that might not make the report slow? Just to let you know I am new to dax. thanks :slightly_smiling_face:
Measure_NightMare =
VAR caldate =
SELECTEDVALUE ( 'Calendar'[DayOfWeek] )
RETURN
SWITCH (
TRUE (),
caldate = 6,
CALCULATE (
SUMX ( 'Fact_TBL', 'Fact_TBL'[Col1] )
- SUMX ( 'Fact_TBL', 'Fact_TBL'[Col2] ),
'Fact_TBL'[Value_Col] >= 40
&& 'Fact_TBL'[Value_Col] <= 74
),
caldate = 7,
BLANK ()
,
CALCULATE (
SUMX ( 'Fact_TBL', 'Fact_TBL'[Col1] )
- SUMX ( 'Fact_TBL', 'Fact_TBL'[Col2] ),
'Fact_TBL'[Value_Col] >= 38
&& 'Fact_TBL'[Value_Col] <= 80
)
)
Tried of creating more VARs may be the syntax was incorrect.

Do you have a proper star schema?
You're iterating your fact table multiple times which isn't wise.
Try this.
Measure_SlightlyBetter =
VAR caldate =
SELECTEDVALUE ( 'Calendar'[DayOfWeek] )
RETURN
SWITCH (
TRUE (),
caldate = 6,
CALCULATE (
SUMX ( 'Fact_TBL', 'Fact_TBL'[Col1] - 'Fact_TBL'[Col2]),
'Fact_TBL'[Value_Col] >= 40
&& 'Fact_TBL'[Value_Col] <= 74
),
caldate = 7,
BLANK ()
,
CALCULATE (
SUMX ( 'Fact_TBL', 'Fact_TBL'[Col1] - 'Fact_TBL'[Col2]),
'Fact_TBL'[Value_Col] >= 38
&& 'Fact_TBL'[Value_Col] <= 80
)
)

Related

PowerBI RANKX is not continious

I want to show the TOP10 difference in a measure.
The difference is calculated YTD actual + Rest of the year forecast - Full year budget values.
The normal measure looks like this:
VAR _Year =
SELECTEDVALUE ( 'Calendar'[Year] )
RETURN
(
CALCULATE (
SELECTEDMEASURE (),
DATESYTD ( Calendar[Dates] ),
CRDB[Scenario] = "Actual",
ALL ( CRDB[ForecastTypeFinal] )
)
+ CALCULATE (
SELECTEDMEASURE (),
CRDB[Scenario] = "Forecast",
'Calendar'[Dates] >= DATE ( _Year, 1, 1 )
&& 'Calendar'[Dates] <= DATE ( _Year, 12, 31 )
)
)
- CALCULATE (
SELECTEDMEASURE (),
Calendar[Dates] >= DATE ( _Year, 1, 1 )
&& Calendar[Dates] <= DATE ( _Year, 12, 31 ),
CRDB[Scenario] = "Budget",
ALL ( CRDB[ForecastTypeFinal] )
)
I would like to rank by project, so I made this ranking measure:
RANKX (
ALL ( CRDB[Project ID - Project ID Level 01 (Text)] ),
(
CALCULATE (
SELECTEDMEASURE (),
DATESYTD ( Calendar[Dates] ),
CRDB[Scenario] = "Actual",
ALL ( CRDB[ForecastTypeFinal] )
)
+ CALCULATE (
SELECTEDMEASURE (),
CRDB[Scenario] = "Forecast",
'Calendar'[Dates] >= DATE ( SELECTEDVALUE ( 'Calendar'[Year] ), 1, 1 )
&& 'Calendar'[Dates] <= DATE ( SELECTEDVALUE ( 'Calendar'[Year] ), 12, 31 )
)
)
- CALCULATE (
SELECTEDMEASURE (),
Calendar[Dates] >= DATE ( SELECTEDVALUE ( 'Calendar'[Year] ), 1, 1 )
&& Calendar[Dates] <= DATE ( SELECTEDVALUE ( 'Calendar'[Year] ), 12, 31 ),
CRDB[Scenario] = "Budget",
ALL ( CRDB[ForecastTypeFinal] )
),
,
DESC
))
The ranking gets me the correct projects, but when I look at the rank values, they are not right:
ranking values
The values are not the same, so it is no reason for skipping places.
What do I do wrong?
Thank you for your help in advance.

EARLIER Function not working in DAX - ATR of Stocks

I am making ATR34 column in PowerBI in which I have a table that consists of Stocks, Date, High, Low, Close, PrevClose and Series (contains Equity,future).
I have made a calculated column of ATR from the High Low Close and Prev Close. I need to calculate the MA of 34 days of ATR calculated filtered by Stocks and Equity.
For that I am first calculating the Daynumber filtering Stocks and EQ; then 34 days and making an average of it.
However when i am using the below mentioned code in DAX EARLIER function is not working and i am unable to calculate it.
I am beginner in POWERBI.
DayNumber =
COUNTROWS (
FILTER ('Table','Table'[Date] <= EARLIER ( 'Table'[Date] )),
FILTER('Table'[Stock] = EARLIER ( 'Table'[Stock] )),
FILTER('Table'[EQ] = EARLIER ( 'Table'[EQ] ))
)
Another Caulcated Measured Column :
FirstDateOfRange34 =
CALCULATE (
VALUES ( 'Table'[Date] ),
FILTER ( 'Table','Table'[DayNumber] = EARLIER ( 'Table'[DayNumber] ) - 34),
FILTER('Table'[Stock] = EARLIER ( 'Table'[Stock] )),
FILTER('Table'[EQ] = EARLIER ( 'Table'[EQ] ))
)
Another Calculated Measured Column :
ATR34 =
CALCULATE (
AVERAGE ( 'Table'[ATR] ),
FILTER ( 'Table','Table'[Date] >= EARLIER ( 'Table'[FirstDateOfRange34] ))
FILTER ('Table','Table'[Date] <= EARLIER ( 'Table'[Date] ))
FILTER('Table'[Stock] = EARLIER ( 'Table'[Stock] ))
FILTER('Table'[EQ] = EARLIER ( 'Table'[EQ] ))
)
Your syntax is off. COUNTROWS expects a single table argument, not three arguments and FILTER needs a table for the first argument, not a column.
Try this intead:
DayNumber =
COUNTROWS (
FILTER (
'Table',
'Table'[Date] <= EARLIER ( 'Table'[Date] )
&& 'Table'[Stock] = EARLIER ( 'Table'[Stock] )
&& 'Table'[EQ] = EARLIER ( 'Table'[EQ] )
)
FirstDateOfRange34 =
CALCULATE (
VALUES ( 'Table'[Date] ),
FILTER (
'Table',
'Table'[DayNumber] = EARLIER ( 'Table'[DayNumber] ) - 34
&& 'Table'[Stock] = EARLIER ( 'Table'[Stock] )
&& 'Table'[EQ] = EARLIER ( 'Table'[EQ] )
)
)
ATR34 =
CALCULATE (
AVERAGE ( 'Table'[ATR] ),
FILTER (
'Table',
'Table'[Date] >= EARLIER ( 'Table'[FirstDateOfRange34] )
&& 'Table'[Date] <= EARLIER ( 'Table'[Date] )
&& 'Table'[Stock] = EARLIER ( 'Table'[Stock] )
&& 'Table'[EQ] = EARLIER ( 'Table'[EQ] )
)
)

Intermittent error: DAX expression with SSAS on prem server

I am currently working at updating Tabular model hosted on-prem on a SASS server version 13.0. The tabular model is version 1200.
We have complex logic created in our measures that allow us to define counting rules or to filter depending on parameters selected in disconnected tables.
The model was created using snowflake ‘like’ schema: It can be simplified as below
Link to image: https://ibb.co/WfkmNPV
The error returned by DAX Studio or PowerBI (on an intermittent basis) is the following:
MdxScript(Model) (5174, 18) Calculation error in measure 'Admission'[Number of Clients]: Function 'CONTAINS' does not support comparing values of type Text with values of type Integer. Consider using the VALUE or FORMAT function to convert one of the values.
Sometimes error is resolving by itself without any doing from our end or (sometimes) by reprocessing the model.
In some instances, all our measures are returning this error. In other instances, it’s only a few of them.
The DAX expression for 'Admission'[Number of Clients] is as below:
Admission[Number of Clients] :=
VAR MinDate = MIN ( 'Date'[Full Date] )
VAR MaxDate = MAX ( 'Date'[Full Date] )
VAR AdmissionDate =
FILTER (
Admission,
SWITCH (
TRUE (),
VALUES ( 'Counting Rules'[Counting Rule] ) = "Starts",
Admission[Start Date] >= MinDate && Admission[Start Date] <= MaxDate,
VALUES ( 'Counting Rules'[Counting Rule] ) = "Ends",
Admission[End Date] >= MinDate && Admission[End Date] <= MaxDate,
VALUES ( 'Counting Rules'[Counting Rule] ) = "Active",
Admission[Start Date] <= MaxDate && Admission[End Date] >= MinDate,
0
)
)
RETURN
IF (
COUNTROWS ( VALUES ( 'Counting Rules'[Counting Rule] ) ) = 1,
CALCULATE (
DISTINCTCOUNT ( 'Admission'[ClientKey] ),
FILTER (
AdmissionDate,
IF ( RELATED ( 'Clients'[Date Of Birth] ) <= MAX ( 'Date'[Full Date] )
&& ( ISFILTERED ( 'Client Age'[Age Band] ) || ISFILTERED ( 'Client Age'[Age] ) ),
IF ( COUNTROWS ( VALUES ( 'Age Counting Rule'[Age Counting Rule] ) ) = 1,
INTERSECT (
VALUES ( 'Clients Age'[Age] ),
SELECTCOLUMNS (
ADDCOLUMNS (
VALUES ( Admission[ClientKey] ),
"Age",
ROUNDDOWN (
DATEDIFF (
CALCULATE ( MAX ( 'Clients'[Date Of Birth] ) ),
VAR AdmissionDateFiltered =
IF (
VALUES ( 'Age Counting Rule'[Age Counting Rule] ) = "Age Min",
CALCULATE ( MIN ( Admission[Start Date] ), AdmissionDate, EARLIER ( Admission[ClientKey] ) = Admission[ClientKey] ),
CALCULATE ( MAX ( Admission[End Date] ), AdmissionDate, EARLIER ( Admission[ClientKey] ) = Admission[ClientKey] )
)
RETURN
IF ( VALUES ( 'Age Counting Rule'[Age Counting Rule] ) = "Age Min",
IF ( AdmissionDateFiltered < MIN ( 'Date'[Full Date] ) && NOT ISBLANK ( AdmissionDateFiltered ),
MIN ( 'Date'[Full Date] ),
AdmissionDateFiltered
),
IF ( AdmissionDateFiltered > MAX ( 'Date'[Full Date] ) && NOT ISBLANK ( AdmissionDateFiltered ),
MAX ( 'Date'[Full Date] ),
AdmissionDateFiltered
)
),
DAY
) / 365.25,
0
)
),
"Age", [Age]
)
),
BLANK ()
),
TRUE ()
) //GOM Status
&& IF (
ISFILTERED ( 'Guardianship Status'[Guardianship Type] ) || ISFILTERED ( 'Guardianship Status'[Guardianship Type Description] ) || ISFILTERED ( 'Guardianship Status'[Under Guardianship] ),
CONTAINS (
VALUES ( 'Guardianship Status'[Guardianship Type] ),
'Guardianship Status'[Guardianship Type],
[Child Protection Order Active]
),
TRUE ()
)
)
),
BLANK ()
)
The measure [Child Protection Order Active] in the GOM part of the expression is returning a string value.
The modification I made to the logic is how the Age is calculated.
Have any of you encountered this error?
How would you go by debugging something like that?
Thank you for you time in helping me.
I have resolved my issue by adding a condition around [Child Protection Order Active] to check that it doesn't return blanks.
Measure is always returning a string (at least I assume considering the code) but for some odd reason, the engine think it may return blank (another assumption).
Blank value is considered as integer and therefore returns an error with the above initial DAX expression.

Out of range percentile values using percentile.exc in powerbi

I am new to PowerBi and would need some help figuring out how to make the percentile.exc function work (I cannot use percentile.inc).
I systematically receive the following error: The percentile value should be in the range 1/(N+1)..N/(N+1) inclusive, where N is the number of data values.
The objective of this code is to compute the 3months rolling 90th percentile of a given ratio in another calculated column.
I would be very grateful for any help!
This is what I have come with up so far:
3MonthsRolling_90thPercentile=
VAR current_date = Table1[Date]
VAR k = 0.9
VAR NumValues =
COUNT (Table1[Ratio] )
VAR LowerBound = 1 / ( NumValues + 1 )
VAR UpperBound = NumValues / ( NumValues + 1 )
RETURN
CALCULATE (
IF (AND ( PERCENTILE.EXC ( Table1[Ratio],0.9) >= LowerBound, PERCENTILE.EXC (Table1[Ratio],0.9) <= UpperBound ),PERCENTILE.EXC ( Table1[Ratio],0.9), BLANK()),
FILTER (
ALL (Table1 ),
Table1[Date]
>= DATE ( YEAR ( current_date ), MONTH ( current_date ) - 3, DAY ( current_date ) )
&& Table1[Date] <= current_date
)
)
If NumValues is less than 9 in, then 0.9 is above your upper bound NumValues / ( NumVales + 1 ), so PERCENTILE.EXC ( Table1[Ratio], 0.9 ) will throw an error.
I think your measure would make more sense as follows:
3MonthsRolling_90thPercentile =
VAR current_date = Table1[Date]
VAR DateFiltered =
FILTER (
ALL ( Table1 ),
Table1[Date]
>= DATE (
YEAR ( current_date ),
MONTH ( current_date ) - 3,
DAY ( current_date )
)
&& Table1[Date] <= current_date
)
VAR k = 0.9
VAR NumValues = CALCULATE ( COUNT ( Table1[Ratio] ), DateFiltered )
VAR LowerBound = 1 / ( NumValues + 1 )
VAR UpperBound = NumValues / ( NumValues + 1 )
RETURN
IF (
AND ( k >= LowerBound, k <= UpperBound ),
CALCULATE ( PERCENTILE.EXC ( Table1[Ratio], k ), DateFiltered )
)
Note that I'm comparing k to the bounds rather than the k-percentile.

Tool recommendation for data transform

I have large amounts of raw fault data in Power BI.
code time status
x123 2019-04-22T23:57:00 ok
x123 2019-04-23T01:00:00 faulty
x123 2019-04-23T02:00:00 ok
x123 2019-04-23T23:00:00 faulty
x123 2019-04-24T01:00:00 ok
I need to transform this to show how long an item has been in a faulty state on a given day. So on the 23rd, the item was in a faulty state between 1 and 2a.m and then again between 11pm until past midnight.
code day % of day faulty
x123 23/04/2019 8.30% (2 hours)
Can I do this easily in Power BI or should I use another tool such as Azure Data Factory?
Add the following Calculated Columns to your table:
Report Date = Table1[time].[Date]
Fault Duration =
VAR CurrentTime = Table1[time]
VAR CurrentCode = Table1[code]
VAR PreviousTime =
CALCULATE (
MAX ( Table1[time] ),
FILTER (
Table1,
Table1[time] < CurrentTime &&
Table1[code] = CurrentCode
)
)
VAR NextTime =
CALCULATE (
MIN ( Table1[time] ),
FILTER (
Table1,
Table1[time] > CurrentTime &&
Table1[code] = CurrentCode
)
)
VAR FaultyFrom =
IF(
Table1[status] = "faulty",
Table1[time],
IF (
DAY(PreviousTime) = DAY(Table1[time]),
BLANK(),
Table1[time].[Date]
)
)
VAR FaultyTo =
IF (
Table1[status] = "ok",
Table1[time],
IF (
DAY(NextTime) = DAY(Table1[time]),
NextTime,
Table1[time].[Date] + 1
)
)
RETURN
IF(
ISBLANK ( PreviousTime ) || ISBLANK ( NextTime ) || ISBLANK ( FaultyFrom ),
BLANK(),
FaultyTo - FaultyFrom
)
Now create measures:
Faulty Hours = SUM ( Table1[Fault Duration] )
Faulty % Day =
IF (
HASONEVALUE ( Table1[Report Date] ),
DIVIDE (
[Faulty Hours],
DISTINCTCOUNT ( Table1[code] ),
BLANK()
),
BLANK()
)
Output:
See https://pwrbi.com/so_55825688/ for a worked example PBIX file