Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
bpdax
New Member

Convert SQL To DAX

Hi,

 

I am very new to DAX and have to convert some SSRS into paginated reports using powerbi data source as the model has already been published. I am stuggling converting this stored procedure into DAX, any help would be very much appreciated. The parameters in this stored procedure are multi-valued ones:

 
SELECT DISTINCT
table.StaffID,
Team,
FirstName,
LastName,
CourseName,
DueOnDate
 
FROM [database].[dbo].[table]
INNER JOIN 
 
(
SELECT 
StaffID, 
SUM(TrainingTracked) TrainingTracked, 
SUM(Denominator) - SUM(Numerator) DemNumDiff, 
SUM(TrackedDenominator) - SUM(TrackedNumerator) TrackDemNumDiff,
CASE WHEN SUM(TrainingTracked) = 16 and SUM(TrackedDenominator) - SUM(TrackedNumerator) > 0 THEN 1
WHEN SUM(TrainingTracked) <> 16 and SUM(Denominator) - SUM(Numerator) > 0 THEN 1
 
ELSE 0
END NotComplaint
 
FROM [database].[dbo].[table]
WHERE [Compliant] = 0
AND Team IN (@Team)
AND Course IN (@Course)
GROUP BY StaffID
 
) A 
ON table.StaffID = a.StaffID
 
 
WHERE A.NotComplaint = 1
 
Thank you
 
1 ACCEPTED SOLUTION
v-yaningy-msft
Community Support
Community Support

Hi, @bpdax 

Based on your description, you want to implement DAX statements that have similar functionality to SQL statements, and the following are possible DAX statements. If it does not work, you can share the data without sensitive information, the expected effect and the logic to achieve the effect. Feel free for anything about this.

 

VAR BaseTable = 
    CALCULATETABLE(
        'database'[table],
        'database'[Compliant] = 0,
        'database'[Team] IN @Team,
        'database'[Course] IN @Course
    )

VAR SummaryTable = 
    SUMMARIZE(
        BaseTable,
        'database'[StaffID],
        "TrainingTracked", SUM('database'[TrainingTracked]),
        "DemNumDiff", SUM('database'[Denominator]) - SUM('database'[Numerator]),
        "TrackDemNumDiff", SUM('database'[TrackedDenominator]) - SUM('database'[TrackedNumerator]),
        "NotComplaint", IF(
            [TrainingTracked] = 16 && [TrackDemNumDiff] > 0, 1,
            IF([TrainingTracked] <> 16 && [DemNumDiff] > 0, 1, 0)
        )
    )

VAR FinalTable = 
    FILTER(
        SummaryTable,
        [NotComplaint] = 1
    )

EVALUATE
    SELECTCOLUMNS(
        FinalTable,
        "StaffID", 'database'[StaffID],
        "Team", 'database'[Team],
        "FirstName", 'database'[FirstName],
        "LastName", 'database'[LastName],
        "CourseName", 'database'[CourseName],
        "DueOnDate", 'database'[DueOnDate]
    )

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

View solution in original post

3 REPLIES 3
v-yaningy-msft
Community Support
Community Support

Hi, @bpdax 

Based on your description, you want to implement DAX statements that have similar functionality to SQL statements, and the following are possible DAX statements. If it does not work, you can share the data without sensitive information, the expected effect and the logic to achieve the effect. Feel free for anything about this.

 

VAR BaseTable = 
    CALCULATETABLE(
        'database'[table],
        'database'[Compliant] = 0,
        'database'[Team] IN @Team,
        'database'[Course] IN @Course
    )

VAR SummaryTable = 
    SUMMARIZE(
        BaseTable,
        'database'[StaffID],
        "TrainingTracked", SUM('database'[TrainingTracked]),
        "DemNumDiff", SUM('database'[Denominator]) - SUM('database'[Numerator]),
        "TrackDemNumDiff", SUM('database'[TrackedDenominator]) - SUM('database'[TrackedNumerator]),
        "NotComplaint", IF(
            [TrainingTracked] = 16 && [TrackDemNumDiff] > 0, 1,
            IF([TrainingTracked] <> 16 && [DemNumDiff] > 0, 1, 0)
        )
    )

VAR FinalTable = 
    FILTER(
        SummaryTable,
        [NotComplaint] = 1
    )

EVALUATE
    SELECTCOLUMNS(
        FinalTable,
        "StaffID", 'database'[StaffID],
        "Team", 'database'[Team],
        "FirstName", 'database'[FirstName],
        "LastName", 'database'[LastName],
        "CourseName", 'database'[CourseName],
        "DueOnDate", 'database'[DueOnDate]
    )

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

Thank you for your help. I managed to write a similar piece of code, I just changed the last part of the code to a  NaturalLeftOuterJoin to get the rest of the things I needed.

bhanu_gautam
Super User
Super User

Hi @bpdax ,

 

Please try using below method

EVALUATE
VAR NotCompliantTable =
SUMMARIZE (
FILTER (
VALUES ( 'database'[Team] ),
[Compliant] = 0
&& [NotComplaint] = 1
),
'database'[StaffID],
"TrainingTracked", SUM ( 'database'[TrainingTracked] ),
"DemNumDiff", SUM ( 'database'[Denominator] ) - SUM ( 'database'[Numerator] ),
"TrackDemNumDiff", SUM ( 'database'[TrackedDenominator] ) - SUM ( 'database'[TrackedNumerator] ),
"NotComplaint",
SWITCH (
TRUE (),
SUM ( 'database'[TrainingTracked] ) = 16
&& SUM ( 'database'[TrackedDenominator] ) - SUM ( 'database'[TrackedNumerator] ) > 0, 1,
SUM ( 'database'[TrainingTracked] ) <> 16
&& SUM ( 'database'[Denominator] ) - SUM ( 'database'[Numerator] ) > 0, 1,
0
)
)
EVALUATE
SUMMARIZE (
'database',
'database'[StaffID],
'database'[Team],
'database'[FirstName],
'database'[LastName],
'database'[CourseName],
'database'[DueOnDate]
)
EVALUATE
FILTER (
'database',
'database'[StaffID]
IN VALUES ( NotCompliantTable[StaffID] )
)

 

Please accept as solution and give kudos if it helps




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.

Top Solution Authors