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
BM_
Frequent Visitor

Earlier function in M code

Hi,

 

I have a table with memberships, I want to add a column that always gives the last enddate of a subscription for a specific member.

 

MEMBERIDSubscriptionEndDateLastSubscriptionDate
110-01-201910-01-2020
110-01-202010-01-2020
217-03-201417-03-2014
301-04-201418-12-2022
318-12-202218-12-2022

 

I use the following DAX

LastSubscriptionDate = CALCULATE(MAX('MEMBER'[SubscriptionEndDate]),FILTER('MEMBER','MEMBER'[MEMBERID]=EARLIER('MEMBER'[MEMBERID])))
 
How do I do this in M code?
 
Thanks!
1 ACCEPTED SOLUTION
smpa01
Community Champion
Community Champion

@BM_  the equivalent M is following

smpa01_0-1641958175569.png

let
  src=Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText(
          "Vcu7CQAxEAPRXhQbtJIX7lyL2f7b8Cdz+piZE0KDgqJDA9UecVzxlk59p8krfUtS+YpM/WczqhY=",
          BinaryEncoding.Base64
        ),
        Compression.Deflate
      )
    ),
    let
      _t = ((type nullable text) meta [Serialized.Text = true])
    in
      type table [MEMBERID = _t, SubscriptionEndDate = _t]
  ),
  CT = Table.TransformColumnTypes(
    src,
    {{"MEMBERID", Int64.Type}, {"SubscriptionEndDate", type date}}
  ),
  AC = Table.AddColumn(
    CT,
    "LastSubscriptionDate",
    each List.Max(Table.SelectRows(CT, (q) => q[MEMBERID] = [MEMBERID])[SubscriptionEndDate])
  )
in
  AC

 

smpa01_1-1641958242931.png

 


========================
Did I answer your question? Mark my post as a solution!
Proud to be a Super User
My Custom Visualization Projects
• Plotting Live Sound: Live Sound
• Beautiful News: Women in Parliament, Energy Mix, Shrinking Armies
• Visual Capitalist: Working Hrs
• Others: Easing Graph, Animated Calendar
MayViz Submissions
• Week 1: View
• Week 2: View
• Week 3: View
• Week 4: View
========================

View solution in original post

10 REPLIES 10
smpa01
Community Champion
Community Champion

@BM_  the equivalent M is following

smpa01_0-1641958175569.png

let
  src=Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText(
          "Vcu7CQAxEAPRXhQbtJIX7lyL2f7b8Cdz+piZE0KDgqJDA9UecVzxlk59p8krfUtS+YpM/WczqhY=",
          BinaryEncoding.Base64
        ),
        Compression.Deflate
      )
    ),
    let
      _t = ((type nullable text) meta [Serialized.Text = true])
    in
      type table [MEMBERID = _t, SubscriptionEndDate = _t]
  ),
  CT = Table.TransformColumnTypes(
    src,
    {{"MEMBERID", Int64.Type}, {"SubscriptionEndDate", type date}}
  ),
  AC = Table.AddColumn(
    CT,
    "LastSubscriptionDate",
    each List.Max(Table.SelectRows(CT, (q) => q[MEMBERID] = [MEMBERID])[SubscriptionEndDate])
  )
in
  AC

 

smpa01_1-1641958242931.png

 


========================
Did I answer your question? Mark my post as a solution!
Proud to be a Super User
My Custom Visualization Projects
• Plotting Live Sound: Live Sound
• Beautiful News: Women in Parliament, Energy Mix, Shrinking Armies
• Visual Capitalist: Working Hrs
• Others: Easing Graph, Animated Calendar
MayViz Submissions
• Week 1: View
• Week 2: View
• Week 3: View
• Week 4: View
========================

I really Liked your approach!

I want to rewrite my below DAX Calculated column in M Query but was not sure how to write the Earlier in this funtion, can you help?

 

RANKX ( 
        FILTER (ALL('0410 tbl'),
            '0410 tbl'[GP] = EARLIER('0410 tbl'[GP])
            && '0410 tbl'[ID] = EARLIER('0410 tbl'[ID])
            && '0410 tbl'[DATEDIFF] = EARLIER('0410 tbl'[DATEDIFF])
            && '0410 tbl'[Start_Date] >= _start_date
            && '0410 tbl'[Start_Date] <= _end_date
        ),        
        '0410 tbl'[Start_Date],
        ,
        ASC,
        Dense
    )

 

ImkeF
Community Champion
Community Champion

Hi @NilR ,
can you please provide some sample data with the input and desired output data please?

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

I gave up and then added an statement to the sql and created something similar in my sql. Thank you!

DENSE_RANK() OVER (PARTITION BY.....)

smpa01
Community Champion
Community Champion

@NilR  my M is not as good as my SQL and /DAX. I can provide you a M but might not be optimized and scalable. Hence, looping

++ @AlexisOlson @ImkeF 


========================
Did I answer your question? Mark my post as a solution!
Proud to be a Super User
My Custom Visualization Projects
• Plotting Live Sound: Live Sound
• Beautiful News: Women in Parliament, Energy Mix, Shrinking Armies
• Visual Capitalist: Working Hrs
• Others: Easing Graph, Animated Calendar
MayViz Submissions
• Week 1: View
• Week 2: View
• Week 3: View
• Week 4: View
========================

@NilR For this RANKX example, you can do something similar in Power Query by filtering Start_Date, grouping on the GP, ID, and DATEDIFF columns, sorting the result by Start_Date, and then adding an index column.

Anonymous
Not applicable

Hi @BM_ 

You could use Table.SelectRows() function to keep the "true" data. Then use Table.Group() to get the result.

In Power Query Editor=> Home , select Group By , then group by Column [MEMBERID], then add two aggregations . You can get a result like the screenshot below.

Ailsamsft_0-1641955468117.png

Then expand the column [all] , you will get a table like this :

Ailsamsft_1-1641955468120.png

Go back to Desktop view, add the columns [MEMBERID] [all.SubscriptionEndDate] [last] to a table visual .

Ailsamsft_2-1641955468121.png

I have attached my pbix file , you can refer to it.

 

Best Regard

Community Support Team _ Ailsa Tao

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

visheshjain
Impactful Individual
Impactful Individual

Hi @BM_ ,

 

Please have a look at the following video and I think it should be pretty much what you are looking for.

 

https://youtu.be/hidJ5T_DYQ0

 

Hope this helps.

 

Thank you,

 

Vishesh Jain

Did I answer your question?
If yes, then please mark my post as a solution!

Thank you,
Vishesh Jain

Proud to be a Super User!



BM_
Frequent Visitor

Hi @amitchandak thanks for sharing. My file has over 250k records, so this method is going to cost in performance. See the comments on the blog. Also I don't know how I would include the MAX function in this way.

amitchandak
Super User
Super User

@BM_ , You do not have the alternative of EARLIER there. create one more copy of the table and merge 

 

refer, if needed

https://eriksvensen.wordpress.com/2019/02/28/powerquery-replicate-doing-an-excel-vlookup-in-m/

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

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.