Convert SQL into DAX [closed] - powerbi

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
I am trying to convert the below SQL code to DAX , I am not able to get the same result as per SQL. Can we do this in single measure or do we need two measures and then subtract.
Also I am not able to use EXCEPT function in this case
SELECT Count(DISTINCT( A.AccountNumber)) FROM factTable F
JOIN dimAccount A ON F.AccountKey = A.AccountKey and F.DateSk = A.DateSk
JOIN [dimDate] D on F.DateSk = D.DateSk
WHERE D.IsLastDayOfMonth = 1 and D.DateKey > '2020-11-30' and D.DateKey <= '2021-11-30'
and ISNULL(F.Sale ,0) = 0
and F.IsDeletetd = 0
and A.IsDeletetd = 0
and A.AccountNumber
NOT IN(
SELECT DISTINCT(A.AccountNumber) FROM fact F
JOIN dimAccount A ON F.AccountKey = A.AccountKey and F.DateSk = A.DateSk
JOIN [dimDate] D on F.DateSk = D.DateSk
WHERE D.IsLastDayOfMonth = 1 and D.DateKey > '2020-11-30' and D.DateKey <= '2021-11-30'
and ISNULL(F.Sale ,0) > 0
and F.IsDeleted = 0
and A.Deleted = 0
)

Going to assume your tables are as follows:
factTable
fact
DimAccount
DimDate
I am also going to assume that you have created relationships between Fact DimAccount and Fact and DimDate.
Your logic is that you want a count of all the Accounts in FactTable Table which do not exist in the Fact Table.
Not testing this so there are likely syntax errors, but the logic would have to be somewhat like the code below. You may need to add in additional filters. I think this will perform pretty poorly, but might give you what you nedd.
AccountCount =
var excludetable =
SUMMARIZE(
FILTER(
fact
,fact[IsDeleted] = 0 && RELATED(dimAccount[Deleted]) = 0
&& Fact.Sale > 0
)
,[AccountNumber]
,"DistinctAccounts",Max([AccountNumber])
)
RETURN
Calculate(
DistinctCount('factTable'[AccountNumber])
,not( 'factTable'[AccountNumber] in (ExcludeTable[DistinctAccounts],1,0)
,factTable[IsDeleted]=0
,RELATED(dimAccount[Deleted]) = 0
, Fact.Sale > 0)
)

Related

ABC analysis without aggregation

I never use DAX and I find it very complicated!
I read this https://www.daxpatterns.com/abc-classification/ but I find it complicated.
My dataset is like this https://1drv.ms/x/s!AvMy-Bzzx3mqgwmKSZm6wr_W4VXr?e=Jwwbji&nav=MTVfezAwMDAwMDAwLTAwMDEtMDAwMC0wMDAwLT...
I need to sum column Value if the Name and date are the sums after doing the ABC analysis. The problem is that I can't aggregate before in transform because I need the column Name Fatt!
It is not necessary for a dynamic ABC analysis. The value for each Name and Date is the same...
I appreciate every help! Also video tutorial or something can help me. Thanks!
'Aggregazione fatturato = SUMX(UNION(VALUES(Name),VALUES(Date)),SUM(Value)
Fatturato Cumulato =
VAR CurrentProductSales = 'Misure'[Aggregazione fatturato]
VAR BetterProducts =
FILTER (
Name,
'Misure'[Aggregazione fatturato] >= CurrentProductSales
)
VAR Result =
SUMX (
BetterProducts,
'Misure'[Aggregazione fatturato]
)
RETURN
Result
Cumulated Pct =
DIVIDE (
'Misure'[Fatturato Cumulato],
SUM ('Misure'[Aggregazione fatturato])
ERROR 'Misure'[Aggregazione fatturato] not found

I am trying to get the difference of measure values in DAX but getting 0

I will like to get the difference between two measure values using DAX
AADPMAU_Increase = [AADPMAU_End] - [AADPMAU_Start]
But when I do the subtraction, I am getting 0
I have tried different things, but nothing is working
The two values are start and end values that are set by date selector filters. It appears that the calculation is always treating both as equal therefore returning 0
AADPMAU_Start = CALCULATE(AADPTable[AADPMAU_Sum],FILTER(DimDate,DimDate[StartDate] = DimDate[StartDate]))
AADPMAU_End = CALCULATE(AADPTable[AADPMAU_Sum],FILTER(DimDate,DimDate[EndDate] = DimDate[EndDate]))
AADPMAU Increase =
var at_end = AADPTable[AADPMAU_End]
var at_start = AADPTable[AADPMAU_Start]
return at_end - at_start

Occurrences of a value in a table in Power BI

I got articles from PubMed, I am using Power BI to visualise How many articles are written by each author, each article is written by many authors as shown in the picture any idea to do that, this is my table structure in power bi. What I want to get is a chart visualising :
name of author: number of articles
One other thing is to visualise collaboration between authors, I mean how many time each pair of authors worked together,
any ideas, I searched about it but not result that I need just an orientation.
First if you want to show how many article writes a specific author then you need a list of authors in one column (if you dont have dictionary then you can create this list as NewTable in dax):
AuthorsList = SUMMARIZE(UNION( VALUES(pubmed[name1]), VALUES(pubmed[name2]), VALUES(pubmed[name3])), pubmed[name1])
Then we can create a measure:
ArticleCount =
var __author = SELECTEDVALUE(AuthorsList[name1])
return
CALCULATE( COUNTROWS(VALUES(pubmed[ID])), FILTER(ALL(pubmed), pubmed[name1] = __author || pubmed[name2] = __author || pubmed[name3] = __author))
And for collaboration, I think we can create a cross join ListOfAuthors and use measures like this one (but I don't test it).
ArticleCount =
var __author = SELECTEDVALUE(AuthorsList[name1])
var __author2 = SELECTEDVALUE(AuthorsList[name2])
var __author3 = SELECTEDVALUE(AuthorsList[name3])
return
CALCULATE( COUNTROWS(VALUES(pubmed[ID])), FILTER(ALL(pubmed),
(pubmed[name1] = __author || pubmed[name2] = __author || pubmed[name3] = __author) &&
(pubmed[name1] = __author2 || pubmed[name2] = __author2 || pubmed[name3] = __author2) &&
(pubmed[name1] = __author3 || pubmed[name2] = __author3 || pubmed[name3] = __author3)
)
)

Implementing Binomial Hypothesis Testing significance tests in Power BI (DAX)

This is partly a theory question, and partly an implementation question. My stats is a little rusty...
I am developing a report that is attempting to determine if the difference in occurances between a reference group and a selected group are statistically significant.
So, for example, if something occurs in X of n tests for one group, is it statistically significant than if it 'normally' occurs at a rate of Y of m tests for a different (control) group.
So, my H0 is that the rate is Y of m, per the control group
h1 is that it is not the same as the control group. (ideally, I'd like to use a 1-tailed test, depending if the observed occurrence is greater or less than the control, but my current implementation is 2 tailed)
I'd be comfortable with a CI of 80%.
I've got (slightly pseudocode here):
Zscore =
VAR pControl = DIVIDE(COUNT([Control occurrences]), COUNT([Control Tests])) RETURN
VAR pTest = DIVIDE(COUNT([Test occurrences]), COUNT([Test Tests])) RETURN
VAR controlStandardError =
SQRT(
DIVIDE(
(pControl * (1-pControl)
, COUNT([Control Tests])
)
) RETURN
VAR testStandardError =
SQRT(
DIVIDE(
(pTest* (1-pTest)
, COUNT([Test Tests])
)
) RETURN
DIVIDE(
(pTest - pControl)
, SQRT(POWER(testStandardError, 2) + POWER(controlStandardError, 2)
)
I'm then calculating:
p-Value =
VAR pControl = DIVIDE(COUNT([Control occurrences]), COUNT([Control Tests])) RETURN
IF(pControl > 0,
1 - ABS(NORM.DIST(Zscore, 0, 1, TRUE)
)
I am then displaying in a table each of my non-null hypotheses and filtering the table such that p-Value is less than 0.1. (2-tailed 80%)
am I on the right track here? Or have I completely bungled the theory on this one?
Theory and example tables - Right-tailed (μ > μ₀)
DAX
ControlGroup
XControl = COUNTROWS(FILTER(ControlGroup,ControlGroup[Outcome]=1))
NControl = COUNTROWS(ControlGroup)
pControl = DIVIDE([XControl],[NControl])
TreatmentGroup
XTreatment = COUNTROWS(FILTER(TreatmentGroup,TreatmentGroup[Outcome]=1))
NTreatment = COUNTROWS(TreatmentGroup)
pTreatment = DIVIDE([XTreatment],[NTreatment])
Test Parameters
PooledProportion =
DIVIDE(
[XTreatment]+[XControl],
[NTreatment]+[NControl]
)
ZCritivalValue = NORM.S.INV(0.90)
ZValue = DIVIDE(
[pTreatment]-[pControl],
SQRT(
[PooledProportion]*(1-[PooledProportion])*((1/[NTreatment])+(1/[NControl]))
)
)
Visualization (example)

Year over Year Variance Calculation Error "EARLIER/EARLIEST" refers to an earlier row context which doesn't exist

While trying to calculate Year over Year variance (unsuccessfully for 2 days now), I get the following error message.
EARLIER/EARLIEST refers to an earlier row context which doesn't exist.
YOY Variance = var PreviousYearPrinBal = CALCULATE(SUM(Deals[Principal Balance]),FILTER(ALL(Deals[Close Date].[Year]),Deals[Close Date].[Year] = EARLIER(Deals[Close Date].[Year])))
return
if(PreviousYearPrinBal = BLANK(), BLANK(), Deals[PrincipalBalance] - PreviousYearPrinBal)
In a different SO question, there is a different approach which gives me the following error:
A column specified in the call to function 'SAMEPERIODLASTYEAR' is not of type DATE. This is not supported.
yoy = CALCULATE([PrincipalBalance], SAMEPERIODLASTYEAR(Deals[Close Date].[Year]))
While I have some idea of what these mean, I do not have an idea of how to fix them. Here is my table.
And Here is what I expect as the result.
I've tried posting this question in Power BI community but haven't received an answer yet. Calculate Year over Year Variance.
ACTUAL DATA SAMPLE:
1) Created Year and Year Difference Column (Calculated Column)
Year = YEAR(Table1[Date])
Year Difference = Table1[Year] - Min(Table1[Year])
2) Created the Variance (Measure)
Variance =
Var current_YearDifference = SELECTEDVALUE(Table1[Year Difference])
Var Current_PrincipalBalance = CALCULATE(SUM(Table1[Principal Balance]),FILTER(ALL(Table1), Table1[Year Difference] = (current_YearDifference)))
Var Previous_PrincipalBalance = CALCULATE(SUM(Table1[Principal Balance]),FILTER(ALL(Table1), Table1[Year Difference] = (current_YearDifference - 1)))
Return if(current_YearDifference <> 0, (Current_PrincipalBalance - Previous_PrincipalBalance), 0)
3) Finally Created the Variance in terms of Percentage (Measure),
Variance in terms of Percentage =
Var current_YearDifference = SELECTEDVALUE(Table1[Year Difference])
Var Current_PrincipalBalance = CALCULATE(SUM(Table1[Principal Balance]),FILTER(ALL(Table1), Table1[Year Difference] = (current_YearDifference)))
Var Previous_PrincipalBalance = CALCULATE(SUM(Table1[Principal Balance]),FILTER(ALL(Table1), Table1[Year Difference] = (current_YearDifference - 1)))
Return if(current_YearDifference <> 0, ((Current_PrincipalBalance - Previous_PrincipalBalance) / Previous_PrincipalBalance), 0)
My Final Output
The Principal Balance has the function SUM selected on the Values Pane of the Output Table, where as the Year is Don't Summarize.
My Best Practice
Always Use Vars when creating Complex Measures to simplify the
formula.
Then return only a part of the Measure to check if the output is as expected.
Kindly let me know, if it helps or not.