Trying to Calculate daily percentage based on Filter and ALLEXCEPT - powerbi

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 )

Related

How to write this Logic in DAX

I'd like to know how to write this line bellow in DAX. I'm a beginner and I'm having a lot of problems trying to write this code in Dax to return the value I need.
(select
top 1 Payor.CompanyName
from Payor, PatientPayor
where PatientPayor.PatientSer = pat.PatientSer
and Payor.PayorSer = PatientPayor.PayorSer) as conv,
This is what I tried to do so far.
Convenio =
TOPN ( 1, VALUES ( Payor[CompanyName] ), PatientPayor[PatientSer] )
= FILTER ( Patient, PatientPayor[PatientSer] = Patient[PatientSer] )
I did solve it with the code bellow:
formula =
VAR currpayorSer =
SELECTEDVALUE ( PatientPayor[PayorSer] )
VAR currPatientSer =
SELECTEDVALUE ( PatientPayor[PatientSer] )
RETURN
CALCULATE (
MIN ( Payor[CompanyName] ),
FILTER ( ALLSELECTED ( Payor ), Payor[PayorSer] = currpayorSer ),
FILTER ( ALLSELECTED ( Patient ), Patient[PatientSer] = currPatientSer )
)

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

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

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

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