Skip to content

Commit 1543605

Browse files
committed
new: Example using transform record fields
1 parent e91b757 commit 1543605

3 files changed

Lines changed: 88 additions & 0 deletions
57.8 KB
Loading
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// BaseData
2+
let
3+
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtc31DcyMDJRUNJRCs5PSQTRhgYGBoZghiGYcs7PTgVzQYSBUqwOAW2WIMLIJL8KrskQryYjLHYZEbYLrMQCzDMizS5SHWhMXmCAtZmDeWZE22VCnl0m5NhlSp5dpjjsUlCKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Category = _t, #"Check #" = _t, Item_ID = _t, #"Item Name" = _t, Qty = _t, Modifier_ID = _t]),
4+
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Category", type text}, {"Check #", Int64.Type}, {"Item_ID", Int64.Type}, {"Item Name", type text}, {"Qty", Int64.Type}, {"Modifier_ID", Int64.Type}})
5+
in
6+
#"Changed Type"
7+
8+
// FinalTransformed
9+
let
10+
Source = Table.RemoveColumns( BaseData, { "Modifier_ID" }, MissingField.Ignore ),
11+
12+
/* this step is generated by PBI when you choose "Group" and "All Rows" */
13+
#"Grouped Rows" = Table.Group(Source,
14+
{"Check #"}, {
15+
{"Rows", each _, type table [
16+
Date = nullable date,
17+
Category = nullable text,
18+
#"Check #" = nullable number,
19+
Item_ID = nullable number,
20+
Item Name = nullable text,
21+
Qty = nullable number,
22+
Modifier_ID = nullable number
23+
] }
24+
} ),
25+
26+
/*
27+
For every Table.Group row, transform groups
28+
- Always take the first record for prefixes
29+
- Then enumerate the remaining 1 or more rows, expanding into records.
30+
- or you could use "FillDown"
31+
*/
32+
transformRow = (rows as table ) => [
33+
/* grab the first row */
34+
base = rows{0}?,
35+
/* grab the rest. It's usually 1-to-n rows */
36+
children = Table.Skip( rows, 1 ),
37+
38+
/* prefix all children using the values from base */
39+
newRows = Table.TransformRows(
40+
children,
41+
(childRow) =>
42+
Record.TransformFields(
43+
childRow, {
44+
{
45+
"Item_ID",
46+
each
47+
Text.From( base[Item_ID]? )
48+
& Text.From( _ )
49+
},
50+
{
51+
"Item Name",
52+
each base[Item Name]
53+
& " "
54+
& Text.From( _ )
55+
}
56+
},
57+
MissingField.UseNull
58+
)
59+
),
60+
return = newRows
61+
// ], // toggle these return lines, for easier debugging
62+
][return],
63+
64+
Expand_addColumn = Table.AddColumn(
65+
#"Grouped Rows", "To Expand",
66+
(row) => transformRow( row[Rows] ),
67+
(type { record })
68+
),
69+
#"Expanded Step1" = Table.ExpandListColumn( Expand_addColumn, "To Expand" ),
70+
71+
#"Removed Other Columns" = Table.SelectColumns(
72+
#"Expanded Step1", { "To Expand" }, MissingField.Error ),
73+
74+
// PBI generates this step when you "expand columns"
75+
#"Expanded Step2" = Table.ExpandRecordColumn(
76+
#"Removed Other Columns", "To Expand",
77+
{ "Date", "Category", "Check #", "Item_ID", "Item Name", "Qty"},
78+
{ "Date", "Category", "Check #", "Item_ID", "Item Name", "Qty" }),
79+
80+
#"Changed Type" = Table.TransformColumnTypes(
81+
#"Expanded Step2",{
82+
{"Date", type date}, {"Category", type text},
83+
{"Check #", Int64.Type},
84+
{"Item_ID", Int64.Type},
85+
{"Item Name", type text},
86+
{"Qty", Int64.Type }} )
87+
in
88+
#"Changed Type"

0 commit comments

Comments
 (0)