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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Get de colum calculated with language M

Hi, I have a table below:

Rick_ferreira_0-1608574446236.png


I need to create a end data column as:

Rick_ferreira_1-1608574553852.png

The end data is calculated as:
1- If exist a new status each id, the end data is equal a first data de next status.
2- If not exist a new status, the end data is equal a data now. 

I need to build this columns in power bi query, but i don´t can it. 

Someone can help me please?

2 ACCEPTED SOLUTIONS
AlB
Community Champion
Community Champion

Hi @Anonymous 

You could also do this in DAX but if you need in in PQ, place the following M code in a blank query to see the steps:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYkN9Q30jAyMDpVidaCUjqKCxvilC0BgqaKpvhiroBNJupG+Bqh8kagYXjAUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Status = _t, #"First data" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Status", type text}, {"First data", type date}}),

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let aux_ = Table.SelectRows(#"Changed Type", (inner)=> inner[Status]<>[Status] and inner[First data] > [First data] and inner[ID]=[ID])[First data], res_ = if List.Count(aux_) = 0 then "now" else List.Min(aux_) in res_, type text)
in
    #"Added Custom"

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

View solution in original post

AlB
Community Champion
Community Champion

@Anonymous 

See it all at work in the attached file.

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

View solution in original post

6 REPLIES 6
Ashish_Mathur
Super User
Super User

Hi,

This calculated column formula works

=if(CALCULATE(COUNTROWS(Data),FILTER(Data,Data[ID]=EARLIER(Data[ID])&&Data[First Data]>EARLIER(Data[First Data])))<1,today(),CALCULATE(min(Data[First Data]),FILTER(Data,Data[ID]=EARLIER(Data[ID])&&Data[First Data]>EARLIER(Data[First Data]))))

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
AlB
Community Champion
Community Champion

@Anonymous 

See it all at work in the attached file.

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

mahoneypat
Microsoft Employee
Microsoft Employee

Try removing the double quotes around your column names when inside the [ ] in the #"Added Custom" step.

Regards,

Pat





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

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


AlB
Community Champion
Community Champion

Hi @Anonymous 

You could also do this in DAX but if you need in in PQ, place the following M code in a blank query to see the steps:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYkN9Q30jAyMDpVidaCUjqKCxvilC0BgqaKpvhiroBNJupG+Bqh8kagYXjAUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Status = _t, #"First data" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Status", type text}, {"First data", type date}}),

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let aux_ = Table.SelectRows(#"Changed Type", (inner)=> inner[Status]<>[Status] and inner[First data] > [First data] and inner[ID]=[ID])[First data], res_ = if List.Count(aux_) = 0 then "now" else List.Min(aux_) in res_, type text)
in
    #"Added Custom"

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

Anonymous
Not applicable

Error appears, when I adapted for my table. jjj.png

Greg_Deckler
Community Champion
Community Champion

@ImkeF @edhans 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.

Top Solution Authors