EARLIER Function not working in DAX - ATR of Stocks - powerbi

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] )
)
)

Related

Countrows/calculate/sum rows in DAX

I am fairly new with Power BI and DAX and I'm stuck. I'll try to explain the current situation and what I want to become my output. I've tried a lot of meaures with distinctcount, calculate, you name it, I did it. But can't find the right solution.
We've got 4 columns: Date, Employee_ID, Sick, %FTE. Every row records if an employee was sick on that date. Blank is not sick and Y = sick.
I would like to create a measure where it counts the %FTE just once when an employee is sick in a particular week, month or year.
So the output of January should be 2,13 (0,8 + 0,33 + 1) and in February 1,8 (0,8 + 1).
enter image description here
You would need two additional columns in the dataset as following
Once you have that, you can use the following measures to reach the goal
Measure8 =
VAR _1 =
IF (
MAX ( 'fact'[sick] ) <> BLANK (),
RANKX (
FILTER (
ALL ( 'fact' ),
'fact'[emp_id] = MAX ( 'fact'[emp_id] )
&& 'fact'[Year] = MAX ( 'fact'[Year] )
&& 'fact'[Month] = MAX ( 'fact'[Month] )
&& 'fact'[sick] = "Y"
),
CALCULATE ( MAX ( 'fact'[date] ) ),
,
ASC,
DENSE
)
)
VAR _2 =
IF ( _1 = 1, IF ( MAX ( 'fact'[sick] ) = "y", MAX ( 'fact'[%FTE] ) ) )
RETURN
_2
Measure9 =
IF (
HASONEVALUE ( 'date'[date] ),
[Measure8],
VAR _1 =
MAXX (
GROUPBY (
ADDCOLUMNS ( 'fact', "val", [Measure8] ),
[Year],
[Month],
"total", SUMX ( CURRENTGROUP (), [val] )
),
[total]
)
VAR _2 =
MAXX (
GROUPBY (
ADDCOLUMNS ( 'fact', "val", [Measure8] ),
[Year],
"total", SUMX ( CURRENTGROUP (), [val] )
),
[total]
)
VAR _3 =
IF ( ISINSCOPE ( 'fact'[Year] ), _2, _1 )
RETURN
_3
)
Also, for any future posts please provide the sample data and expected output as markdown tables How To

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.

Power BI Dax Group By Measure - Display Dates Before Today

I am trying to create a measure that will show booked revenue over time, grouped by a few categories. I also want to show as 0 within line charts when the data is BLANK(), so I have it in an IF statement. Everything works just fine except for whatever reason I cannot get it to only show dates prior to today.
Here is the current Measure:
M_BookedRevenue =
VAR CurrentAccountingYear =
CALCULATE ( MIN ( 'Date'[AccountingYear] ), 'Date'[Date] = TODAY () )
VAR CurrentDate =
CALCULATE ( MIN ( 'Date'[SortDate] ), 'Date'[Date] = TODAY() )
RETURN
CALCULATE (
IF (
ISBLANK (
SUMX (
FILTER (
GROUPBY (
RevenueChannel,
RevenueChannel[ChannelBucket],
RevenueChannel[Channel],
'Date'[Date],
'Date'[SortDate],
'Date'[AccountingWeek],
'Date'[WeekNumber],
'Date'[AccountingMonthEnglishAbbrYear],
'Date'[AccountingMonth],
'Date'[AccountingYear],
"Revenue", SUMX ( CURRENTGROUP (), RevenueChannel[TransactionRevenue] )
),
'Date'[AccountingYear] >= CurrentAccountingYear
&& 'Date'[SortDate] < CurrentDate
),
[Revenue]
)
),
0,
SUMX (
FILTER (
GROUPBY (
RevenueChannel,
RevenueChannel[ChannelBucket],
RevenueChannel[Channel],
'Date'[Date],
'Date'[SortDate],
'Date'[AccountingWeek],
'Date'[WeekNumber],
'Date'[AccountingMonthEnglishAbbrYear],
'Date'[AccountingMonth],
'Date'[AccountingYear],
"Revenue", SUMX ( CURRENTGROUP (), RevenueChannel[TransactionRevenue] )
),
'Date'[AccountingYear] >= CurrentAccountingYear
&& 'Date'[SortDate] < CurrentDate
),
[Revenue]
)
)
)

Trying to Calculate daily percentage based on Filter and ALLEXCEPT

I have the below question which I asked earlier but along with that I want to filter further along with other columns apart from month and year I want to add Resource Name,RecordType
How to calculate daily percentage over month on month volume?
Below I tried to add allexcept which is not working
Total_Percentage =
VAR TotalPerMonth =
CALCULATE (
SUM ( data1[Actual] ),
FILTER ( data1, data1[Month].[Month] = EARLIER ( data1[Month].[Month] ) ),
FILTER ( data1, data1[Month].[Year] = EARLIER ( data1[Month].[Year] ) ),
ALLEXCEPT(data1,data[RecordType],data1[Resource Name]),
FILTER ( data1, data1[Flag] = 1 )
)
RETURN
DIVIDE ( data1[actual], TotalPerMonth, 0 )
This might be a bit more optimized:
Total_Percentage =
VAR TotalPerMonth =
CALCULATE (
SUM ( data1[Actual] ),
FILTER (
ALLEXCEPT ( data1, data[RecordType], data1[Resource Name] ),
data1[Month].[Month] = EARLIER ( data1[Month].[Month] ) &&
data1[Month].[Year] = EARLIER ( data1[Month].[Year] ) &&
data1[Flag] = 1
)
)
RETURN
DIVIDE ( data1[actual], TotalPerMonth, 0 )
I think this should work for me. If you have any optimized please let me know
Total_Percentage =
VAR TotalPerMonth =
CALCULATE (
SUM ( data1[Actual] ),
FILTER ( data1, data1[Month].[Month] = EARLIER ( data1[Month].[Month] ) ),
FILTER ( data1, data1[Month].[Year] = EARLIER ( data1[Month].[Year] ) ),
FILTER(ALL('data1'),[Resource Name]=EARLIER('data1'[Resource Name])),
FILTER(ALL('data1'),[RecordType]=EARLIER('data1'[RecordType])),
FILTER ( data1, data1[Flag] = 1 )
)
RETURN
DIVIDE ( data1[actual], TotalPerMonth, 0 )

Calculation based on previous row in same column and previous row in another column in power bi

I am trying to calculate NoofEventsInQueue Column as below in power Bi. 157 is the total no of Events
In a calculated column:
NumberOfEventsInQueue =
IF (
ISBLANK ( Table1[NoOfEventsAtATime] ),
BLANK(),
CALCULATE (
SUM ( Table1[NoOfEventsAtATime] ),
FILTER (
ALL ( Table1 ),
Table1[Ref_Time] >= EARLIER ( Table1[Ref_Time] )
)
)
)
If you have blank rows in the middle of your NoOfEventsAtATime column, and still want a value returned on your calculated column, you could use something like:
NumberOfEventsInQueue =
VAR TimeMin =
CALCULATE (
MIN ( Table1[Ref_Time] ),
FILTER (
ALL ( Table1 ),
NOT ISBLANK ( Table1[NoOfEventsAtATime] )
)
)
VAR TimeMax =
CALCULATE (
MAX ( Table1[Ref_Time] ),
FILTER (
ALL ( Table1 ),
NOT ISBLANK ( Table1[NoOfEventsAtATime] )
)
)
VAR Output =
IF (
Table1[Ref_Time] >= TimeMin && Table1[Ref_Time] <= TimeMax,
CALCULATE (
SUM ( Table1[NoOfEventsAtATime] ),
FILTER (
ALL ( Table1 ),
Table1[Ref_Time] >= EARLIER ( Table1[Ref_Time] )
)
),
BLANK()
)
RETURN Output