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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Dev-13
Helper I
Helper I

Direct query

I want to use direct query to pick out the latest record for a user. A user may have 5 records in the database and I want to pick the latest one. The latest one can be determined by the user Id and max mod id as there is a new id created every time a user registers for one. 

 

Is this the most efficient query to pick the latest record

 

 

 

SELECT

    MC.USERID

   ,OG.OrganisationID

   , MC.Id AS ModID

   , MC.TYPE AS ResID

   ,CAST(MC.EndDateTime AS DATETIME2) AS EndDate

   ,CAST(MC.ExpiryDate AS DATETIME2) ExpiryDate

   ,CASE

              WHEN ExpiryDate >= GETDATE() AND

                     IsDeleted = 0 THEN 1

              ELSE 0

       END AS Compliance

 

FROM

MC

LEFT JOIN OG

       ON MC.USERID = OG. USERID

 

INNER JOIN 

 

(

 

       SELECT

          MC.USERID

         ,MAX(MC.Id) AS Max_Record

       FROM MC

       GROUP BY MC.USERID) Latest

       ON MC.USERID = Latest.USERID

              AND MC.Id = Latest.Max_Record

1 REPLY 1
Anonymous
Not applicable

Hi @Dev-13 

The query you write is right, did you want to translate it to M code, if you want to translate it to M code, can you provide some sample data that can provide more suggestion for you.

 

Best Regards!

Yolo Zhu

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.