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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
BenDC
Regular Visitor

Custom Column based on comparing a value against a shared identifier

Hi,

 

I have achieved this by duplicating the data source and using the GROUP BY function, however, I wondered if there was a cleaner way to achieve what I am looking for.

 

In the example below, I have a table structured for orders which can include hardware elements and training elements. I want to add a new custom column, 'Training Order'. The logic being, if there is a single Order Line on the same OrderID where Training is TRUE, add this column to all lines of the order with the value of TRUE. The reverse logic applies for FALSE if there are no lines.

 

I would like to know if there is a way to use a nested query that queries the data in the same source, without having to create a separate copy of the source and join it back in.

 

If I were writing this as an SQL query, for example, I would create a query like this:

 

SELECT

*,

Training Order = (CASE

WHEN OrderID IN (SELECT OrderID FROM Orders WHERE Training = TRUE")

THEN "TRUE" 
ELSE "FALSE"

END) 

FROM Orders 

 

OrderIDOrderLineIDHardwareTraining
11TRUEFALSE
12TRUEFALSE
13FALSETRUE
1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @BenDC ,

 

In Power Query, you can group then expand the same table.

1) Group by [Order ID] and create an 'All Rows' aggregate column

2) Add a custom column that evaluates the [Training] column of the nested tables

3) Expand your grouped rows ack out again

 

Paste this over the default code in Advanced Editor to follow the steps I took:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSAeOQoFBXIOXm6BPsqhSrA5EwwiVhDBeBKoiNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [OrderID = _t, OrderLineID = _t, Hardware = _t, Training = _t]),
    chgTypes = Table.TransformColumnTypes(Source,{{"OrderID", Int64.Type}, {"OrderLineID", Int64.Type}, {"Hardware", type logical}, {"Training", type logical}}),
    groupRows = Table.Group(chgTypes, {"OrderID"}, {{"data", each _, type table [OrderID=nullable number, OrderLineID=nullable number, Hardware=nullable logical, Training=nullable logical]}}),
    addTrainingOrder = Table.AddColumn(groupRows, "trainingOrder", each if List.Contains([data][Training], true) then "TRUE" else "FALSE"),
    expandDataCol = Table.ExpandTableColumn(addTrainingOrder, "data", {"OrderLineID", "Hardware", "Training"}, {"OrderLineID", "Hardware", "Training"})
in
    expandDataCol

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

1 REPLY 1
BA_Pete
Super User
Super User

Hi @BenDC ,

 

In Power Query, you can group then expand the same table.

1) Group by [Order ID] and create an 'All Rows' aggregate column

2) Add a custom column that evaluates the [Training] column of the nested tables

3) Expand your grouped rows ack out again

 

Paste this over the default code in Advanced Editor to follow the steps I took:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSAeOQoFBXIOXm6BPsqhSrA5EwwiVhDBeBKoiNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [OrderID = _t, OrderLineID = _t, Hardware = _t, Training = _t]),
    chgTypes = Table.TransformColumnTypes(Source,{{"OrderID", Int64.Type}, {"OrderLineID", Int64.Type}, {"Hardware", type logical}, {"Training", type logical}}),
    groupRows = Table.Group(chgTypes, {"OrderID"}, {{"data", each _, type table [OrderID=nullable number, OrderLineID=nullable number, Hardware=nullable logical, Training=nullable logical]}}),
    addTrainingOrder = Table.AddColumn(groupRows, "trainingOrder", each if List.Contains([data][Training], true) then "TRUE" else "FALSE"),
    expandDataCol = Table.ExpandTableColumn(addTrainingOrder, "data", {"OrderLineID", "Hardware", "Training"}, {"OrderLineID", "Hardware", "Training"})
in
    expandDataCol

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors