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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

Adding Calculated field in Power Query

Hi,

Hope someone can help.

I have data as in the table below.

IDStart DateEnd DateStatusReason
101/04/2019 30/04/2019AR1
130/04/2019 01/10/2020BR6
101/10/2020 A 
202/03/2020 04/05/2021BR2
204/05/2021 B 
302/02/2019 06/03/2020CR1
306/03/2020 08/09/2020BR2
308/09/2020 03/02/2021AR2
303/02/2021 12/06/2021AR4

 

I want to add a calculated field, in Power Query, which should be 'Yes' for each ID if reason, R1 has been selected in atleast one of the rows and 'No' otherwise. Please see required table below.

IDStart Date End DateStatusReasonCalcCol 
101/04/2019 30/04/2019AR1Yes
130/04/2019 01/10/2020BR6Yes
101/10/2020 A Yes
202/03/2020 04/05/2021BR2No
204/05/2021 B No
302/02/2019 06/03/2020CR1Yes
306/03/2020 08/09/2020BR2Yes
308/09/2020 03/02/2021AR2Yes
303/02/2021 12/06/2021AR4Yes

 

Is this possible?

 

Many Thanks in advance.

1 ACCEPTED SOLUTION
mahoneypat
Employee
Employee

This kind of thing is better done with DAX, but here's one way to do it in the query editor.  To see how it works, just create a blank query, open the Advanced Editor and replace the text there with the M code below.

 

mahoneypat_0-1628907420245.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XY5BDsMgDAS/YvkcibVJUHNM+4NeI/7/jRhCg90Dh9WMlz1PFl4YkrAmhewWKMOlw95XuC63GRjZncCSwtK7meUxA6PRRB1rw5qQH2yd2FqSX5FO0zMa/C7Ko0jnouJqP3N7N0v48pWwh+06Tc/IjvoPfcDxZ3pGYltKMFeu9QI=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Start Date" = _t, #"End Date" = _t, Status = _t, Reason = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each let 
thisID = [ID],
thisIDlist = Table.SelectRows(Source, each [ID] = thisID)[Reason]
in 
if List.Contains(thisIDlist, "R1") then "Yes" else "No")
in
    #"Added Custom"

 

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


View solution in original post

6 REPLIES 6
mahoneypat
Employee
Employee

This kind of thing is better done with DAX, but here's one way to do it in the query editor.  To see how it works, just create a blank query, open the Advanced Editor and replace the text there with the M code below.

 

mahoneypat_0-1628907420245.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XY5BDsMgDAS/YvkcibVJUHNM+4NeI/7/jRhCg90Dh9WMlz1PFl4YkrAmhewWKMOlw95XuC63GRjZncCSwtK7meUxA6PRRB1rw5qQH2yd2FqSX5FO0zMa/C7Ko0jnouJqP3N7N0v48pWwh+06Tc/IjvoPfcDxZ3pGYltKMFeu9QI=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Start Date" = _t, #"End Date" = _t, Status = _t, Reason = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each let 
thisID = [ID],
thisIDlist = Table.SelectRows(Source, each [ID] = thisID)[Reason]
in 
if List.Contains(thisIDlist, "R1") then "Yes" else "No")
in
    #"Added Custom"

 

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


Anonymous
Not applicable

Many thanks @mahoneypat 

Anonymous
Not applicable

Thanks @wdx223_Daniel and @Syndicate_Admin for your quick response. Sorry, I should have mentioned I was trying to do this in Power Query.

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XY5BDsMgDAS/YvkcibVJUHNM+4NeI/7/jRhCg90Dh9WMlz1PFl4YkrAmhewWKMOlw95XuC63GRjZncCSwtK7meUxA6PRRB1rw5qQH2yd2FqSX5FO0zMa/C7Ko0jnouJqP3N7N0v48pWwh+06Tc/IjvoPfcDxZ3pGYltKMFeu9QI=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Start Date" = _t, #"End Date" = _t, Status = _t, Reason = _t]),
Custom1 = Table.Buffer(Table.Group(Source,"ID",{"n",each List.Contains([Reason],"R1")})),
Custom2 = Table.AddColumn(Source,"CalcCol",each Custom1{[ID=[ID]]}?[n]?)
in
Custom2

Syndicate_Admin
Administrator
Administrator

wdx223_Daniel_0-1628824043283.png

 

wdx223_Daniel
Super User
Super User

wdx223_Daniel_0-1628824043283.png

 

Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

Top Solution Authors
Top Kudoed Authors