{ FIXED [ID] : MIN(
IF [Temp] >= 0 AND [Temp] <= 35.2 THEN [Value]
END
)}
How to write the same calculation in DAX
You can try this:
FIXED[ID] =
MINX ( FILTER ( YourTable, [Temp] >= 0 && [Temp] <= 35.2 ), [Value] )
Related
I have the need of creating a cumulative measure that resets when a certain condition happens.
The target is to create the 'Longest Stock Out Period' measure. That will be inserted in a matrix visual like this.
Date
No. of Negative Days
Longest Stock Out Period
08-03-2022
0
0
09-03-2022
1
1
10-03-2022
1
2
11-03-2022
0
0
The logic is that 'Longest stock out period' should cumulative sum 'No. of negative days' until 'No. of negative days' is 0, then it should reset.
This is what I have currently tried, which just computes a 0. I believe there should also be some logic in the measure regarding no. of negative days should not be 0 or alike.
Longest Stock Out =
VAR _date =
SELECTEDVALUE ( 'Calendar'[Date] )
RETURN
CALCULATE (
[No of Days],
FILTER ( ALL ( 'Calendar'[Date] ), 'Calendar'[Date] <= _date )
)
Try this Measure:
Longest Stock Out Period :=
VAR ThisDate =
MIN( 'Table'[Date] )
VAR DateOfLatestZero =
CALCULATE(
MAX( 'Table'[Date] ),
FILTER(
ALL( 'Table' ),
'Table'[Date] <= ThisDate
&& 'Table'[No. of Negative Days] = 0
)
)
RETURN
CALCULATE(
SUM( 'Table'[No. of Negative Days] ),
FILTER(
ALL( 'Table' ),
'Table'[Date] <= ThisDate
&& 'Table'[Date] >= DateOfLatestZero
)
)
Adapt so as to use your Calendar dates as required.
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.
I want the values in between the years mentioned in the image and get their equivalent sales qty.
The sample data is:
Sample Data
For ex: If I select 2005 in the slicer, I should get the qty ordered as 4+5+6 =15 in the new column or as a measure.
Try with this code:
BetweenRange =
CALCULATE (
SUM ( 'Table'[Sales Qty] ),
FILTER (
ALL ( 'Table'[Start Year], 'Table'[End Year] ),
SELECTEDVALUE ( 'YourFilterTable'[Year] ) >= 'Table'[Start Year]
&& SELECTEDVALUE ( 'YourFilterTable'[Year] ) <= 'Table'[End Year]
)
)
I'd suggest using a variable to read in the slicer value:
SumQuantity =
VAR SelectedYear = SELECTEDVALUE ( Slicer[Year] )
RETURN
CALCULATE (
SUM ( Sales[Sales qty] ),
Sales[Start year] <= SelectedYear,
Sales[end Year] >= SelectedYear
)
I have a measure in which i am dividing total number of contractual months from total number of months. I am getting correct result, but the total in the bottom is not correct.
The first column is a unique ID, Third and fourth columns are numerator and denominator, Second column is the result of the division, I want to count those IDs, where the division is between 0.75 and 1.00
Here are my calculations
Var Check=DIVIDE([Month of Engagement],[Months In Contract L30])
RETURN
IF(HASONEVALUE('Fact - TABLE'[ID]),IF(Check>=0.75 && Check<=1.00,DISTINCTCOUNT(ID),0),SUMX('Fact - TABLE',IF(Check>=0.75 && Check<=1.00,DISTINCTCOUNT(ID),0)))
Please let me know, how to solve this.
For Total, you should change 3th parameter of IF to:
CALCULATE (
SUMX (
CALCULATETABLE (
tab,
FILTER (
ALL ( tab ),
VAR __Check =
DIVIDE ( 'Tab'[Month of Engagement], Tab[Months In Contract L30] )
RETURN
__Check < 1
&& __Check > 0.75
)
),
1
)
)
Below my test example (for testing only I have put check variable in first IF
IF ( Check >= 0.75 && Check <= 1.00, Check, 0 ) to see if check calculation is correct):
Measure =
VAR Check =
DIVIDE (
SUM ( 'Tab'[Month of Engagement] ),
SUM ( Tab[Months In Contract L30] )
)
RETURN
IF (
HASONEVALUE ( 'Tab'[ID] ),
IF ( Check >= 0.75 && Check <= 1.00, Check, 0 ),
CALCULATE (
SUMX (
CALCULATETABLE (
tab,
FILTER (
ALL ( tab ),
VAR __Check =
DIVIDE ( 'Tab'[Month of Engagement], Tab[Months In Contract L30] )
RETURN
__Check < 1
&& __Check > 0.75
)
),
1
)
)
)
Measure =
VAR Check =
DIVIDE (
[Month of Engagement] ,
[Months In Contract L30]
)
RETURN
IF (
HASONEVALUE ( 'Tab'[ID] ),
IF ( Check >= 0.75 && Check <= 1.00, Check, 0 ),
CALCULATE (
SUMX (
CALCULATETABLE (
'Tab',
FILTER (
ALL ( 'Tab' ),
VAR __Check =
DIVIDE ( [Month of Engagement], [Months In Contract L30] )
RETURN
__Check >=0.75
&& __Check <=1.00
)
),
1
)
)
)
Here are the brief definitions
Month of Engagement:= CALCULATE(COUNTROWS(
SUMMARIZE('table','table'[MONTH_OF_ENGAGEMENT],"Count",DISTINCT('table'[MONTH_OF_ENGAGEMENT]))),'table'[FREQUENCY_FLAG]="Y")+0
Here MONTH_OF_ENGAGEMENT is value extracted from a date as YYYYMM
Months In Contract L30:=
Var DBRefreshDate30= [Database Refresh Date]-30
RETURN
DATEDIFF(DBRefreshDate30,[Database Refresh Date],MONTH)+1
Now basis on dividing Month of Engagement from Months In Contract L30, i am deriving a percentage, and checking if that percentage lies between 0.75 and 1.00
I have the below table and trying to move from excel to Power Bi. In excel I use =COUNTIFS($A$2:$A$16,"<="&E2,$B$2:$B$16,">="&E2) to get Count value but I wonder how can I calculate it in Power BI
open_date close_date Date Count
16-Sep-18 14-Jan-19 16-Sep-18 1
21-Sep-18 19-Jan-19 17-Sep-18 1
23-Sep-18 21-Jan-19 18-Sep-18 1
17-Jan-19 27-Jan-19 19-Sep-18 1
26-Jan-19 28-Jan-19 20-Sep-18 1
27-Jan-19 28-Jan-19 21-Sep-18 2
19-Jan-19 19-Jan-19 22-Sep-18 2
19-Jan-19 29-Jan-19 23-Sep-18 3
27-Jan-19 29-Jan-19 24-Sep-18 3
20-Jan-19 30-Jan-19 25-Sep-18 3
23-Jan-19 30-Jan-19 26-Sep-18 3
26-Jan-19 30-Jan-19 27-Sep-18 3
28-Jan-19 30-Jan-19 28-Sep-18 3
21-Jan-19 31-Jan-19 29-Sep-18 3
25-Jan-19 31-Jan-19 30-Sep-18 3
There are multiple ways to do this, but they'll all use some sort of filtering.
Here are a couple examples:
CountIf =
COUNTROWS (
FILTER (
ALL ( Table1 ),
Table1[open_date] <= MAX ( Table1[Date] ) &&
Table1[close_date] >= MAX ( Table1[Date] )
)
)
and
CountIf =
VAR CurrentDate =
MAX ( Table1[Date] )
RETURN
CALCULATE (
COUNT ( Table1[Date] ),
ALL ( Table1 ),
Table1[open_date] <= CurrentDate,
Table1[close_date] >= CurrentDate
)