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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
wangjuan303
Helper III
Helper III

How to use M code create a column contain date and text

Hi Team, I need create a Slicer column like this use M code, this column should show latest date and Previous date, other date show original format, if you have any good idea, please share to me, Thank you ahead. 

wangjuan303_0-1643984824233.png

 

1 ACCEPTED SOLUTION
v-yalanwu-msft
Community Support
Community Support

Hi, @wangjuan303 ;

Try it.

= Table.AddColumn(#"Changed Type", "Custom", each if [Date] = List.Max(#"Changed Type"[Date]) then "Latest date" 
else if [Date] = List.Sort(#"Changed Type"[Date],Order.Descending){1} then "Previous date" else [Date])

The complete M language:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMjQAQiUdMFPXAISUYnWgMsYGpnAZY10gBy5jaWQEl7HUBXJgMoZGRnA9hka6QE5sLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DateKEY = _t, Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DateKEY", Int64.Type}, {"Date", type date}}),
    Custom1 = Table.AddColumn(#"Changed Type", "Custom", each if [Date] = List.Max(#"Changed Type"[Date]) then "Latest date" 
else if [Date] = List.Sort(#"Changed Type"[Date],Order.Descending){1} then "Previous date" else [Date])
in
    Custom1

The final output is shown below:

vyalanwumsft_0-1644389892261.png

 


Best Regards,
Community Support Team_ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
v-yalanwu-msft
Community Support
Community Support

Hi, @wangjuan303 ;

Try it.

= Table.AddColumn(#"Changed Type", "Custom", each if [Date] = List.Max(#"Changed Type"[Date]) then "Latest date" 
else if [Date] = List.Sort(#"Changed Type"[Date],Order.Descending){1} then "Previous date" else [Date])

The complete M language:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMjQAQiUdMFPXAISUYnWgMsYGpnAZY10gBy5jaWQEl7HUBXJgMoZGRnA9hka6QE5sLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DateKEY = _t, Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DateKEY", Int64.Type}, {"Date", type date}}),
    Custom1 = Table.AddColumn(#"Changed Type", "Custom", each if [Date] = List.Max(#"Changed Type"[Date]) then "Latest date" 
else if [Date] = List.Sort(#"Changed Type"[Date],Order.Descending){1} then "Previous date" else [Date])
in
    Custom1

The final output is shown below:

vyalanwumsft_0-1644389892261.png

 


Best Regards,
Community Support Team_ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Pragati11
Super User
Super User

HI @wangjuan303 ,

 

Yes you can definitely get this.

What are the rules behind getting current date, previous date in the date column?

 

Thanks,

Pragati

Best Regards,

Pragati Jain


MVP logo


LinkedIn | Twitter | Blog YouTube 

Did I answer your question? Mark my post as a solution! This will help others on the forum!

Appreciate your Kudos!!

Proud to be a Super User!!

if Date column sorting by desc, then latest date will be the first date, previous date will be second date, Date column won't show everyday, so latest date will max date, previous date is latest date closest day in Date column

need to show the latest two date as "Latest Date" and "Previous Date", the other date show original format , Thank you

Hi @wangjuan303 ,

 

I will ask again as may be I was not clear earlier.

 

  • Latest date makes sense - which should be the maximum date in your date column. Right?
  • Previous date - what should be this? Just one day before latest date or something else? For example: if Latest date is 4th feb 2022, then previous date will be 3rd feb 2022. The reason for this question is - in your screenshot: latest date is 12/25/2021; but previous date is 9/22/2021 which is nearly 2 months before latest date.

Thanks,

Pragati

Best Regards,

Pragati Jain


MVP logo


LinkedIn | Twitter | Blog YouTube 

Did I answer your question? Mark my post as a solution! This will help others on the forum!

Appreciate your Kudos!!

Proud to be a Super User!!

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.