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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Super User
Super User

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
Super User
Super User

@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
Super User
Super User

@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
Employee
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
Super User
Super User

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
Super User
Super User

@ImkeF @edhans 


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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