How to write this Logic in DAX - powerbi

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

Related

PowerBI DAX - countdistinct based on two columns

I would like to calculate column 'E' in DAX
I wanted to count distinct 'B'
based on 'C' and filter on 'D' = instock.
I later want to sum column E -- what is the best way to do this to exclude the duplications
Thanks in advance
You mean you want a Calculated Column?
=
VAR ThisProductCode = 'Table'[product code]
RETURN
CALCULATE(
DISTINCTCOUNT( 'Table'[size] ),
FILTER(
'Table',
'Table'[product code] = ThisProductCode
&& 'Table'[stock] = "instock"
)
)
Not sure what you mean by your second request, though.
Your question is split into 2 parts: Go step by step then:
First Step Code:
Result =
CALCULATE (
DISTINCTCOUNT ( 'Product'[size] ),
ALLEXCEPT ( 'Product', 'Product'[product code] ),
'Product'[stock] = "instock"
)
Second Step:
Result =
VAR TblSummary =
ADDCOLUMNS (
VALUES ( 'Product'[product code] ),
"DISCTotal",
CALCULATE (
DISTINCTCOUNT ( 'Product'[size] ),
ALLEXCEPT ( 'Product', 'Product'[product code] ),
'Product'[stock] = "instock"
)
)
RETURN
SUMX ( TblSummary, [DISCTotal] )

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

DAX - Find and return the category value with the highest summarized measure

In the following example,
Highest Shipping Unit - should be the "ActualArea" where TotalPS has the maximum value.
This is a summarized table grouped by - Customer_ID, ProductDesignation,Package_CD,ActualArea
As you can see above I cannot produce the desired results. The DAX for HighestShippingUnit measure is as follows. I can't seem to figure out what I'm doing wrong.
HighestShippingUnit =
VAR tab =
SUMMARIZE (
ALLEXCEPT (
DeviationReport,
DeviationReport[CUSTOMER_ID],
DeviationReport[ProductDesignation],
DeviationReport[PACKAGE_CD]
),
DeviationReport[ActualArea],
"GroupTotalPS", SUM ( DeviationReport[TotalPS] )
)
VAR maxps =
MAXX ( tab, [GroupTotalPS] )
RETURN
CALCULATE (
MAXX ( FILTER ( tab, [GroupTotalPS] = maxps ), MAX ( [ActualArea] ) )
)
DeviationReport is the name of my table in the dataset.
Please can you tell me what I'm doing wrong in my DAX or how to do it in a more efficient manner?
You were actually very close with your attempt.
If you change the final line from
CALCULATE (
MAXX ( FILTER ( tab, [GroupTotalPS] = maxps ), MAX ( [ActualArea] ) )
)
to
MAXX ( FILTER ( tab, [GroupTotalPS] = maxps ), [ActualArea] )
then it should work.
The reason is that using CALCULATE re-introduces the ActualArea filter context that you removed with ALLEXCEPT so that you only see the current ActualArea.
Thanks to #Alexis Olson. I managed to fix the problem.
The DAX is as follows -
HighestShippingUnit =
VAR tab =
SUMMARIZE (
ALLEXCEPT (
DeviationReport,
DeviationReport[CUSTOMER_ID],
DeviationReport[ProductDesignation],
DeviationReport[PACKAGE_CD]
),
DeviationReport[CUSTOMER_ID],
DeviationReport[ProductDesignation],
DeviationReport[PACKAGE_CD],
DeviationReport[ActualArea],
"GroupTotalPS", SUM ( DeviationReport[TotalPS] )
)
RETURN
MAXX(TOPN(1, tab, [GroupTotalPS]), [ActualArea])
)

Percentage change from category to category

I will like to create a new column that calculates the percentage change from JAN-FEB-MAR-APR-MAY-JUN which are in the COLUMN "MONTH" based on the COLUMN "TOTAL".
Here is a script I have tried but its not working
change perc =
VAR ValueLastMONTH =
CALCULATE (
SUM ( Sheet1[TOTAL] ),
FILTER (
Sheet1,
Sheet1[MONTH]
= ( EARLIER ( Sheet1[MONTH] ) - 1 )
&&Sheet1 = EARLIER ( Sheet1[CATEGORY] )
)
)
RETURN
IF (
ISBLANK ( ValueLastMONTH ),
0,
( Sheet1[TOTAL] - ValueLastMONTH )
/ ValueLastMONTH
Here is the link to the power bi file. enter link description here
The Column 'Month' is not of type date. How would PowerBi know the text APR represents April? You need to make this column a date.
Now you need to change the script to work with DateDiff:
change perc =
VAR ValueLastMONTH =
CALCULATE (
SUM ( Sheet1[TOTAL] ),
FILTER (
Sheet1,
DATEDIFF(Sheet1[MONTH], EARLIER ( Sheet1[MONTH] ),MONTH) = 1
&& Sheet1[CATEGORY] = EARLIER ( Sheet1[CATEGORY] )
)
)
RETURN
IF (
ISBLANK ( ValueLastMONTH );
0;
( Sheet1[TOTAL] - ValueLastMONTH )
/ ValueLastMONTH)

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 )