Don't miss your chance to take the Fabric Data Engineer (DP-600) exam for FREE! Find out how by watching the DP-600 session on-demand now through April 28th.
Learn moreJoin the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now
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
| OrderID | OrderLineID | Hardware | Training |
| 1 | 1 | TRUE | FALSE |
| 1 | 2 | TRUE | FALSE |
| 1 | 3 | FALSE | TRUE |
Solved! Go to Solution.
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
Proud to be a Datanaut!
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
Proud to be a Datanaut!
Check out the April 2026 Power BI update to learn about new features.
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
| User | Count |
|---|---|
| 3 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
| User | Count |
|---|---|
| 8 | |
| 6 | |
| 5 | |
| 5 | |
| 4 |