Forum Discussion
Can this SQL be converted to DAX
- 2 years ago
Hi, Bokazoit
To convert the provided SQL query to DAX, you can follow the steps below:
Create a calculation table in Power BI to replicate FirstSubscriptionCTE:
FirstSubscription = FILTER ( Medlemsdata[FactBetalingslinjer], Medlemsdata[FactBetalingslinjer][BetalingsNr] = 1 && Medlemsdata[FactBetalingslinjer][AbonnementsNr] = 1 && Medlemsdata[FactBetalingslinjer][DatoKeyStoettePeriode] >= MIN(DateTable[Date]) && Medlemsdata[FactBetalingslinjer][DatoKeyStoettePeriode] <= MAX(DateTable[Date]) && NOT (Medlemsdata[FactBetalingslinjer][BetalingsmetodeKey] IN {-1, -24}) && (Medlemsdata[FactBetalingslinjer][StøttetypeKey] IN {24, 25, 26}) )Create metrics for counts and sums:
CustomerCategoryCount = CALCULATE ( COUNT ( Medlemsdata[FactBetalingslinjer][StøttetypeKey] ), FILTER ( Medlemsdata[FactBetalingslinjer], Medlemsdata[FactBetalingslinjer][DonorNummer] IN VALUES (FirstSubscription[DonorNummer]) && Medlemsdata[FactBetalingslinjer][DatoKeyStoettePeriode] <= VALUES (FirstSubscription[DatoKeyStoettePeriode]) ) ) CustomerCategoryAmount = CALCULATE ( SUM ( Medlemsdata[FactBetalingslinjer][#Bogført betalt beløb] ), FILTER ( Medlemsdata[FactBetalingslinjer], Medlemsdata[FactBetalingslinjer][DonorNummer] IN VALUES (FirstSubscription[DonorNummer]) && Medlemsdata[FactBetalingslinjer][DatoKeyStoettePeriode] <= VALUES (FirstSubscription[DatoKeyStoettePeriode]) ) )To display the results, create a table visual object in Power BI and add the following fields:
Customer from Medlemsdata[FactBetalingslinjer][DonorNummer]
Category from Medlemsdata[DimStøttetype][Støttetype]
pcs as a measure of CustomerCategoryCount
Amount as a measure of CustomerCategoryAmountIf desired, you can also create a summary table in DAX for visual use:
SummaryTable = SUMMARIZE ( Medlemsdata[FactBetalingslinjer], Medlemsdata[FactBetalingslinjer][DonorNummer], Medlemsdata[DimStøttetype][Støttetype], "pcs", [CustomerCategoryCount], "Amount", [CustomerCategoryAmount] )By following these steps, you can replicate the functionality of SQL queries in Power BI using DAX
hackcrr
If this post helps, then please consider Accept it as the solution and kudos to this post to help the other members find it more quickly
Hi, Bokazoit
To convert the provided SQL query to DAX, you can follow the steps below:
Create a calculation table in Power BI to replicate FirstSubscriptionCTE:
FirstSubscription =
FILTER (
Medlemsdata[FactBetalingslinjer],
Medlemsdata[FactBetalingslinjer][BetalingsNr] = 1
&& Medlemsdata[FactBetalingslinjer][AbonnementsNr] = 1
&& Medlemsdata[FactBetalingslinjer][DatoKeyStoettePeriode] >= MIN(DateTable[Date])
&& Medlemsdata[FactBetalingslinjer][DatoKeyStoettePeriode] <= MAX(DateTable[Date])
&& NOT (Medlemsdata[FactBetalingslinjer][BetalingsmetodeKey] IN {-1, -24})
&& (Medlemsdata[FactBetalingslinjer][StøttetypeKey] IN {24, 25, 26})
)Create metrics for counts and sums:
CustomerCategoryCount =
CALCULATE (
COUNT ( Medlemsdata[FactBetalingslinjer][StøttetypeKey] ),
FILTER (
Medlemsdata[FactBetalingslinjer],
Medlemsdata[FactBetalingslinjer][DonorNummer] IN VALUES (FirstSubscription[DonorNummer])
&& Medlemsdata[FactBetalingslinjer][DatoKeyStoettePeriode] <= VALUES (FirstSubscription[DatoKeyStoettePeriode])
)
)
CustomerCategoryAmount =
CALCULATE (
SUM ( Medlemsdata[FactBetalingslinjer][#Bogført betalt beløb] ),
FILTER (
Medlemsdata[FactBetalingslinjer],
Medlemsdata[FactBetalingslinjer][DonorNummer] IN VALUES (FirstSubscription[DonorNummer])
&& Medlemsdata[FactBetalingslinjer][DatoKeyStoettePeriode] <= VALUES (FirstSubscription[DatoKeyStoettePeriode])
)
)To display the results, create a table visual object in Power BI and add the following fields:
Customer from Medlemsdata[FactBetalingslinjer][DonorNummer]
Category from Medlemsdata[DimStøttetype][Støttetype]
pcs as a measure of CustomerCategoryCount
Amount as a measure of CustomerCategoryAmount
If desired, you can also create a summary table in DAX for visual use:
SummaryTable =
SUMMARIZE (
Medlemsdata[FactBetalingslinjer],
Medlemsdata[FactBetalingslinjer][DonorNummer],
Medlemsdata[DimStøttetype][Støttetype],
"pcs", [CustomerCategoryCount],
"Amount", [CustomerCategoryAmount]
)By following these steps, you can replicate the functionality of SQL queries in Power BI using DAX
hackcrr
If this post helps, then please consider Accept it as the solution and kudos to this post to help the other members find it more quickly